Pu Zhibing
2025-05-09 8580866e175ad0050ee9c5ea3f757856fc242c39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.stylefeng.guns.modular.taxi.controller;
 
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.PushUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.SignVerificationUtil;
import com.stylefeng.guns.modular.taxi.dao.OrderTaxiMapper;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 支付回调控制器
 */
@RestController
@RequestMapping("/base")
public class TaxiCallbackController {
 
    @Autowired
    private ICompanyService companyService;
 
    @Autowired
    private IPushOrderService pushOrderService;
    @Autowired
    private IDriverService driverService;
    @Autowired
    private PushUtil pushUtil;
    @Resource
    private OrderTaxiMapper orderTaxiMapper;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ICallingAreaService callingAreaService;
    @Autowired
    private ICompanyCityService companyCityService;
    @ResponseBody
    @PostMapping("/callback")
    @ApiOperation(value = "AI回调", tags = {"AI回调"}, notes = "")
    public ResultUtil callback(@RequestBody JSONObject req, HttpServletRequest request) throws Exception {
        System.err.println("回调信息==========》"+req);
 
        String appKey = request.getHeader("appKey");
        String ts = request.getHeader("ts");
        String signature = request.getHeader("signature");
 
        // 验签
        Boolean flag = SignVerificationUtil.signVerification(appKey, ts, signature, req);
        if (!flag) {
            System.err.println("验签失败");
            return ResultUtil.error("验签失败");
        }
 
        // 遍历列表以获取每个结果的地址信息
        OrderTaxi orderTaxi = new OrderTaxi();
 
        String phone = req.getStr("phone");
        JSONObject outputParam = req.getJSONObject("outputParam");
        String area = outputParam.getStr("area");
 
        System.err.println("phone++++"+phone);
 
        String downloadUrl = ""
;
        for (Object ivrNodeAudio : req.getJSONArray("ivrNodeAudioList")) {
            JSONObject jsonObject = (JSONObject) ivrNodeAudio;
            downloadUrl = jsonObject.getStr("downloadUrl");
            if(StringUtils.hasLength(downloadUrl)){
                break;
            }
        }
 
        //判断该号码是否为黑名单用户
        try {
            UserInfo userInfo = userInfoService.queryByPhone(phone);
//                orderTaxi.setUserId(userInfo.getId());
            if (userInfo!=null&&userInfo.getIsBlack()==1){
                return ResultUtil.success();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 查询电召区域
        CallingArea callingArea = callingAreaService.selectOne(new EntityWrapper<CallingArea>().eq("areaCode", area));
        if(Objects.nonNull(callingArea)){
            // 打印或处理地址信息
            System.out.println("地址信息:" + callingArea.getArea());
            orderTaxi.setStartAddress(callingArea.getArea());
        }else {
            return ResultUtil.error("电召未开通该区域城市");
        }
        //创建订单
        orderTaxi.setAudioLinkUrl(downloadUrl);
        orderTaxi.setInsertTime(new Date());
        orderTaxi.setIsReassign(1);
        orderTaxi.setOrderSource(6);
        orderTaxi.setState(1);//待接单
        orderTaxi.setOrderNum(getOrderNum());
        orderTaxi.setPassengersPhone(phone);
        orderTaxi.setTravelTime(new Date());
        orderTaxi.setOrderType(1);
        orderTaxiMapper.insert(orderTaxi);
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(orderTaxi.getState() == 2){
                    //推送司机订单状态
                    pushUtil.pushOrderState(2, orderTaxi.getDriverId(), orderTaxi.getId(), 2, orderTaxi.getState(), 0);
                }
            }
        }).start();
        if(orderTaxi.getState() == 1){
            //推送司机抢单
            try {
                this.pushOrder(orderTaxi,callingArea);
            }catch (Exception e){
            }
        }
 
        return ResultUtil.success();
    }
    @Autowired
    private IOrderTaxiService orderTaxiService;
    public synchronized String getOrderNum() throws Exception{
        int size = orderTaxiService.selectCount(null);
        return "TAXI" + String.valueOf(1000000 + size + 1).substring(1);
    }
    public static List<Integer> orderIds = new ArrayList<>();
 
    public void pushOrder(OrderTaxi orderTaxi,CallingArea callingArea) throws Exception{
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    orderIds.add(orderTaxi.getId());//添加记录,防止调用接口重复提醒无人接单
                    Company query = companyService.selectById(1);//获取起点所属分公司
                    List<PushOrder> querys = pushOrderService.querys(null, 2, query.getId());//获取需要推送的次数
                    // 优先查询区域
                    List<Company> companys = companyCityService.queryCall(callingArea.getAreaCode());
                    System.err.println("根据区域查询出来的分公司======"+companys);
                    List<Integer> companyIds = companys.stream().map(Company::getId).collect(Collectors.toList());
                    for(int i = 1; i <= querys.size(); i++){
                        PushOrder pushOrder = pushOrderService.querys(i, 2, query.getId()).get(0);
                        //获取空闲司机
                        List<Driver> list = driverService.queryIdleDriverAllCall(2, orderTaxi.getStartLon(), orderTaxi.getStartLat(), pushOrder.getPushDistance(), companyIds);//所有附近空闲司机
//                        List<Driver> list = driverService.queryIdleDriverAll(2, orderTaxi.getStartLon(), orderTaxi.getStartLat(), pushOrder.getPushDistance(), null);//所有附近空闲司机
                        if(list.size() > 0){
                            double driverProportion = pushOrder.getDriverProportion() / 100;//推送占比计算成小数
                            int lastIndex = Double.valueOf(list.size() * driverProportion).intValue();//计算占比转成整数(下标截取)
                            list = list.subList(0, lastIndex);//获取空闲司机中占比数据
                            for(Driver driver : list){//开始进行推送
                                pushUtil.pushOrderStateVedio(2, driver.getId(), orderTaxi.getId(), 2, orderTaxi.getState(), pushOrder.getPushTime(),orderTaxi.getAudioLinkUrl());
                            }
                        }
                        Thread.sleep(pushOrder.getPushTime() * 1000);//设置等待时间
                        Integer state = orderTaxiMapper.selectById(orderTaxi.getId()).getState();
                        if(state > 1){
                            break;
                        }
                        if(i == querys.size() && state == 1){
                            orderIds.remove(orderTaxi.getId());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
 
 
 
 
}