puzhibing
2023-05-18 53562814add61acfdc02d6b25dae6324f6fd5f92
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.DriverWorkMapper;
import com.stylefeng.guns.modular.system.dao.PushAuxiliaryMapper;
import com.stylefeng.guns.modular.system.dao.PushOrderMapper;
import com.stylefeng.guns.modular.system.model.DriverWork;
import com.stylefeng.guns.modular.system.model.OrderPosition;
import com.stylefeng.guns.modular.system.model.PushAuxiliary;
import com.stylefeng.guns.modular.system.model.PushOrder;
import com.stylefeng.guns.modular.system.service.IDriverOrdersService;
import com.stylefeng.guns.modular.system.service.INettyService;
import com.stylefeng.guns.modular.system.service.IOrderPositionService;
import com.stylefeng.guns.modular.system.util.GDMapElectricFenceUtil;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.warpper.OrderStatusWarpper;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
 
 
@Service
public class NettyServiceImpl implements INettyService {
 
    @Resource
    private DriverWorkMapper driverWorkMapper;
 
    @Resource
    private PushOrderMapper pushOrderMapper;
 
    @Resource
    private PushAuxiliaryMapper pushAuxiliaryMapper;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private IOrderTaxiService orderTaxiService;
 
    @Autowired
    private GDMapElectricFenceUtil gdMapElectricFenceUtil;
 
    @Autowired
    private IOrderPositionService orderPositionService;
 
    @Autowired
    private IDriverOrdersService driverOrdersService;
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
    @Autowired
    private IOrderLogisticsService orderLogisticsService;
 
 
 
 
 
 
 
