无关风月
2025-03-19 12293a8893a129357fa0603550604df225bc2784
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
package com.stylefeng.guns.modular.specialTrain.server.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.specialTrain.dao.OrderPrivateCarMapper;
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.dao.SystemPriceMapper;
import com.stylefeng.guns.modular.system.dao.UserInfoMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.*;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
 
 
@Service
public class OrderPrivateCarServiceImpl extends ServiceImpl<OrderPrivateCarMapper, OrderPrivateCar> implements IOrderPrivateCarService {
 
    @Resource
    private OrderPrivateCarMapper orderPrivateCarMapper;
 
    @Resource
    private SystemPriceMapper systemPriceMapper;
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private PushUtil pushUtil;
 
    @Autowired
    private GDFalconUtil gdFalconUtil;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
//    @Autowired
//    private ChinaMobileUtil chinaMobileUtil;
 
    @Resource
    private RegionMapper regionMapper;
 
    @Autowired
    private ISystemNoticeService systemNoticeService;
 
    @Autowired
    private IOrderPositionService orderPositionService;
 
    @Autowired
    private GDMapElectricFenceUtil gdMapElectricFenceUtil;
 
    @Autowired
    private PushMinistryOfTransportUtil pushMinistryOfTransportUtil;
 
    @Autowired
    private IOrderTaxiService orderTaxiService;
 
    @Value("${pushMinistryOfTransport}")
    private boolean pushMinistryOfTransport;
 
 
    @Resource
    private UserInfoMapper userInfoMapper;
 
    @Autowired
    private ICompanyService companyService;
 
    @Autowired
    private IIncomeService incomeService;
 
 
    @Value("${filePath}")
    private String filePath;
 
 
 
 
 
    /**
     * 获取司机端首页订单列表
     * @param state     1=服务中,2=待服务(30分钟定义预约)
     * @param driverId
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryOrderList(Integer state, Integer driverId) throws Exception {
        return orderPrivateCarMapper.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 orderPrivateCarMapper.queryMyAllOrder(state, uid);
    }
 
 
    /**
     * 获取司机抢单页面的订单详情
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryPushOrder(Integer orderId) throws Exception {
        return orderPrivateCarMapper.queryPushOrder(orderId);
    }
 
 
 
    /**
     * 抢单操作
     * @param orderId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public synchronized ResultUtil grabOrder(Integer orderId, Integer uid) throws Exception {
        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
        //处理摆渡车的情况
        if(orderPrivateCar.getType() == 2){
            //查看用户下的摆渡车是否已被人抢了
            List<OrderPrivateCar> list = this.selectList(
                    new EntityWrapper<OrderPrivateCar>()
                            .eq("type", 2)
                            .eq("userId", orderPrivateCar.getUserId())
                            .ne("state", 1)
                            .eq("crossCityOrderId", orderPrivateCar.getCrossCityOrderId())
                            .eq("place", orderPrivateCar.getPlace())
            );
            List<OrderTaxi> list1 = orderTaxiService.selectList(
                    new EntityWrapper<OrderTaxi>()
                            .eq("type", 2)
                            .eq("userId", orderPrivateCar.getUserId())
                            .ne("state", 1)
                            .eq("crossCityOrderId", orderPrivateCar.getCrossCityOrderId())
                            .eq("place", orderPrivateCar.getPlace())
            );
            if(list.size() > 0 || list1.size() > 0){
                return ResultUtil.error("手速有点慢哦,订单已被抢啦!");
            }
        }
 
        if(orderPrivateCar.getState() == 9){
            return ResultUtil.error("订单已取消");
        }
        if(orderPrivateCar.getState() != 1){
            return ResultUtil.error("手速有点慢哦,订单已被抢啦!");
        }
        Driver driver = driverService.selectById(uid);
        orderPrivateCar.setDriverId(uid);
        orderPrivateCar.setCarId(driver.getCarId());
        orderPrivateCar.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : (
                driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1));
        orderPrivateCar.setState(2);
        orderPrivateCar.setSnatchOrderTime(new Date());
 
        //调用高德创建轨迹
//        String s = gdFalconUtil.selectTerminal(driver.getPhone());
//        String track = gdFalconUtil.createTrack(s);
//        JSONObject jsonObject = JSONObject.parseObject(track);
//        orderPrivateCar.setTrackId(String.valueOf(jsonObject.getInteger("trid")));
 
        //调用移动的小号接口
        /*Map<String, String> geocode = gdMapGeocodingUtil.geocode(orderPrivateCar.getStartLon().toString(), orderPrivateCar.getStartLat().toString());
        Region region = regionMapper.query(geocode.get("districtCode"));*/
//        Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderPrivateCar.getPassengersPhone(), driver.getPhone(), null);
//        if(String.valueOf(map.get("code")).equals("200")){
//            orderPrivateCar.setTelX(map.get("telX"));
//            orderPrivateCar.setBindId(map.get("bindId"));
//        }
//        System.out.println(map);
        this.updateById(orderPrivateCar);
 
