puzhibing
2023-07-09 d9c4252c54adc1684de9b56ad810465945e442a8
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package com.stylefeng.guns.modular.taxi.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.RegionMapper;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.model.OrderPosition;
import com.stylefeng.guns.modular.system.model.Region;
import com.stylefeng.guns.modular.system.model.UserRedPacketRecord;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.*;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
@Service
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class OrderTaxiServiceImpl extends ServiceImpl<OrderTaxiMapper, OrderTaxi> implements IOrderTaxiService {
 
    @Resource
    private OrderTaxiMapper orderTaxiMapper;
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private IUserRedPacketRecordService userRedPacketRecordService;
 
    @Autowired
    private PushUtil pushUtil;
 
    @Autowired
    private GDMapElectricFenceUtil gdMapElectricFenceUtil;
 
    @Autowired
    private GDFalconUtil gdFalconUtil;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private IOrderPositionService orderPositionService;
 
    @Autowired
    private ISystemNoticeService systemNoticeService;
 
    @Autowired
    private ChinaMobileUtil chinaMobileUtil;
 
    @Resource
    private RegionMapper regionMapper;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
 
 
 
 
 
    /**
     * 获取司机端首页订单列表
     * @param state     1=服务中,2=待服务(30分钟定义预约)
     * @param driverId
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryOrderList(Integer state, Integer driverId) throws Exception {
        return orderTaxiMapper.queryOrderList(state, driverId);
    }
 
 
    /**
     * 获取司机端我的订单列表
     * @param state     1=全部,2=待支付,3=已取消
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryMyAllOrder(Integer state, Integer uid) throws Exception {
        return orderTaxiMapper.queryMyAllOrder(state, uid);
    }
 
 
    /**
     * 获取订单数据
     * @param state
     * @param driverId
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderTaxi> query(Integer driverId, Integer...state) throws Exception {
        List<Integer> integers = Arrays.asList(state);
        return orderTaxiMapper.query(integers, driverId);
    }
 
 
    /**
     * 获取司机抢单页面的订单详情
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryPushOrder(Integer orderId) throws Exception {
        return orderTaxiMapper.queryPushOrder(orderId);
    }
 
 
    /**
     * 抢单操作
     * @param orderId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public synchronized ResultUtil grabOrder(Integer orderId, Integer uid) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        //处理摆渡车的情况
        if(orderTaxi.getType() == 2){
            //查看用户下的摆渡车是否已被人抢了
            List<OrderTaxi> list = this.selectList(
                    new EntityWrapper<OrderTaxi>()
                            .eq("type", 2)
                            .eq("userId", orderTaxi.getUserId())
                            .ne("state", 1)
                            .eq("crossCityOrderId", orderTaxi.getCrossCityOrderId())
                            .eq("place", orderTaxi.getPlace())
            );
            List<OrderPrivateCar> list1 = orderPrivateCarService.selectList(
                    new EntityWrapper<OrderPrivateCar>()
                            .eq("type", 2)
                            .eq("userId", orderTaxi.getUserId())
                            .ne("state", 1)
                            .eq("crossCityOrderId", orderTaxi.getCrossCityOrderId())
                            .eq("place", orderTaxi.getPlace())
            );
            if(list.size() > 0 || list1.size() > 0){
                return ResultUtil.error("手速有点慢哦,订单已被抢啦!");
            }
        }
 
        if(orderTaxi.getState() == 9){
            return ResultUtil.error("订单已取消");
        }
        if(orderTaxi.getState() != 1){
            return ResultUtil.error("手速有点慢哦,订单已被抢啦!");
        }
        Driver driver = driverService.selectById(uid);
        orderTaxi.setDriverId(uid);
        orderTaxi.setCarId(driver.getCarId());
        orderTaxi.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : (
                driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1));
        orderTaxi.setState(2);
        orderTaxi.setSnatchOrderTime(new Date());
 
        //调用高德创建轨迹
        String s = gdFalconUtil.selectTerminal(driver.getPhone());
        String track = gdFalconUtil.createTrack(s);
        orderTaxi.setTrackId(track);
 
        //调用移动的小号接口
        Map<String, String> geocode = gdMapGeocodingUtil.geocode(orderTaxi.getStartLon().toString(), orderTaxi.getStartLat().toString());
        Region region = regionMapper.query(geocode.get("districtCode"));
        Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderTaxi.getPassengersPhone(), driver.getPhone(), Integer.valueOf(region.getCitycode().substring(1)));
        if(String.valueOf(map.get("code")).equals("200")){
            orderTaxi.setTelX(map.get("telX"));
            orderTaxi.setBindId(map.get("bindId"));
        }
 
        this.updateById(orderTaxi);
 
        //如果是预约单,则不修改司机为服务中
        if(orderTaxi.getOrderType() != 2 || (orderTaxi.getOrderType() == 2 && orderTaxi.getTravelTime().getTime() < System.currentTimeMillis() + 600000)){
            //修改司机为服务中
            driver.setState(3);
            driverService.updateById(driver);
        }
 
        //推送相关代码------------------start----------------
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderTaxi.getUserId(), orderTaxi.getId(), 2, orderTaxi.getState());
                pushUtil.pushOrderState(2, orderTaxi.getDriverId(), orderTaxi.getId(), 2, orderTaxi.getState());
                if(orderTaxi.getType() == 2){
                    pushUtil.pushFerryOrderState(1, orderTaxi.getUserId(), orderTaxi.getId(), 2, 2);
                    System.err.println("----------------------------------推送摆渡订单-----------------------------");
                }
            }
        }).start();
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushDriverPosition(orderId, 2);
            }
        }).start();
 
        systemNoticeService.addSystemNotice(2, "您已成功抢得出租车订单,请及时联系客户!", orderTaxi.getDriverId());
        systemNoticeService.addSystemNotice(1, "您的订单已指派给" + driver.getLastName().substring(0, 1) + "师傅,请保持电话畅通!", orderTaxi.getUserId());
 
        return ResultUtil.success();
    }
 
 
 
    /**
     * 抢单操作(车载端)
     * @param orderId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public synchronized ResultUtil grabOrder_(Integer orderId, Integer uid) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        if(orderTaxi.getState() == 9){
            return ResultUtil.error("订单已取消");
        }
        if(orderTaxi.getState() != 1){
            return ResultUtil.error("手速有点慢哦,订单已被抢啦!");
        }
        Driver driver = driverService.selectById(uid);
        orderTaxi.setDriverId(uid);
        orderTaxi.setCarId(driver.getCarId());
        orderTaxi.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : (
                driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1));
        orderTaxi.setState(2);
        Date date = new Date();
        orderTaxi.setSnatchOrderTime(date);
        orderTaxi.setSetOutTime(date);
        orderTaxi.setArriveTime(date);
        orderTaxi.setStartServiceTime(date);
        orderTaxi.setBoardingTime(date);
 
        String value = redisUtil.getValue("DRIVER" + uid);
        if(ToolUtil.isNotEmpty(value)){
            String[] split = value.split(",");
            Map<String, String> geocode1 = gdMapGeocodingUtil.geocode(split[0], split[1]);
            orderTaxi.setBoardingAddress(geocode1.get("address"));
            orderTaxi.setBoardingLon(Double.valueOf(split[0]));
            orderTaxi.setBoardingLat(Double.valueOf(split[1]));
        }
 
        //调用高德创建轨迹
        String s = gdFalconUtil.selectTerminal(driver.getPhone());
        String track = gdFalconUtil.createTrack(s);
        orderTaxi.setTrackId(track);
 
        //调用移动的小号接口 TODO 车载端使用真实号码
//        Map<String, String> geocode = gdMapGeocodingUtil.geocode(orderTaxi.getStartLon().toString(), orderTaxi.getStartLat().toString());
//        Region region = regionMapper.query(geocode.get("districtCode"));
//        Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderTaxi.getPassengersPhone(), driver.getPhone(), Integer.valueOf(region.getCitycode().substring(1)));
////        if(String.valueOf(map.get("code")).equals("200")){
////            orderTaxi.setTelX(map.get("telX"));
////            orderTaxi.setBindId(map.get("bindId"));
////        }
 
        this.updateById(orderTaxi);
 
        //如果是预约单,则不修改司机为服务中
        if(orderTaxi.getOrderType() != 2 || (orderTaxi.getOrderType() == 2 && orderTaxi.getTravelTime().getTime() < System.currentTimeMillis() + 600000)){
            //修改司机为服务中
            driver.setState(3);
            driverService.updateById(driver);
        }
 
        //推送相关代码------------------start----------------
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderTaxi.getUserId(), orderTaxi.getId(), 2, orderTaxi.getState());
                pushUtil.pushOrderState(2, orderTaxi.getDriverId(), orderTaxi.getId(), 2, orderTaxi.getState());
            }
        }).start();
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushDriverPosition(orderId, 2);
            }
        }).start();
 
        systemNoticeService.addSystemNotice(2, "您已成功抢得出租车订单,请及时联系客户!", orderTaxi.getDriverId());
        systemNoticeService.addSystemNotice(1, "您的订单已指派给" + driver.getLastName().substring(0, 1) + "师傅,请保持电话畅通!", orderTaxi.getUserId());
 
        return ResultUtil.success();
    }
 
 
 
 
 
    /**
     * 获取订单详情页(服务中的页面)
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryOrderInfo(Integer orderId) throws Exception {
        return orderTaxiMapper.queryOrderInfo(orderId);
    }
 
 
    /**
     * 走订单流程操作
     * @param orderId
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil process(Integer orderId, Integer state, Double lon, Double lat, String address) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        switch (state){
            case 3://出发前往预约点
                orderTaxi.setState(3);
                orderTaxi.setSetOutTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已出发,请耐心等待", orderTaxi.getUserId());
                break;
            case 4://到达预约点,等待客户上车
                orderTaxi.setState(4);
                orderTaxi.setArriveTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已到达您设置的预约地点,请及时上车", orderTaxi.getUserId());
                break;
            case 5://开始服务
                orderTaxi.setBoardingLon(lon);
                orderTaxi.setBoardingLat(lat);
                orderTaxi.setBoardingAddress(address);
                orderTaxi.setBoardingTime(new Date());
                orderTaxi.setState(5);
                orderTaxi.setStartServiceTime(new Date());
 
                pushUtil.pushDriverPosition(orderTaxi.getId(), 2);//主动推送司机定位
                break;
            case 6://结束服务
                orderTaxi.setGetoffLon(lon);
                orderTaxi.setGetoffLat(lat);
                orderTaxi.setGetoffAddress(address);
                orderTaxi.setGetoffTime(new Date());
                orderTaxi.setState(6);
                orderTaxi.setEndServiceTime(new Date());
 
                pushUtil.removeTask(orderId, 2);//删除定时任务,结束推送数据
                systemNoticeService.addSystemNotice(1, "司机已结束本次行程,谢谢使用", orderTaxi.getUserId());
                break;
        }
        this.updateById(orderTaxi);
 
        // TODO: 2020/6/5 推送状态
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderTaxi.getUserId(), orderTaxi.getId(), 2, orderTaxi.getState());
                pushUtil.pushOrderState(2, orderTaxi.getDriverId(), orderTaxi.getId(), 2, orderTaxi.getState());
            }
        }).start();
        return ResultUtil.success();
    }
 
 
    /**
     * 确认费用操作
     * @param orderId
     * @param type
     * @param travelFee
     * @param parkingFee
     * @param crossingFee
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil confirmFees(Integer orderId, Integer type, Double travelFee, Double parkingFee, Double crossingFee) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        orderTaxi.setPayManner(type);
        if(type == 2){//其他支付,不需要操作,直接完成订单
            orderTaxi.setOrderMoney(0D);
            orderTaxi.setTravelMoney(0D);
            orderTaxi.setParkMoney(0D);
            orderTaxi.setRoadTollMoney(0D);
            orderTaxi.setRedPacketMoney(0D);
            orderTaxi.setCouponMoney(0D);
            orderTaxi.setDiscount(0D);
            orderTaxi.setDiscountMoney(0D);
            orderTaxi.setPayMoney(0D);
            orderTaxi.setState(8);
        }else{
            orderTaxi = this.setMoney(orderTaxi, travelFee, parkingFee, crossingFee);
            orderTaxi.setState(7);
        }
        this.updateById(orderTaxi);
 
        //回滚司机状态为空闲
        Driver driver = driverService.selectById(orderTaxi.getDriverId());
        driver.setState(2);
        driverService.updateById(driver);
 
        OrderTaxi finalOrderTaxi = orderTaxi;
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, finalOrderTaxi.getUserId(), finalOrderTaxi.getId(), 2, finalOrderTaxi.getState());
                pushUtil.pushOrderState(2, finalOrderTaxi.getDriverId(), finalOrderTaxi.getId(), 2, finalOrderTaxi.getState());
            }
        }).start();
        return ResultUtil.success();
    }
 
 
    /**
     * 确认费用(车载端)
     * @param orderId
     * @param type
     * @param travelFee
     * @param lon
     * @param lat
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil confirmFees_(Integer orderId, Integer type, Double travelFee, Double lon, Double lat, String address) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        if(orderTaxi.getState() == 5){//服务中才能确认费用
            orderTaxi.setPayManner(type);
            orderTaxi = this.setMoney(orderTaxi, travelFee, 0D, 0D);
            orderTaxi.setState(7);
            orderTaxi.setGetoffLon(lon);
            orderTaxi.setGetoffLat(lat);
            orderTaxi.setGetoffAddress(address);
            orderTaxi.setGetoffTime(new Date());
            orderTaxi.setEndServiceTime(new Date());
 
            this.updateById(orderTaxi);
 
            pushUtil.removeTask(orderId, 2);//删除定时任务,结束推送数据
            systemNoticeService.addSystemNotice(1, "司机已结束本次行程,谢谢使用", orderTaxi.getUserId());
 
            //回滚司机状态为空闲
            Driver driver = driverService.selectById(orderTaxi.getDriverId());
            driver.setState(2);
            driverService.updateById(driver);
 
            OrderTaxi finalOrderTaxi = orderTaxi;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    pushUtil.pushOrderState(1, finalOrderTaxi.getUserId(), finalOrderTaxi.getId(), 2, finalOrderTaxi.getState());
                    pushUtil.pushOrderState(2, finalOrderTaxi.getDriverId(), finalOrderTaxi.getId(), 2, finalOrderTaxi.getState());
                }
            }).start();
            //添加定时任务6分钟司机不确认收款自动完成支付(仅车载端),6分钟之内司机无法接单
            String vehicle = redisUtil.getValue("VEHICLE");
            JSONArray jsonArray = new JSONArray();
            if(ToolUtil.isNotEmpty(vehicle)){
                jsonArray = JSON.parseArray(vehicle);
            }
            jsonArray.add(orderTaxi.getDriverId());
            redisUtil.setStrValue("VEHICLE", jsonArray.toJSONString());//添加司机不能接单标识
 
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    OrderTaxi orderTaxi1 = OrderTaxiServiceImpl.this.selectById(orderId);
                    if(orderTaxi1.getState() == 7){//如果是待支付
                        orderTaxi1.setPayManner(2);//其他方式支付
                        orderTaxi1.setRedPacketMoney(0D);
                        orderTaxi1.setCouponMoney(0D);
                        orderTaxi1.setDiscount(0D);
                        orderTaxi1.setDiscountMoney(0D);
                        orderTaxi1.setPayMoney(orderTaxi1.getOrderMoney());
                        orderTaxi1.setState(8);
                        OrderTaxiServiceImpl.this.updateById(orderTaxi1);
 
                        String vehicle = redisUtil.getValue("VEHICLE");
                        if(ToolUtil.isNotEmpty(vehicle)){
                            JSONArray jsonArray = JSON.parseArray(vehicle);
                            for(int i = 0; i < jsonArray.size(); i++){
                                if(jsonArray.getInteger(i).compareTo(orderTaxi1.getDriverId()) == 0){
                                    jsonArray.remove(i);
                                    break;
                                }
                            }
                            redisUtil.setStrValue("VEHICLE", jsonArray.toJSONString());
                        }
 
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                pushUtil.pushOrderState(1, orderTaxi1.getUserId(), orderId, 2, 8);
                                pushUtil.pushOrderState(2, orderTaxi1.getDriverId(), orderId, 2, 8);
                            }
                        }).start();
                    }
                }
            };
            Timer timer = new Timer();
            timer.schedule(timerTask, 6 * 60 * 1000);
        }
        return ResultUtil.success();
    }
 
 
    /**
     * 司机确认费用(车载端)不管之前数据状态直接修改到待支付(流程断网情况的处理流程)
     * @param orderId
     * @param type
     * @param travelFee
     * @param lon
     * @param lat
     * @param address
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil confirmFees$(Integer orderId, Integer type, Double travelFee, Double lon, Double lat, String address) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        orderTaxi.setPayManner(type);
        orderTaxi = this.setMoney(orderTaxi, travelFee, 0D, 0D);
        orderTaxi.setState(7);
        orderTaxi.setGetoffLon(lon);
        orderTaxi.setGetoffLat(lat);
        orderTaxi.setGetoffAddress(address);
        orderTaxi.setGetoffTime(new Date());
        orderTaxi.setEndServiceTime(new Date());
 
        this.updateById(orderTaxi);
 
        pushUtil.removeTask(orderId, 2);//删除定时任务,结束推送数据
        systemNoticeService.addSystemNotice(1, "司机已结束本次行程,谢谢使用", orderTaxi.getUserId());
 
        //回滚司机状态为空闲
        Driver driver = driverService.selectById(orderTaxi.getDriverId());
        driver.setState(2);
        driverService.updateById(driver);
 
        OrderTaxi finalOrderTaxi = orderTaxi;
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, finalOrderTaxi.getUserId(), finalOrderTaxi.getId(), 2, finalOrderTaxi.getState());
                pushUtil.pushOrderState(2, finalOrderTaxi.getDriverId(), finalOrderTaxi.getId(), 2, finalOrderTaxi.getState());
            }
        }).start();
 
        //添加定时任务6分钟司机不确认收款自动完成支付(仅车载端),6分钟之内司机无法接单
        String vehicle = redisUtil.getValue("VEHICLE");
        JSONArray jsonArray = new JSONArray();
        if(ToolUtil.isNotEmpty(vehicle)){
            jsonArray = JSON.parseArray(vehicle);
        }
        jsonArray.add(orderTaxi.getDriverId());
        redisUtil.setStrValue("VEHICLE", jsonArray.toJSONString());//添加司机不能接单标识
 
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                OrderTaxi orderTaxi1 = OrderTaxiServiceImpl.this.selectById(orderId);
                if(orderTaxi1.getState() == 7){//如果是待支付
                    orderTaxi1.setPayManner(2);//其他方式支付
                    orderTaxi1.setRedPacketMoney(0D);
                    orderTaxi1.setCouponMoney(0D);
                    orderTaxi1.setDiscount(0D);
                    orderTaxi1.setDiscountMoney(0D);
                    orderTaxi1.setPayMoney(orderTaxi1.getOrderMoney());
                    orderTaxi1.setState(8);
                    OrderTaxiServiceImpl.this.updateById(orderTaxi1);
 
                    String vehicle = redisUtil.getValue("VEHICLE");
                    if(ToolUtil.isNotEmpty(vehicle)){
                        JSONArray jsonArray = JSON.parseArray(vehicle);
                        for(int i = 0; i < jsonArray.size(); i++){
                            if(jsonArray.getInteger(i).compareTo(orderTaxi1.getDriverId()) == 0){
                                jsonArray.remove(i);
                                break;
                            }
                        }
                        redisUtil.setStrValue("VEHICLE", jsonArray.toJSONString());
                    }
 
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            pushUtil.pushOrderState(1, orderTaxi1.getUserId(), orderId, 2, 8);
                            pushUtil.pushOrderState(2, orderTaxi1.getDriverId(), orderId, 2, 8);
                        }
                    }).start();
                }
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, 6 * 60 * 1000);
        return ResultUtil.success();
    }
 
 
    /**
     * 计算已服务的实时里程
     * @param orderId
     * @param lon
     * @param lat
     */
    @Override
    public boolean calculateMileage(Integer orderId, String lon, String lat) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        if(null == orderTaxi){
            System.err.println("订单数据异常:" + orderId);
            return false;
        }
        OrderPosition orderPosition = orderPositionService.queryNew(orderId, 2);
        String now = lon + "," + lat;
        String old = null;
        if(null != orderPosition){
            old = orderPosition.getLon() + "," + orderPosition.getLat();
        }else{
            orderTaxi.setMileage(0D);
            this.updateById(orderTaxi);
            return true;//第一条数据不作处理,直接存储
        }
        Map<String, String> distance = gdMapElectricFenceUtil.getDistance(now, old, 0);//直线距离
        if(null != distance){
            String distance1 = distance.get("distance");
            if(Double.valueOf(distance1) > 50){//大于50米表示在移动
                orderTaxi.setMileage(new BigDecimal(orderTaxi.getMileage()).add(new BigDecimal(distance1)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                this.updateById(orderTaxi);
                return true;
            }
            return false;
        }else{
            System.err.println("调用高德计算距离出错");
        }
        return false;
    }
 
 
    /**
     * 获取所有快到期的预约单(出行时间在30分钟内)
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderTaxi> queryMaturity() throws Exception {
        return orderTaxiMapper.queryMaturity();
    }
 
 
    /**
     * 获取订单完成后30分钟的数据
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderTaxi> taskMidAxbUnBindSend() throws Exception {
        return orderTaxiMapper.taskMidAxbUnBindSend();
    }
 
 
    /**
     * 计算费用
     * @param orderTaxi
     * @param travelFee
     * @param parkingFee
     * @param crossingFee
     * @return
     */
    public OrderTaxi setMoney(OrderTaxi orderTaxi, Double travelFee, Double parkingFee, Double crossingFee) throws Exception{
        orderTaxi.setTravelMoney(travelFee);
        orderTaxi.setParkMoney(parkingFee);
        orderTaxi.setRoadTollMoney(crossingFee);
        BigDecimal sum = new BigDecimal(travelFee).add(new BigDecimal(parkingFee)).add(new BigDecimal(crossingFee)).add(new BigDecimal(orderTaxi.getTipMoney()));
        orderTaxi.setOrderMoney(sum.setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        return orderTaxi;
    }
}