    /**
     * 司机主动上传位置信息
     * @param uid   司机id
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil positionSocket(OrderPosition orderPosition, Integer uid) throws Exception {
        redisUtil.setStrValue(String.valueOf(uid), orderPosition.getLon() + "," + orderPosition.getLat());
        if(ToolUtil.isNotEmpty(orderPosition.getOrderId()) && ToolUtil.isNotEmpty(orderPosition.getOrderType())){
            orderPositionService.saveData(orderPosition);
        }
        return ResultUtil.success();
    }
 
 
    /**
     * 进入业务流程中,根据订单的不同状态返回不同的数据
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil<List<OrderStatusWarpper>> orderStateSocket(Integer uid) throws Exception {
        List<OrderStatusWarpper> list = new ArrayList<>();
        List<OrderTaxi> query = orderTaxiService.query(uid, 8, 10);//已支付和已取消
        for(OrderTaxi o : query){
            OrderStatusWarpper orderStatusWarpper = new OrderStatusWarpper();
            orderStatusWarpper.setOrderId(o.getId());
            orderStatusWarpper.setOrderType(2);
            orderStatusWarpper.setState(o.getState());
            list.add(orderStatusWarpper);
        }
 
        //查看是否有可接单的数据
        String value = redisUtil.getValue("DRIVER" + String.valueOf(uid));
        if(ToolUtil.isNotEmpty(value)){
            String[] split = value.split(",");
            List<OrderStatusWarpper> list1 = this.searchOrder(split[0], split[1], uid);
            list.addAll(list1);
        }
 
        return ResultUtil.success(list);
    }
 
 
 
    /**
     * 搜寻是否有订单可服务(需要抢单的业务)
     * @param uid
     * @return
     * @throws Exception
     */
    public List<OrderStatusWarpper> searchOrder(String lon, String lat, Integer uid) throws Exception{
        List<OrderStatusWarpper> list = new ArrayList<>();
        String driver = lon + "," + lat;
        //获取需要接单的订单
        //获取司机的上班业务类型
        DriverWork driverWork = driverWorkMapper.queryNewWork(uid, null, 1);
        if(null == driverWork){//还未上班
            return list;
        }
        String[] split = driverWork.getType().split(",");
        for(String type : split){
            switch (Integer.valueOf(type)){
                case 1://专车
                    List<OrderPrivateCar> query = orderPrivateCarService.query(null, 1);//待接订单
                    for(OrderPrivateCar orderPrivateCar : query){
                        String order = orderPrivateCar.getStartLon() + "," + orderPrivateCar.getStartLat();
                        boolean b = this.judgePush(driver, order, orderPrivateCar.getId(), 1, orderPrivateCar.getCompanyId(), uid);//判断能否继续推单
                        if(b){
                            OrderStatusWarpper orderStatusWarpper = new OrderStatusWarpper();
                            orderStatusWarpper.setOrderId(orderPrivateCar.getId());
                            orderStatusWarpper.setOrderType(1);
                            orderStatusWarpper.setState(orderPrivateCar.getState());
                            list.add(orderStatusWarpper);
                        }
                    }
                    break;
                case 2://出租车
                    List<OrderTaxi> query1 = orderTaxiService.query(null, 1);//待接订单
                    for(OrderTaxi orderTaxi : query1){
                        String order = orderTaxi.getStartLon() + "," + orderTaxi.getStartLat();
                        boolean b = this.judgePush(driver, order, orderTaxi.getId(), 2, orderTaxi.getCompanyId(), uid);//判断能否继续推单
                        if(b){
                            OrderStatusWarpper orderStatusWarpper = new OrderStatusWarpper();
                            orderStatusWarpper.setOrderId(orderTaxi.getId());
                            orderStatusWarpper.setOrderType(2);
                            orderStatusWarpper.setState(orderTaxi.getState());
                            list.add(orderStatusWarpper);
                        }
 
                    }
                    break;
                case 3://城际
                    break;
                case 4://市内小件物流
                    List<OrderLogistics> query2 = orderLogisticsService.query(null, 1);//待接订单
                    for(OrderLogistics orderLogistics : query2){
                        String order = orderLogistics.getStartLon() + "," + orderLogistics.getStartLat();
                        boolean b = this.judgePush(driver, order, orderLogistics.getId(), 4, orderLogistics.getCompanyId(), uid);//判断能否继续推单
                        if(b){
                            OrderStatusWarpper orderStatusWarpper = new OrderStatusWarpper();
                            orderStatusWarpper.setOrderId(orderLogistics.getId());
                            orderStatusWarpper.setOrderType(4);
                            orderStatusWarpper.setState(orderLogistics.getState());
                            list.add(orderStatusWarpper);
                        }
                    }
                    break;
                case 5://跨城小件物流
                    List<OrderLogistics> query3 = orderLogisticsService.query(null, 1);//待接订单
                    for(OrderLogistics orderLogistics : query3){
                        String order = orderLogistics.getStartLon() + "," + orderLogistics.getStartLat();
                        boolean b = this.judgePush(driver, order, orderLogistics.getId(), 5, orderLogistics.getCompanyId(), uid);//判断能否继续推单
                        if(b){
                            OrderStatusWarpper orderStatusWarpper = new OrderStatusWarpper();
                            orderStatusWarpper.setOrderId(orderLogistics.getId());
                            orderStatusWarpper.setOrderType(5);
                            orderStatusWarpper.setState(orderLogistics.getState());
                            list.add(orderStatusWarpper);
                        }
                    }
                    break;
            }
        }
        return list;
    }
 
 
    /**
     * 判断订单能否进入推单通道中
     * @param orderId
     * @param orderType
     * @param companyId
     * @return
     */
    public boolean judgePush(String lonlat1, String lonlat2, Integer orderId, Integer orderType, Integer companyId, Integer driverId) throws Exception{
        List<Integer> integers = driverOrdersService.queryOrders(driverId);//获取可接单的设置
        if(integers.contains(orderType)){
            PushAuxiliary query = pushAuxiliaryMapper.query(orderId, orderType, null);
            List<PushOrder> querys = pushOrderMapper.querys(null, orderType, companyId);
            if(null != query){//如果是推过单
                Integer num = query.getNum();
                if(num == querys.size()){//推送完了
                    return false;
                }
                //开始判断时间是否到达推送条件
                long time = query.getInsertTime().getTime();
                PushOrder pushOrder = pushOrderMapper.querys(num, orderType, companyId).get(0);
                if(System.currentTimeMillis() - time <= pushOrder.getPushTime() * 1000){
                    return false;
                }
 
                //开始判断距离是否符合
                pushOrder = pushOrderMapper.querys(num + 1, orderType, companyId).get(0);
                Map<String, String> distance = gdMapElectricFenceUtil.getDistance(lonlat1, lonlat2, 0);
                if(null != distance) {
                    String distance1 = distance.get("distance");
                    if(Double.valueOf(distance1).compareTo(pushOrder.getPushDistance()) > 0){//超出范围
                        return false;
                    }
                }else{
                    System.err.println("调用高德计算距离出错啦!");
                }
                return true;
            }else{
                //开始判断距离是否符合
                List<PushOrder> querys1 = pushOrderMapper.querys(1, orderType, companyId);
                if(querys1.size() > 0){
                    PushOrder pushOrder = querys1.get(0);
                    Map<String, String> distance = gdMapElectricFenceUtil.getDistance(lonlat1, lonlat2, 0);
                    if(null != distance) {
                        String distance1 = distance.get("distance");
                        if(Double.valueOf(distance1).compareTo(pushOrder.getPushDistance()) > 0){//超出范围
                            return false;
                        }
                    }else{
                        System.err.println("调用高德计算距离出错啦!");
                    }
                    return true;
                }
                return false;
            }
        }else{
            return false;
        }
 
    }
}