        //如果是预约单,则不修改司机为服务中
        if(orderPrivateCar.getOrderType() != 2 || (orderPrivateCar.getOrderType() == 2 && orderPrivateCar.getTravelTime().getTime() < System.currentTimeMillis() + 600000)){
            //修改司机为服务中
            driver.setState(3);
            driverService.updateById(driver);
        }
 
        //推送相关代码------------------start----------------
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState());
                pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState());
                if(orderPrivateCar.getType() == 2){
                    pushUtil.pushFerryOrderState(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1, 2);
                }
            }
        }).start();
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushDriverPosition(orderId, 1);
            }
        }).start();
 
        systemNoticeService.addSystemNotice(2, "您已成功抢得专车订单,请及时联系客户!", orderPrivateCar.getDriverId());
        systemNoticeService.addSystemNotice(1, "您的订单已指派给" + driver.getName().substring(0, 1) + "师傅,请保持电话畅通!", orderPrivateCar.getUserId());
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(pushMinistryOfTransport){
                    //上传数据
                    try {
                        pushMinistryOfTransportUtil.orderCreate(orderId);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        pushMinistryOfTransportUtil.orderMatch(orderId);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
 
        return ResultUtil.success();
    }
 
 
 
    /**
     * 获取订单详情页(服务中的页面)
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryOrderInfo(Integer orderId) throws Exception {
        return orderPrivateCarMapper.queryOrderInfo(orderId);
    }
 
 
 
    /**
     * 走订单流程操作
     * @param orderId
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil process(Integer orderId, Integer state, Double lon, Double lat, String address,String phone) throws Exception {
        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
        if(state==5){
            UserInfo userInfo = userInfoMapper.selectById(orderPrivateCar.getUserId());
            if(!userInfo.getPhone().substring(7, userInfo.getPhone().length()).equals(phone)){
                return ResultUtil.error("手机号错误");
            }
        }
        if(state==3 && orderPrivateCar.getState()!=2){
            return ResultUtil.error("当前订单不能触发前往预约地点");
        }
        if(state==4 && orderPrivateCar.getState()!=3){
            return ResultUtil.error("当前订单不能到达预约地点");
        }
        /*if(state==5 && orderPrivateCar.getState()!=4){
            return ResultUtil.error("当前订单不能开始服务");
        }*/
        switch (state){
            case 3://出发前往预约点
                orderPrivateCar.setState(3);
                orderPrivateCar.setSetOutTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已出发,请耐心等待", orderPrivateCar.getUserId());
                break;
            case 4://到达预约点,等待客户上车
                orderPrivateCar.setState(4);
                orderPrivateCar.setArriveTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已到达您设置的预约地点,请及时上车", orderPrivateCar.getUserId());
                break;
            case 5://开始服务
                orderPrivateCar.setBoardingLon(lon);
                orderPrivateCar.setBoardingLat(lat);
                orderPrivateCar.setBoardingAddress(address);
                orderPrivateCar.setBoardingTime(new Date());
                orderPrivateCar.setState(5);
                orderPrivateCar.setStartServiceTime(new Date());
 
                pushUtil.pushDriverPosition(orderPrivateCar.getId(), 1);//主动推送司机定位
 
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(pushMinistryOfTransport){//上传数据
                            pushMinistryOfTransportUtil.operateDepart(orderId);
                        }
                    }
                }).start();
                break;
            case 6://结束服务(专车可以返回继续服务)不修改状态
                orderPrivateCar.setGetoffLon(lon);
                orderPrivateCar.setGetoffLat(lat);
                orderPrivateCar.setGetoffAddress(address);
                orderPrivateCar.setGetoffTime(new Date());
                orderPrivateCar.setEndServiceTime(new Date());
 
                // 结束服务时查询轨迹,计算距离
                Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
                double sum = 0;
                try {
                    String tid = gdFalconUtil.selectTerminal(driver.getPhone());
                    String result = gdFalconUtil.selectTrack(tid, orderPrivateCar.getTrackId(),1);
    
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    JSONArray tracks = jsonObject.getJSONArray("tracks");
                    Integer counts = jsonObject.getInteger("counts");
                    JSONArray points = new JSONArray();
                    // 如果一单轨迹点数大于999,需进行分页查询,将所有的点查询出
                    if(counts > 999){
                        int i = counts%999;
                        for (int j = 1; j < i + 1; j++) {
                            String res = gdFalconUtil.selectTrack(tid, orderPrivateCar.getTrackId(),1);
                            JSONObject jsonObject1 = JSONObject.parseObject(res);
                            JSONArray tracks1 = jsonObject1.getJSONArray("tracks");
                            Object ob = tracks1.get(0);
                            String obs = JSONObject.toJSONString(ob);
                            JSONObject object = JSONObject.parseObject(obs);
                            JSONArray point = object.getJSONArray("points");
                            points.addAll(point);
                        }
                    }
                    // 轨迹点数小于999,一页查询出即可
                    if(counts > 0 && counts < 999){
                        Object ob = tracks.get(0);
                        String obs = JSONObject.toJSONString(ob);
                        JSONObject object = JSONObject.parseObject(obs);
                        JSONArray point = object.getJSONArray("points");
                        points.addAll(point);
                    }
    
                    String toLonLat = "";
                    List<OrderPosition> orderPositions = new ArrayList<>();
                    for (Object o : points) {
                        String s = JSONObject.toJSONString(o);
                        JSONObject obj = JSONObject.parseObject(s);
                        String fromLonLat = obj.getString("location");
                        String[] split = fromLonLat.split(",");
                        Long locatetime = obj.getLong("locatetime");
                        Date date = new Date(locatetime);
                        if(date.after(orderPrivateCar.getStartServiceTime()) && date.before(orderPrivateCar.getEndServiceTime())){
                            // 封装定位文件里的单个定位对象
                            OrderPosition orderPosition = new OrderPosition();
                            orderPosition.setOrderId(orderId);
                            orderPosition.setOrderType(1);
                            orderPosition.setDriverId(orderPrivateCar.getDriverId());
                            orderPosition.setLat(split[1]);
                            orderPosition.setLon(split[0]);
                            orderPosition.setAltitude("0");
                            orderPosition.setDirectionAngle("0");
                            //先计算里程,在存储最新位置
                            orderPosition.setInsertTime(new Date());
                            orderPositions.add(orderPosition);
                            if(StringUtils.hasLength(fromLonLat) && StringUtils.hasLength(toLonLat)){
                                String[] from = fromLonLat.split(",");
                                String[] to = toLonLat.split(",");
                                GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(from[1]), Double.valueOf(from[0]));
                                GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(to[1]), Double.valueOf(to[0]));
                                double WGS84 = getDistanceMeter(source, target, Ellipsoid.WGS84);
                                sum += WGS84;
                            }
                            toLonLat = fromLonLat;
                        }
                    }
    
                    //存储轨迹
                    // 将数据存储到文件中
                    File file = new File(filePath + orderId + "_" + 1 + ".txt");
                    if(!file.exists()){
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                    }
                    //写入相应的文件
                    PrintWriter out = new PrintWriter(new FileWriter(file));
                    out.write(JSON.toJSONString(orderPositions));
                    out.flush();
                    out.close();
                }catch (Exception e){
                    e.printStackTrace();
                    System.err.println("查询高德轨迹出错,使用本地轨迹计算");
                    List<OrderPosition> list = orderPositionService.queryPosition(orderId, 1);
                    String fromLonLat = "";
                    for (OrderPosition orderPosition : list) {
                        String toLonLat = orderPosition.getLon() + "," + orderPosition.getLat();
                        if(StringUtils.hasLength(fromLonLat) && StringUtils.hasLength(toLonLat)){
                            String[] from = fromLonLat.split(",");
                            String[] to = toLonLat.split(",");
                            GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(from[1]), Double.valueOf(from[0]));
                            GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(to[1]), Double.valueOf(to[0]));
                            double WGS84 = getDistanceMeter(source, target, Ellipsoid.WGS84);
                            sum += WGS84;
                        }
                        fromLonLat = toLonLat;
                    }
                }
                orderPrivateCar.setMileage(sum);
                orderPrivateCar.setIsAbnormal(0);
                break;
        }
        this.updateById(orderPrivateCar);
 
        // TODO: 2020/6/5 推送状态
        OrderPrivateCar finalOrderPrivateCar = orderPrivateCar;
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, finalOrderPrivateCar.getUserId(), finalOrderPrivateCar.getId(), 1, finalOrderPrivateCar.getState());
                pushUtil.pushOrderState(2, finalOrderPrivateCar.getDriverId(), finalOrderPrivateCar.getId(), 1, finalOrderPrivateCar.getState());
            }
        }).start();
        return ResultUtil.success();
    }
 
    private double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
        //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
 
 
    /**
     * 确认费用操作
     * @param orderId
     * @param type
     * @param parkingFee
     * @param crossingFee
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil confirmFees(Integer orderId, Integer type, Double parkingFee, Double crossingFee) throws Exception {
        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
        if(orderPrivateCar.getState() != 5){
            return ResultUtil.error("异常操作,请刷新订单");
        }
        if(orderPrivateCar.getArriveTime()==null){
            orderPrivateCar.setArriveTime(orderPrivateCar.getStartServiceTime());
        }
        orderPrivateCar = this.setMoney(orderPrivateCar, 0D, 0D);//计算费用
        orderPrivateCar.setPayManner(type);
        orderPrivateCar.setParkMoney(null == parkingFee ? 0D : parkingFee);
        orderPrivateCar.setRoadTollMoney(null == crossingFee ? 0D : crossingFee);
        orderPrivateCar.setOrderMoney(orderPrivateCar.getOrderMoney() + orderPrivateCar.getParkMoney() + orderPrivateCar.getRoadTollMoney());
        //判断是否首单免费-免费直接完成
        Integer orderNumber = this.selectCount(new EntityWrapper<OrderPrivateCar>().eq("userId",orderPrivateCar.getUserId()).last("and (state=8 or state=9)"));
 
        if(orderNumber<=0){//判断是否是首单
            //判断是否免单
            UserInfo userInfo = userInfoMapper.selectById(orderPrivateCar.getUserId());
            if(userInfo.getFreeMoney().doubleValue()>0d){
                if(orderPrivateCar.getOrderMoney()<=userInfo.getFreeMoney().doubleValue()){
                    orderPrivateCar.setState(8);
                    orderPrivateCar.setIsFree(2);
                    //添加已收入明细
                    Company company = companyService.selectById(orderPrivateCar.getCompanyId());
                    Double speMoney = company.getSpeMoney();
                    BigDecimal d = null;//企业收入
                    BigDecimal c = null;//司机收入
                    if(company.getIsSpeFixedOrProportional() == 2){//固定
                        d = new BigDecimal(speMoney);
                        c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d);//只有出行金额参与抽成,其余归属司机
                    }
                    if(company.getIsSpeFixedOrProportional() == 1){//比例
                        Double price = orderPrivateCar.getStartMoney() + orderPrivateCar.getMileageMoney() + orderPrivateCar.getWaitMoney() + orderPrivateCar.getDurationMoney() + orderPrivateCar.getLongDistanceMoney();
                        d = new BigDecimal(price).multiply(new BigDecimal(speMoney).divide(new BigDecimal(100), new MathContext(2, RoundingMode.HALF_EVEN))).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                        c = new BigDecimal(orderPrivateCar.getOrderMoney()).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
                    }
                    incomeService.saveData(1, orderPrivateCar.getCompanyId(), 2, orderPrivateCar.getId(), 1, d.doubleValue());
                    incomeService.saveData(2, orderPrivateCar.getDriverId(), 2, orderPrivateCar.getId(), 1, c.doubleValue());
                    Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
                    driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                    driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                    driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                    driverService.updateById(driver);
                }else{
                    orderPrivateCar.setState(7);
                    orderPrivateCar.setOrderMoney(orderPrivateCar.getOrderMoney()-userInfo.getFreeMoney().doubleValue());
                    orderPrivateCar.setFreeMoney(userInfo.getFreeMoney());
 
                }
 
            }else{
                orderPrivateCar.setState(7);
            }
 
        }else{
            orderPrivateCar.setState(7);
        }
 
//        Map<String, String> map = chinaMobileUtil.midAxbUnBindSend(orderPrivateCar.getBindId(),orderPrivateCar.getTelX());
//        if(String.valueOf(map.get("code")).equals("200")){
//            orderPrivateCar.setTelX("");
//            orderPrivateCar.setBindId("");
//        }
        this.updateById(orderPrivateCar);
 
 
        pushUtil.removeTask(orderId, 1);//删除定时任务,结束推送数据
        systemNoticeService.addSystemNotice(1, "司机已结束本次行程,谢谢使用", orderPrivateCar.getUserId());
 
        //回滚司机状态为空闲
        Driver driver = driverService.selectById(orderPrivateCar.getDriverId());
        driver.setState(2);
        driverService.updateById(driver);
 
        OrderPrivateCar finalOrderTaxi = orderPrivateCar;
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, finalOrderTaxi.getUserId(), finalOrderTaxi.getId(), 1, finalOrderTaxi.getState());
                pushUtil.pushOrderState(2, finalOrderTaxi.getDriverId(), finalOrderTaxi.getId(), 1, finalOrderTaxi.getState());
            }
        }).start();
 
        OrderPrivateCar finalOrderPrivateCar = orderPrivateCar;
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(pushMinistryOfTransport){//上传交通数据
                    pushMinistryOfTransportUtil.baseInfoVehicleTotalMile(finalOrderPrivateCar.getCarId());
                    pushMinistryOfTransportUtil.operateArrive(orderId);
                }
            }
        }).start();
 
 
        return ResultUtil.success();
    }
 
 
    /**
     * 获取订单费用明细
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryMoneyInfo(Integer orderId) throws Exception {
        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
        if(orderPrivateCar.getState() == 5){//服务中的时候获取实时费用数据
            this.setMoney(orderPrivateCar, 0D, 0D);
        }
 
        Map<String, Object> map = new HashMap<>();
        map.put("orderMoney", orderPrivateCar.getOrderMoney());//订单金额
        map.put("startMileage", orderPrivateCar.getStartMileage());//起步价
        map.put("startMoney", orderPrivateCar.getStartMoney());//起步价
        map.put("mileageKilometers", orderPrivateCar.getMileageKilometers());//里程费
        map.put("mileageMoney", orderPrivateCar.getMileageMoney());//里程费
        map.put("duration", orderPrivateCar.getDuration());//时长费
        map.put("durationMoney", orderPrivateCar.getDurationMoney());//时长费
        map.put("wait", orderPrivateCar.getWait());//等待费
        map.put("waitMoney", orderPrivateCar.getWaitMoney());//等待费
        map.put("longDistance", orderPrivateCar.getLongDistance());//远途费
        map.put("longDistanceMoney", orderPrivateCar.getLongDistanceMoney());//远途费
        map.put("parkMoney", orderPrivateCar.getParkMoney());//停车费
        map.put("roadTollMoney", orderPrivateCar.getRoadTollMoney());//过路费
        map.put("redPacketMoney", orderPrivateCar.getRedPacketMoney());//红包抵扣金额
        map.put("couponMoney", orderPrivateCar.getCouponMoney());//优惠券抵扣金额
        map.put("discountMoney", orderPrivateCar.getDiscountMoney());//折扣抵扣金额
        map.put("discount", orderPrivateCar.getDiscount());//折扣
        return map;
    }
 
    /**
     * 计算已服务的实时里程
     * @param orderId
     * @param lon
     * @param lat
     */
    @Override
    public boolean calculateMileage(Integer orderId, String lon, String lat) throws Exception {
        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
        OrderPosition orderPosition = orderPositionService.queryNew(orderId, 1);
        String now = lon + "," + lat;
        String old = null;
        if(null != orderPosition){
            old = orderPosition.getLon() + "," + orderPosition.getLat();
        }else{
            orderPrivateCar.setMileage(0D);
            this.updateById(orderPrivateCar);
            return true;//第一条数据不作处理,直接存储
        }
        Map<String, Double> distance = GeodesyUtil.getDistance(now, old);//直线距离
        if(null != distance){
            Double distance1 = distance.get("WGS84");
            if(distance1 > 50 && orderPrivateCar.getState()==5){//大于50米表示在移动
                orderPrivateCar.setMileage(new BigDecimal(orderPrivateCar.getMileage()).add(new BigDecimal(distance1)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                this.updateById(orderPrivateCar);
                return true;
            }
            return false;
        }else{
            System.err.println("调用高德计算距离出错");
        }
        return false;
    }
 
    @Override
    public List<OrderPrivateCar> query(Integer driverId, Integer... state) throws Exception {
        return orderPrivateCarMapper.query(Arrays.asList(state), driverId);
    }
 
    /**
     * 计算价格
     * @param orderPrivateCar
     * @param parkingFee
     * @param crossingFee
     * @return
     * @throws Exception
     */
    public OrderPrivateCar setMoney(OrderPrivateCar orderPrivateCar, Double parkingFee, Double crossingFee) throws Exception {
        Map<String, Object> query1 = systemPriceMapper.query(orderPrivateCar.getCompanyId(), 1, orderPrivateCar.getServerCarModelId());
        //开始根据不同的方式计算金额
        double amount = 0;
        JSONObject jsonObject = JSON.parseObject(query1.get("content").toString());
        Double num1 = jsonObject.getDouble("num1");//起步价(元)
        Double num2 = jsonObject.getDouble("num2");//起步公里(公里)
        Double num3 = jsonObject.getDouble("num3");//起步时间(分钟)
        Double num4 = jsonObject.getDouble("num4");//里程费(元)
        Double num5 = jsonObject.getDouble("num5");//时长费(分钟)
        Double num6 = jsonObject.getDouble("num6");//等待费(分钟)
        Double num7 = jsonObject.getDouble("num7");//等待费(元)
        Double num8 = jsonObject.getDouble("num8");//远途费(公里)
        Double num9 = jsonObject.getDouble("num9");//远途费(公里)
        Double num10 = jsonObject.getDouble("num10");//远途费(元)
        Double num11 = jsonObject.getDouble("num11");//远途费(公里)
        Double num12 = jsonObject.getDouble("num12");//远途费(公里)
        Double num13 = jsonObject.getDouble("num13");//远途费(元)
        Double num14 = jsonObject.getDouble("num14");//远途费(公里)
        Double num15 = jsonObject.getDouble("num15");//远途费(元)
        String num16 = jsonObject.getString("num16");//夜间费(开始时间)
        Double num17 = jsonObject.getDouble("num17");//夜间费(元)
        Double num18 = jsonObject.getDouble("num18");//夜间费(元)
        Double num19 = jsonObject.getDouble("num19");//夜间费(元)
        Double num20 = jsonObject.getDouble("num20");//夜间费(元)
        Double num21 = jsonObject.getDouble("num21");//夜间费(元)
        Double num22 = jsonObject.getDouble("num22");//夜间费(元)
        String num23 = jsonObject.getString("num23");//高峰费(开始时间)
        String num24 = jsonObject.getString("num24");//高峰费(开始时间)
        Double num25 = jsonObject.getDouble("num25");//高峰费(元)
        Double num26 = jsonObject.getDouble("num26");//高峰费(元)
        Double num27 = jsonObject.getDouble("num27");//高峰费(元)
        Double num28 = jsonObject.getDouble("num28");//高峰费(元)
        Double num29 = jsonObject.getDouble("num29");//高峰费(元)
        Double num30 = jsonObject.getDouble("num30");//高峰费(元)
 
        Date date = new Date();
        double d = (null == orderPrivateCar.getMileage() ? 0D : orderPrivateCar.getMileage()) / 1000;//实际公里
        double t = ((orderPrivateCar.getEndServiceTime().getTime() - orderPrivateCar.getStartServiceTime().getTime()) / 60000) + 1;//实际时间(不满一分钟按一分钟算)
        double w = ((orderPrivateCar.getStartServiceTime().getTime() - orderPrivateCar.getArriveTime().getTime()) / 60000) + 1;//等待分钟(不满一分钟按一分钟算)
        double d1 = (d - num2) < 0 ? 0 : d - num2;//超出起步里程的公里
        double t1 = (t - num3) < 0 ? 0 : new BigDecimal(t - num3).setScale(0, BigDecimal.ROUND_UP).doubleValue();//超过起步分钟数的时间
        double w1 = (w - num6) < 0 ? 0 : new BigDecimal(w - num6).setScale(0, BigDecimal.ROUND_UP).doubleValue();//超出等待时间的时间
        double yt1 = 0;//远途1段
        double yt2 = 0;//远途2段
        double yt3 = 0;//远途3段
 
 
        //夜间服务处理逻辑
        Calendar s = Calendar.getInstance();
        s.setTime(date);
        s.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num16.split(" - ")[0].split(":")[0]));
        s.set(Calendar.MINUTE, Integer.valueOf(num16.split(" - ")[0].split(":")[1]));
 
        Calendar e = Calendar.getInstance();
        e.setTime(date);
        e.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num16.split(" - ")[1].split(":")[0]));
        e.set(Calendar.MINUTE, Integer.valueOf(num16.split(" - ")[1].split(":")[1]));
 
        if(date.getTime() > s.getTimeInMillis() && date.getTime() < e.getTimeInMillis()){
            if(d > num8.doubleValue() && d <= num9.doubleValue()){
                yt1 = num20 * (d - num8);
            }
            if(d > num9.doubleValue()){
                yt1 = num20 * (num9 - num8);
            }
            if(d > num11.doubleValue() && d <= num12.doubleValue()){
                yt2 = num21 * (d - num11);
            }
            if(d > num12.doubleValue()){
                yt2 = num21 * (num12 - num11);
            }
            if(d > num14.doubleValue()){
                yt3 = num22 * (d - num14);
            }
            amount = num17 + (d1 * num18) + (t1 * num19) + (w1 * num7) + yt1 + yt2 + yt3;
            orderPrivateCar.setStartMileage(num2);
            orderPrivateCar.setStartMoney(num17);//起步价
            orderPrivateCar.setStartDuration((int)(num3.doubleValue()));
            orderPrivateCar.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setMileageMoney(new BigDecimal(d1 * num18).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
            orderPrivateCar.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setDurationMoney(new BigDecimal(t1 * num19).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//时长费
            orderPrivateCar.setWait(new BigDecimal(w1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setWaitMoney(new BigDecimal(w1 * num7).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//等待费
            orderPrivateCar.setLongDistance(new BigDecimal((d > num8) ? d - num8 : 0).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setLongDistanceMoney(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//远途费
            orderPrivateCar.setParkMoney(parkingFee);//停车费
            orderPrivateCar.setRoadTollMoney(crossingFee);//过路费
            orderPrivateCar.setRedPacketMoney(0D);//红包抵扣
            orderPrivateCar.setCouponMoney(0D);//优惠券抵扣
            orderPrivateCar.setDiscount(0D);//优惠抵扣
            orderPrivateCar.setPayMoney(0D);//支付金额
            orderPrivateCar.setOrderMoney(new BigDecimal(amount + parkingFee + crossingFee).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setMoneyType(2);
            orderPrivateCar.setMoneyTime(num16);
            return orderPrivateCar;
        }
 
 
        //高峰时段处理逻辑
        Calendar s1 = Calendar.getInstance();
        s1.setTime(date);
        s1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num23.split(" - ")[0].split(":")[0]));
        s1.set(Calendar.MINUTE, Integer.valueOf(num23.split(" - ")[0].split(":")[1]));
 
        Calendar e1 = Calendar.getInstance();
        e1.setTime(date);
        e1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num23.split(" - ")[1].split(":")[0]));
        e1.set(Calendar.MINUTE, Integer.valueOf(num23.split(" - ")[1].split(":")[1]));
 
        Calendar s2 = Calendar.getInstance();
        s2.setTime(date);
        s2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num24.split(" - ")[0].split(":")[0]));
        s2.set(Calendar.MINUTE, Integer.valueOf(num24.split(" - ")[0].split(":")[1]));
 
        Calendar e2 = Calendar.getInstance();
        e2.setTime(date);
        e2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num24.split(" - ")[1].split(":")[0]));
        e2.set(Calendar.MINUTE, Integer.valueOf(num24.split(" - ")[1].split(":")[1]));
 
        if((date.getTime() > s1.getTimeInMillis() && date.getTime() < e1.getTimeInMillis()) || (date.getTime() > s2.getTimeInMillis() && date.getTime() < e2.getTimeInMillis())){
            if(d > num8.doubleValue() && d <= num9.doubleValue()){
                yt1 = num28 * (d - num8);
            }
            if(d > num9.doubleValue()){
                yt1 = num28 * (num9 - num8);
            }
            if(d > num11.doubleValue() && d <= num12.doubleValue()){
                yt2 = num29 * (d - num11);
            }
            if(d > num12.doubleValue()){
                yt2 = num29 * (num12 - num11);
            }
            if(d > num14.doubleValue()){
                yt3 = num30 * (d - num14);
            }
            amount = num25 + (d1 * num26) + (t1 * num27) + (w1 * num7) + yt1 + yt2 + yt3;
            orderPrivateCar.setStartMileage(num2);
            orderPrivateCar.setStartDuration((int)(num3.doubleValue()));
            orderPrivateCar.setStartMoney(num25);//起步价
            orderPrivateCar.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setMileageMoney(new BigDecimal(d1 * num26).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
            orderPrivateCar.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setDurationMoney(new BigDecimal(t1 * num27).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//时长费
            orderPrivateCar.setWait(new BigDecimal(w1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setWaitMoney(new BigDecimal(w1 * num7).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//等待费
            orderPrivateCar.setLongDistance(new BigDecimal((d > num8) ? d - num8 : 0).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            orderPrivateCar.setLongDistanceMoney(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//远途费
            orderPrivateCar.setParkMoney(parkingFee);//停车费
            orderPrivateCar.setRoadTollMoney(crossingFee);//过路费
            orderPrivateCar.setRedPacketMoney(0D);//红包抵扣
            orderPrivateCar.setCouponMoney(0D);//优惠券抵扣
            orderPrivateCar.setDiscount(0D);//优惠抵扣
            orderPrivateCar.setPayMoney(0D);//支付金额
            orderPrivateCar.setOrderMoney(new BigDecimal(amount + parkingFee + crossingFee).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
 
            orderPrivateCar.setMoneyType(3);
            if((date.getTime() > s1.getTimeInMillis() && date.getTime() < e1.getTimeInMillis())){
                orderPrivateCar.setMoneyTime(num23);
            }else{
                orderPrivateCar.setMoneyTime(num24);
            }
 
            return orderPrivateCar;
        }
 
        //其他时间段的计算
        if(d > num8.doubleValue() && d <= num9.doubleValue()){
            yt1 = num10 * (d - num8);
        }
        if(d > num9.doubleValue()){
            yt1 = num10 * (num9 - num8);
        }
        if(d > num11.doubleValue() && d <= num12.doubleValue()){
            yt2 = num13 * (d - num11);
        }
        if(d > num12.doubleValue()){
            yt2 = num13 * (num12 - num11);
        }
        if(d > num14.doubleValue()){
            yt3 = num15 * (d - num14);
        }
        amount = num1 + (d1 * num4) + (t1 * num5) + (w1 * num7) + yt1 + yt2 + yt3;
        orderPrivateCar.setStartMileage(num2);
        orderPrivateCar.setStartDuration((int)(num3.doubleValue()));
        orderPrivateCar.setStartMoney(num1);//起步价
        orderPrivateCar.setMileageKilometers(new BigDecimal(d1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setMileageMoney(new BigDecimal(d1 * num4).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//里程费
        orderPrivateCar.setDuration(new BigDecimal(t1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setDurationMoney(new BigDecimal(t1 * num5).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//时长费
        orderPrivateCar.setWait(new BigDecimal(w1).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setWaitMoney(new BigDecimal(w1 * num7).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//等待费
        orderPrivateCar.setLongDistance(new BigDecimal((d > num8) ? d - num8 : 0).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
        orderPrivateCar.setLongDistanceMoney(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());//远途费
        orderPrivateCar.setParkMoney(parkingFee);//停车费
        orderPrivateCar.setRoadTollMoney(crossingFee);//过路费
        orderPrivateCar.setRedPacketMoney(0D);//红包抵扣
        orderPrivateCar.setCouponMoney(0D);//优惠券抵扣
        orderPrivateCar.setDiscount(0D);//优惠抵扣
        orderPrivateCar.setPayMoney(0D);//支付金额
        orderPrivateCar.setOrderMoney(new BigDecimal(amount + parkingFee + crossingFee).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
 
        orderPrivateCar.setMoneyType(1);
        return orderPrivateCar;
    }
 
    @Override
    public List<OrderPrivateCar> taskMidAxbUnBindSend() {
        return this.baseMapper.taskMidAxbUnBindSend();
    }
 
    @Override
    public List<OrderPrivateCar> getSmsOrderList() {
        return this.baseMapper.getSmsOrderList();
    }
 
    @Override
    public void getTrackId(Integer orderId, Integer trackId) {
        OrderPrivateCar orderPrivateCar = this.selectById(orderId);
        orderPrivateCar.setTrackId(String.valueOf(trackId));
        this.updateById(orderPrivateCar);
    }
}