zhibing.pu
2024-07-25 b3b7d3ae4789eb3d4d4bbadccd03d79adb28cb1a
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package com.stylefeng.guns.modular.system.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
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.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
 
/**
 * socket推单处理类
 */
@Component
public class PushUtil {
 
    @Autowired
    private RestTemplate internalRestTemplate;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private IOrderTaxiService orderTaxiService;
 
    @Autowired
    private GDMapElectricFenceUtil gdMapElectricFenceUtil;
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
    @Autowired
    private IOrderCrossCityService orderCrossCityService;
 
    private Map<String, JSONObject> pushMap = new HashMap<>();//存储需要定时推送的数据
 
    private Map<String, Timer> taskMap = new HashMap<>();//存储定时推送的定时器
 
 
 
 
    /**
     * 推送订单状态
     * @param type          1=用户,2=司机
     * @param uid           对象id
     * @param orderId       订单id
     * @param orderType     订单类型(1=专车,2=出租车,3=城际,4=小件物流-同城,5=小件物流-跨城,6=包车)
     * @param state         订单状态(1=待接单,2=待出发,3=待到达预约地点,4=待乘客上车,5=服务中,6=完成服务,7=待支付,8=待评价,9=已完成,10=已取消,11=改派中,12=取消待支付)
     */
    public void pushOrderState(Integer type, Integer uid, Integer orderId, Integer orderType, Integer state, Integer time, String audioUrl){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "SUCCESS");
        jsonObject.put("method", "ORDER_STATUS");
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("orderType", orderType);
        map.put("state", state);
        map.put("time", time);
        map.put("audioUrl", audioUrl);
        jsonObject.put("data", map);
 
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
    
 
 
 
 
 
    /**
     * 推单完成后,没有司机接单的推送提醒
     * @param type
     * @param uid
     * @param orderId
     * @param orderType
     */
    public void pushEndPush(Integer type, Integer uid, Integer orderId, Integer orderType){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "SUCCESS");
        jsonObject.put("method", "END_PUSH");
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("orderType", orderType);
        jsonObject.put("data", map);
 
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
    
    
    /**
     * 司机超时提醒
     * @param type
     * @param uid
     * @param orderId
     * @param orderType
     */
    public void pushDriverTimeOut(Integer type, Integer uid, Integer orderId, Integer orderType){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "SUCCESS");
        jsonObject.put("method", "DRIVER_TIME_OUT");
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("orderType", orderType);
        jsonObject.put("data", map);
        
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
    
    
    /**
     * 修改目的地推送通知
     * @param type
     * @param uid
     * @param orderId
     * @param orderType
     * @param status 1=申请,2=同意,3=拒绝
     */
    public void pushModifyAddress(Integer type, Integer uid, Integer orderId, Integer orderType, Integer status, String audioUrl){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "SUCCESS");
        jsonObject.put("method", "MODIFY_ADDRESS");
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("orderType", orderType);
        map.put("status", status);
        map.put("audioUrl", audioUrl);
        jsonObject.put("data", map);
        
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
 
 
    /**
     * 推送司机位置给乘客端
     */
    public void pushDriverPosition(Integer orderId, Integer orderType){
        Integer userId = null;
        switch (orderType){
            case 1:
                OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(orderId);
                userId = orderPrivateCar.getUserId();
                break;
            case 2:
                OrderTaxi orderTaxi = orderTaxiService.selectById(orderId);
                userId = orderTaxi.getUserId();
                break;
            case 3:
                OrderCrossCity orderCrossCity = orderCrossCityService.selectById(orderId);
                userId = orderCrossCity.getUserId();
                break;
        }
        JSONObject data = new JSONObject();
        data.put("id", userId);
        data.put("type", 1);
        this.pushMap.put(orderId + "_" + orderType, data);
        this.createTask(orderId, orderType);
    }
 
 
    /**
     * 创建定时任务
     * @param orderId
     */
    public void createTask(Integer orderId, Integer orderType){
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                try {
                    PushUtil.this.pushPositon(orderId, orderType);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 1000, 10000);//1秒后开始10秒钟一次的重复执行
        taskMap.put(orderId + "_" + orderType, timer);
    }
 
 
    /**
     * 推送处理方法
     * @param orderId
     * @param orderType
     * @throws Exception
     */
    public void pushPositon(Integer orderId, Integer orderType) throws Exception{
        Integer driverId = null;
        String startLonLat = null;
        String endLonLat = null;
        Integer state = 0;
        Integer oldState = 0;
        Long startServiceTime = null;
        Double servedMileage = null;
        switch (orderType){
            case 1:
                OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(orderId);
                driverId = orderPrivateCar.getDriverId();
                startLonLat = orderPrivateCar.getStartLon() + "," + orderPrivateCar.getStartLat();
                state = orderPrivateCar.getState();
                oldState = orderPrivateCar.getOldState();
                startServiceTime = null != orderPrivateCar.getStartServiceTime() ? orderPrivateCar.getStartServiceTime().getTime() : null;
                servedMileage = orderPrivateCar.getMileage();
                endLonLat = orderPrivateCar.getEndLon() + "," + orderPrivateCar.getEndLat();
                break;
            case 2:
                OrderTaxi orderTaxi = orderTaxiService.selectById(orderId);
                driverId = orderTaxi.getDriverId();
                startLonLat = orderTaxi.getStartLon() + "," + orderTaxi.getStartLat();
                state = orderTaxi.getState();
                oldState = orderTaxi.getOldState();
                startServiceTime = null != orderTaxi.getStartServiceTime() ? orderTaxi.getStartServiceTime().getTime() : null;
                servedMileage = orderTaxi.getMileage();
                endLonLat = orderTaxi.getEndLon() + "," + orderTaxi.getEndLat();
                break;
            case 3:
                OrderCrossCity orderCrossCity = orderCrossCityService.selectById(orderId);
                driverId = orderCrossCity.getDriverId();
                startLonLat = orderCrossCity.getStartLon() + "," + orderCrossCity.getStartLat();
                state = orderCrossCity.getState();
                oldState = orderCrossCity.getOldState();
                startServiceTime = null != orderCrossCity.getStartServiceTime() ? orderCrossCity.getStartServiceTime().getTime() : null;
                servedMileage = orderCrossCity.getMileage();
                endLonLat = orderCrossCity.getEndLon() + "," + orderCrossCity.getEndLat();
                break;
        }
        //计算预计距离和剩余时间
        String value = redisUtil.getValue("DRIVER" + String.valueOf(driverId));
        if(null == value || "".equals(value)){
            System.err.println("司机没有上传位置信息");
        }
        Map<String, String> distance = gdMapElectricFenceUtil.getDistance(value, startLonLat, 1);
        String d = "0";
        String t = "0";
        if(null == distance){
            System.err.println("查询距离出错了");
        }else{
            d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000), new MathContext(2, RoundingMode.HALF_EVEN)).toString();
            t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60), new MathContext(2, RoundingMode.HALF_EVEN)).intValue() + "";
        }
        JSONObject msg = new JSONObject();
        msg.put("code", 200);
        msg.put("msg", "SUCCESS");
        msg.put("method", "DRIVER_POSITION");
        Map<String, String> map = new HashMap<>();
        map.put("orderId", String.valueOf(orderId));
        map.put("orderType", String.valueOf(orderType));
        map.put("lon", (null != value ? value.split(",")[0] : ""));
        map.put("lat", (null != value ? value.split(",")[1] : ""));
        if(state == 7 || state == 8 || state == 9 || state == 10 || state == 12){//删除定时任务
            this.removeTask(orderId, orderType);
            return;
        }
        if((state == 2 || state == 3 || state == 4) || (oldState != null && (oldState == 2 || oldState == 3 || oldState == 4))){//前往预约地
            map.put("reservationMileage", d);//当前位置距离预约点的剩余里程
            map.put("reservationTime", t);//当前位置距离预约点的剩余分钟
            map.put("servedMileage", "0");//距离起点已经服务的里程
            map.put("servedTime", "0");//距离起点已经服务的时间
            map.put("laveMileage", "0");//距离终点剩余未服务的里程数
            map.put("laveTime", "0");//距离终端剩余未服务的预计时间
        }
        if((state == 5 || state == 6) || (oldState != null && (oldState == 5 || oldState == 6))){//服务中
            map.put("reservationMileage", "0");//当前位置距离预约点的剩余里程
            map.put("reservationTime", "0");//当前位置距离预约点的剩余分钟
            map.put("servedMileage", String.valueOf(servedMileage / 1000));//距离起点已经服务的里程
            Integer servedTime = Long.valueOf((new Date().getTime() - startServiceTime) / 60000).intValue();
            map.put("servedTime", servedTime + "");//距离起点已经服务的时间
            distance = gdMapElectricFenceUtil.getDistance(value, endLonLat, 1);
            if(null == distance){
                System.err.println("查询距离出错了");
            }else{
                d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000), new MathContext(2, RoundingMode.HALF_EVEN)).toString();
                t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60), new MathContext(2, RoundingMode.HALF_EVEN)).intValue() + "";
            }
            map.put("laveMileage", d);//距离终点剩余未服务的里程数
            map.put("laveTime", t);//距离终端剩余未服务的预计时间
        }
 
        msg.put("data", map);
 
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        JSONObject jsonObject = pushMap.get(orderId + "_" + orderType);
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", msg.toJSONString());
        params.add("id", jsonObject.getString("id"));
        params.add("type", jsonObject.getString("type"));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
 
 
    /**
     * 推送强迫下线
     * @param id
     * @param type
     */
    public void pushOffline(Integer id, Integer type){
        JSONObject msg = new JSONObject();
        msg.put("code", 200);
        msg.put("msg", "SUCCESS");
        msg.put("method", "OFFLINE");
        msg.put("data", new Object());
 
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", msg.toJSONString());
        params.add("id", id.toString());
        params.add("type", type.toString());
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
 
 
 
 
    /**
     * 摆渡抢单成功后推单
     * @param type
     * @param uid
     * @param orderId
     * @param orderType
     * @param state
     */
    public void pushFerryOrderState(Integer type, Integer uid, Integer orderId, Integer orderType, Integer state){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "SUCCESS");
        jsonObject.put("method", "FERRY");
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("orderType", orderType);
        map.put("state", state);
        jsonObject.put("data", map);
 
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
 
 
 
    /**
     * 删除定时任务
     * @param orderId
     */
    public void removeTask(Integer orderId, Integer orderType){
        Timer timer = taskMap.get(orderId + "_" + orderType);
        if (null != timer){
            timer.cancel();
        }
    }
 
 
    /**
     * 线下支付数据推送
     * @param type
     * @param uid
     * @param orderId
     * @param orderType
     */
    public void pushOfflinePayment(Integer type, Integer uid, Integer orderId, Integer orderType){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "SUCCESS");
        jsonObject.put("method", "OFFLINE_PAYMENT");
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("orderType", orderType);
        jsonObject.put("data", map);
 
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
    
    
    
    /**
     * 小件物流差价支付的推送
     * @param type
     * @param uid
     * @param orderId
     * @param orderType
     * @param money
     * @param status 1=申请,2=同意,3=拒绝
     */
    public void pushPayDifference(Integer type, Integer uid, Integer orderId, Integer orderType, Double money, Integer status, String audioUrl){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "SUCCESS");
        jsonObject.put("method", "DIFFERENCE");
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        map.put("orderType", orderType);
        map.put("money", money);
        map.put("status", status);
        map.put("audioUrl", audioUrl);
        jsonObject.put("data", map);
        
        //调用推送
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("msg", jsonObject.toJSONString());
        params.add("id", String.valueOf(uid));
        params.add("type", String.valueOf(type));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
        String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class);
        JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class);
        if(jsonObject1.getIntValue("code") != 200){
            System.err.println(jsonObject1.getString("msg"));
        }
    }
}