yyy
2026-05-08 d2f2d372e03a81cdf2e44f02f1105cfb32dad88d
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
package com.xiaopeng.itinerary.service.impl;
 
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaopeng.api.dto.account.GetUserCouponDto;
import com.xiaopeng.api.dto.account.UcUserInfo;
import com.xiaopeng.api.dto.map.RouteAndFareDto;
import com.xiaopeng.api.dto.map.RouteStaleCheckDto;
import com.xiaopeng.api.dto.map.SelectNavigationRouteDto;
import com.xiaopeng.api.enums.RouteStrategyKind;
import com.xiaopeng.api.feignClient.account.UserResourcesClient;
import com.xiaopeng.api.feignClient.account.UserRoutePlanRuleClient;
import com.xiaopeng.api.feignClient.account.XpAppUserClient;
import com.xiaopeng.api.feignClient.account.XpAppUserCouponClient;
import com.xiaopeng.api.feignClient.map.XpMapClient;
import com.xiaopeng.api.feignClient.operation.XpPointClient;
import com.xiaopeng.api.feignClient.operation.XpTargetedServiceClient;
import com.xiaopeng.api.feignClient.operation.XpTradingStrategyClient;
import com.xiaopeng.api.feignClient.order.XpOrderClient;
import com.xiaopeng.api.feignClient.pushMessage.XpPushMessageClient;
import com.xiaopeng.api.feignClient.vehicleConvoy.XpVehicleClient;
import com.xiaopeng.api.model.account.XpUserRoutePlanRule;
import com.xiaopeng.api.model.operation.*;
import com.xiaopeng.api.model.order.XpOrder;
import com.xiaopeng.api.vo.account.UserCouponVo;
import com.xiaopeng.api.vo.operation.*;
import com.xiaopeng.api.vo.order.XpPricingCardVo;
import com.xiaopeng.common.core.constant.CacheConstants;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.DateUtils;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.itinerary.constant.I18nKeyConstant;
import com.xiaopeng.itinerary.dto.XpOrderDto;
import com.xiaopeng.itinerary.mapper.*;
import com.xiaopeng.itinerary.navigation.MultiRouteSnapshotPayload;
import com.xiaopeng.itinerary.service.BootCommonService;
import com.xiaopeng.itinerary.service.IXpGeoQueryEntityService;
import com.xiaopeng.itinerary.service.XpPointService;
import com.xiaopeng.itinerary.service.XpPricingService;
import com.xiaopeng.itinerary.utils.XpPricingUtil;
import com.xiaopeng.itinerary.utils.holiday.HolidayUtil;
import com.xiaopeng.itinerary.utils.map.FastDistanceCalculator;
import com.xiaopeng.itinerary.utils.map.GDMapGeocodingUtil;
import com.xiaopeng.itinerary.vo.QueueStatusVo;
import com.xiaopeng.itinerary.vo.RedisQueuePointIdVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Service;
 
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 定价管理 服务实现类
 * </p>
 *
 * @author liu
 * @since 2026-02-03
 */
@Slf4j
@Service
public class XpPricingServiceImpl extends ServiceImpl<XpPricingMapper, XpPricing> implements XpPricingService {
 
 
    @Resource
    private XpPricingUtil xpPricingUtil;
 
    @Resource
    private XpMapClient xpMapClient;
 
    @Resource
    private BootCommonService bootCommonService;
    @Resource
    private XpOpenCityMapper xpOpenCityMapper;
 
    @Resource
    private XpRoutePlanMapper xpRoutePlanMapper;
 
    @Resource
    private RedisService redisService;
 
    @Resource
    private XpSystemSettingMapper systemSettingMapper;
 
    @Resource
    private XpAppUserCouponClient xpAppUserCouponClient;
 
    @Resource
    private XpOrderClient xpOrderClient;
    @Resource
    private XpVehicleClient xpVehicleClient;
 
    @Resource
    private XpTradingStrategyMapper xpTradingStrategyMapper;
 
    @Resource
    private XpTradingStrategyPushMapper xpTradingStrategyPushMapper;
 
    @Resource
    private XpElectronicFenceAreaMapper xpElectronicFenceAreaMapper;
 
    @Resource
    private XpWhitelistMapper xpWhitelistMapper;
 
    @Resource
    private XpAppUserClient xpAppUserClient;
 
    @Resource
    private HolidayUtil holidayUtil;
 
    @Autowired
    private IXpGeoQueryEntityService geoQueryEntityService;
 
    @Autowired
    private XpElectronicFenceMapper xpElectronicFenceMapper;
 
    @Autowired
    private XpPointService xpPointService;
 
    @Resource
    private XpPushMessageClient xpPushMessageClient;
 
    @Resource
    private GDMapGeocodingUtil gdMapGeocodingUtil;
    @Resource
    private XpPointClient xpPointClient;
 
    @Resource
    private UserRoutePlanRuleClient userRoutePlanRuleClient;
    @Resource
    private XpTargetedServiceClient xpTargetedServiceClient;
    @Resource
    private XpTradingStrategyClient xpTradingStrategyClient;
    @Resource
    private UserResourcesClient userResourcesClient;
 
    @Resource(name = "hutoolSnowflake")
    private Snowflake snowflake;
 
    @Value("${navigation.route.stale-seconds:60}")
    private int navigationStaleSeconds;
 
    @Value("${navigation.route.price-wave-percent:5}")
    private double navigationPriceWavePercent;
 
    /**
     * 后台可配置的偏好项编码,逗号分隔,如 SMART,TIME,PRICE,AVOID_CONGESTION
     */
    @Value("${navigation.route.preference.enabled-codes:SMART,TIME,PRICE}")
    private String navigationPreferenceEnabledCodes;
 
    // 自定义线程池用于并行执行任务
    private final ExecutorService executorService;
 
 
    // 初始化自定义线程池
    public XpPricingServiceImpl() {
        this.executorService = new ThreadPoolExecutor(
                40, // 核心线程数
                100, // 最大线程数
                60, // 线程空闲时间(秒)
                TimeUnit.SECONDS, // 时间单位
                new LinkedBlockingQueue<>(400), // 工作队列
                new ThreadFactory() {
                    private final AtomicInteger threadNumber = new AtomicInteger(1);
 
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r);
                        thread.setName("pricing-service-thread-" + threadNumber.getAndIncrement());
                        thread.setDaemon(false);
                        return thread;
                    }
                }, // 线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
    }
 
    // 关闭线程池
    @PreDestroy
    public void destroy() {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
            try {
                if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                executorService.shutdownNow();
                Thread.currentThread().interrupt();
            }
        }
    }
 
    /**
     * 根据当前时间获取时段
     * 例如:15:50 → 15-16
     * @return 时段字符串,格式:HH-HH
     */
    private String getCurrentTimeSlot() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour + "-" + (hour + 1);
    }
 
    public XpPricing getInfo(Long cityId,String vehicleModel){
 
        LambdaQueryWrapper<XpPricing> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XpPricing::getDeleted, 0);
        queryWrapper.eq(XpPricing::getCityId, cityId);
        if(vehicleModel != null){
            queryWrapper.eq(XpPricing::getVehicleModel, vehicleModel);
        }
        return super.getOne(queryWrapper);
    }
 
 
    @Override
    public List<RouteAndFareVo> getRouteAndFare(RouteAndFareDto routeAndFareDto, String userId) {
        ArrayList<RouteAndFareVo> routeAndFareVos = new ArrayList<>(3);
 
        // 1. 基础数据查询
        Map<String, String> geocode = gdMapGeocodingUtil.geocode(routeAndFareDto.getOrigin().getLongitude().toString(), routeAndFareDto.getOrigin().getLatitude().toString());
        if(null == geocode){
            throw new ServiceException("城市暂未开通服务", I18nKeyConstant.CITY_NOT_FOUND);
        }
        Long cityCode = routeAndFareDto.getOpenCityId();
        XpOpenCity openCities = xpOpenCityMapper.selectById(cityCode);
        if (openCities == null ) {
            throw new ServiceException("城市暂未开通服务", I18nKeyConstant.CITY_NOT_FOUND);
        }
        routeAndFareDto.setOpenCityId(openCities.getId());
        //根据订单获取车型 获取定价策略
        XpPricing xpPricing = bootCommonService.getXpPricing(routeAndFareDto.getOpenCityId(), routeAndFareDto.getOrderId(), null);
        if (xpPricing == null) {
            throw new ServiceException("当前区域未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
        }
        routeAndFareDto.setOpenCityId(openCities.getId());
        List<XpRoutePlan> xpRoutePlan = xpRoutePlanMapper.selectList(new LambdaQueryWrapper<XpRoutePlan>().eq(XpRoutePlan::getAuditStatus, 2).eq(XpRoutePlan::getOpenCityId, cityCode));
 
        // 找出用户偏好
        Object cacheObject = redisService.getCacheObject(CacheConstants.USER_ROUTE_PREFERENCE + userId);
        for (XpRoutePlan routePlan : xpRoutePlan) {
 
            if (xpRoutePlan != null) {
                routeAndFareDto.setRoutePlanRule(routePlan.getRoutePlanRule());
            }
            // Object userPref = redisService.getCacheObject(CacheConstants.USER_ROUTE_PREFERENCE + userId);
            // if (userPref != null) {
            //     try {
            //         routeAndFareDto.setRoutePlanRule(Integer.valueOf(userPref.toString()));
            //     } catch (Exception ignored) {
            //         // keep city default
            //     }
            // }
 
            // 算距离
            R<RouteVo> distanceAndDuration = xpMapClient.getDistanceAndDuration(routeAndFareDto);
            if (distanceAndDuration.getCode() != 200) {
//                throw new ServiceException("获取导肮信息失败", I18nKeyConstant.PATH_NOT_FOUND);
                continue;
            }
 
 
            RouteVo routeVo = distanceAndDuration.getData();
 
            // 2. 计算价格(需要在并行任务前完成,因为优惠券查询需要价格)
            XpPricingCardVo xpPricingCardVo;
            if (xpPricing.getPricingType() == 1) {
                xpPricingCardVo = xpPricingUtil.calculationPrice(userId,
                        JSON.parseObject(xpPricing.getBasicPricingJson()),
                        JSON.parseArray(xpPricing.getStartingFareJson()),
                        routeVo.getDistance(), routeVo.getDuration(), 0, 0.0, xpPricing.getIsDynamicPrice(),
                        routeAndFareDto.getOpenCityId(), xpPricing.getDynamicPricingJson()
                );
            } else {
                xpPricingCardVo = xpPricingUtil.calculationFixedPrice(userId,
                        JSON.parseObject(xpPricing.getBasicPricingJson()),
                        JSON.parseArray(xpPricing.getStartingFareJson()),
                        routeVo.getDistance(), routeVo.getDuration(), 0, 0.0, xpPricing.getIsDynamicPrice(), routeAndFareDto.getOpenCityId(), xpPricing.getDynamicPricingJson()
                );
            }
 
            // 优惠券:null=自动最佳;0=不使用;>0=指定券
            XpPricingCardVo finalXpPricingCardVo = xpPricingCardVo;
            R<UserCouponVo> coupon = null;
            CompletableFuture<R<UserCouponVo>> couponFuture=null;
            Long ucId = routeAndFareDto.getUserCouponId();
            if (Long.valueOf(0L).equals(ucId)) {
                coupon = R.ok(new UserCouponVo());
            } else if (ucId != null && ucId > 0L) {
                coupon = xpAppUserCouponClient.getUserCoupon(new GetUserCouponDto(Long.valueOf(userId), finalXpPricingCardVo.getTotalPrice(), routeAndFareDto.getOpenCityId(), ucId));
                if(coupon.getCode()==200 && coupon.getData().getStatus()==2){
                    throw new ServiceException("优惠券不满足条件", I18nKeyConstant.COUPON_NOT_SATISFY);
                }
            } else {
                couponFuture= CompletableFuture.supplyAsync(
                        () -> xpAppUserCouponClient.getUserCoupon(
                                new GetUserCouponDto(Long.valueOf(userId), finalXpPricingCardVo.getTotalPrice(), routeAndFareDto.getOpenCityId(),null)
                        ),
                        executorService
                );
            }
 
 
 
            // 3. 并行执行远程服务调用 判断是否同一经营区域
            CompletableFuture<R<Boolean>> diagnosisAreaFuture = CompletableFuture.supplyAsync(
                    () -> xpMapClient.diagnosisArea(routeAndFareDto),
                    executorService
            );
 
            // 4. 等待所有并行任务完成并获取结果
            R<Boolean> diagnosisResult = diagnosisAreaFuture.join();
            R<UserCouponVo> couponResult =null;
            if (Long.valueOf(0L).equals(ucId) || (ucId != null && ucId > 0L)) {
                couponResult = coupon;
            } else {
                couponResult = couponFuture.join();
            }
 
            // 5. 处理诊断结果
            if (diagnosisResult.getCode() != R.SUCCESS) {
                throw new RuntimeException(diagnosisResult.getMsg());
            }
            if (!diagnosisResult.getData()) {
                throw new ServiceException("当前区域未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
            }
 
//        // 6. 处理路线结果
//        if (routeResult.getCode() != 200) {
//            throw new RuntimeException(routeResult.getMsg());
//        }
//        RouteVo routeVo = routeResult.getData();
 
            // 起终点最小距离 走缓存
            Integer minOrderDistance;
            Object minOrderDistanceObj = redisService.getCacheObject(CacheConstants.MIN_ORDER_DISTANCE);
            if (minOrderDistanceObj != null) {
                minOrderDistance = Integer.valueOf(minOrderDistanceObj.toString());
            } else {
                minOrderDistance = systemSettingMapper.selectOne(null).getMinOrderDistance();
            }
            if (routeVo.getDistance() < Integer.valueOf(minOrderDistance)) {
                throw new ServiceException("当前上下车点距离过近,建议步行前往", I18nKeyConstant.TO_WORK);
            }
 
            // 7. 处理优惠券结果
            if (couponResult.getCode() != R.SUCCESS) {
                throw new RuntimeException(couponResult.getMsg());
            }
            UserCouponVo userCouponVo = couponResult.getData();
            if (userCouponVo != null) {
                Double money = userCouponVo.getMoney();
                money = new BigDecimal(money).setScale(2, RoundingMode.HALF_UP).doubleValue();
                xpPricingCardVo.setCoupon(String.valueOf(money));
                if (xpPricingCardVo.getTotalPrice() > money) {
                    xpPricingCardVo.setSystemDiscount(xpPricingCardVo.getTotalPrice() - money);
                }
 
 
            }
 
            // 8. 构建响应对象
            RouteAndFareVo routeAndFareVo = new RouteAndFareVo();
            routeAndFareVo.setRoute(routeVo);
            // 转2位小数处理
            xpPricingCardVo.setTotalPriceStr(new BigDecimal(xpPricingCardVo.getTotalPrice()).setScale(2, RoundingMode.HALF_UP).toString());
            xpPricingCardVo.setStartPriceStr(new BigDecimal(xpPricingCardVo.getStartPrice()).setScale(2, RoundingMode.HALF_UP).toString());
            xpPricingCardVo.setDistancePriceStr(new BigDecimal(xpPricingCardVo.getDistancePrice()).setScale(2, RoundingMode.HALF_UP).toString());
            xpPricingCardVo.setDurationPriceStr(new BigDecimal(xpPricingCardVo.getDurationPrice()).setScale(2, RoundingMode.HALF_UP).toString());
            xpPricingCardVo.setSystemDiscountStr(new BigDecimal(xpPricingCardVo.getSystemDiscount()).setScale(2, RoundingMode.HALF_UP).toString());
 
 
            routeAndFareVo.setXpPricingCard(xpPricingCardVo);
 
            FareVo fareVo = new FareVo();
            fareVo.setEstimatedDistanceKm(routeVo.getDistance().doubleValue() / 1000);
            fareVo.setEstimatedDurationMin(routeVo.getDuration() / 60);
 
            // 9. 设置费用信息
            fareVo.setOriginalFare(xpPricingCardVo.getTotalPrice());
            fareVo.setOriginalFareStr(new BigDecimal(fareVo.getOriginalFare()).setScale(2, RoundingMode.HALF_UP).toString());
            if (userCouponVo != null) {
                if(userCouponVo.getMoney()>fareVo.getOriginalFare()){
                    fareVo.setDiscountAmount(fareVo.getOriginalFare());
                    fareVo.setFinalFare(0.00);
                }else {
                    fareVo.setDiscountAmount(userCouponVo.getMoney());
                    fareVo.setFinalFare(fareVo.getOriginalFare() - fareVo.getDiscountAmount());
                }
                if(fareVo.getFinalFare()>0){
                    fareVo.setSystemDiscount(fareVo.getFinalFare());
                    fareVo.setFinalFare(0.00);
                }
                fareVo.setCouponId(userCouponVo.getId());
            } else {
                fareVo.setDiscountAmount(0.00);
                fareVo.setFinalFare(fareVo.getOriginalFare());
                fareVo.setSystemDiscount(fareVo.getFinalFare());
                fareVo.setFinalFare(0.00);
            }
 
            // 10. 检查等待时间
            Integer waitTime = getWaitTime(routeAndFareDto.getOrigin().getLongitude(), routeAndFareDto.getOrigin().getLatitude());
//        if (waitTime > 3) {
//            throw new ServiceException("当前区域正在等待中,请耐心等待", I18nKeyConstant.WAITING_TIME);
//        }
            fareVo.setWaitingTime(waitTime);
 
 
            // 11. 生成订单ID并缓存
            String id = CacheConstants.ORD + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0, 8);
            redisService.setCacheObject(CacheConstants.ORD + id + userId, xpPricingCardVo, 1L, TimeUnit.DAYS);
            fareVo.setId(id);
 
            // 12. 设置费用信息
            fareVo.setFinalFareStr(new BigDecimal(fareVo.getFinalFare()).setScale(2, RoundingMode.HALF_UP).toString());
            fareVo.setOriginalFare(new BigDecimal(fareVo.getOriginalFare()).setScale(2,RoundingMode.HALF_UP).doubleValue());
            fareVo.setSystemDiscount(new BigDecimal(fareVo.getSystemDiscount() == null ? 0 : fareVo.getSystemDiscount()).setScale(2,RoundingMode.HALF_UP).doubleValue());
            routeAndFareVo.setFareEstimate(fareVo);
 
            routeAndFareVo.getXpPricingCard().setFinalFareStr(fareVo.getFinalFareStr());
 
 
//        String voJsonStr = JSONObject.toJSONString(routeAndFareVo);
//         进行缓存
//        redisService.setCacheObject(key, voJsonStr, 5l, TimeUnit.MINUTES);
            //缓存导航数据,用于下单后推送导航给车端和乘客端
            redisService.setCacheObject("ORDER_NAVIGATION_END_DATA:" + userId, JSON.toJSONString(routeVo), 24L, TimeUnit.HOURS);
 
            if(cacheObject!=null){
                if (cacheObject.toString().equals(routePlan.getRoutePlanRule().toString())) {
                    routeAndFareVo.setPreference(1);
                }
            }
            routeAndFareVo.setRoutePlanName(routePlan.getPlanName());
            routeAndFareVo.setAmapStrategy(routePlan.getRoutePlanRule());
            routeAndFareVos.add(routeAndFareVo);
 
 
        }
        if(routeAndFareVos.isEmpty()){
            throw new ServiceException("获取导肮信息失败", I18nKeyConstant.PATH_NOT_FOUND);
        }
        XpSystemSetting xpSystemSetting = systemSettingMapper.selectOne(null);
        if(null != xpSystemSetting){
            routeAndFareVos.forEach(routeAndFareVo -> {
                routeAndFareVo.setOperateFree(xpSystemSetting.getOperateFree());
            });
        }
        return routeAndFareVos;
    }
 
 
    /**
     * 获取等待时间
     * @param lon
     * @param lat
     * @return
     */
    @Override
    public Integer getWaitTime(double lon, double lat) {
        List<XpGeoQueryEntity> entities = geoQueryEntityService.findByLocationWithin500MetersAndLastWeekAndStatusGreater(
                lon,
                lat,
                getCurrentTimeSlot()
        );
 
        log.info("等待时间查询结果:{}", entities.toString());
 
        if (entities.isEmpty()) {
            return 1;
        } else {
            // 已完成接单的数据
            List<XpGeoQueryEntity> validEntities = entities.stream().filter(e -> e.getStatus() == 1).collect(Collectors.toList());
            long waitNum = entities.stream().filter(e -> e.getStatus() == 0).count();
 
 
            // 计算平均等待时间
            OptionalDouble averageOptional = validEntities.stream().mapToDouble(XpGeoQueryEntity::getSeconds).average();
            if (averageOptional.isPresent()) {
                double average = averageOptional.getAsDouble();
                // 未完成接单的数量
                int count = (int) waitNum;
                if (count == 0) {
                    return 1;
                } else {
                    double waitMinutes = (average * count) / 60;
                    if (waitMinutes > 3) {
//                        throw new ServiceException("当前区域正在等待中,请耐心等待", I18nKeyConstant.WAITING_TIME);
                    } else if (waitMinutes == 0) {
                        return 1;
                    }else if(waitMinutes>0 && waitMinutes<1){
                        return 1;
                    }
                    return (int) waitMinutes;
                }
            } else {
                return 1;
            }
        }
    }
 
    /**
     * 获取等待时间
     * @param lon
     * @param lat
     * @return
     */
    @Override
    public Integer getWaitTimeTimeSlot(double lon, double lat,String timeSlot) {
        List<XpGeoQueryEntity> entities = geoQueryEntityService.findByLocationWithin500MetersAndLastWeekAndStatusGreater(
                lon,
                lat,
                timeSlot
        );
 
 
        if (entities.isEmpty()) {
            return 1;
        } else {
            // 已完成接单的数据
            List<XpGeoQueryEntity> validEntities = entities.stream().filter(e -> e.getStatus() == 1).collect(Collectors.toList());
            // 计算平均等待时间
            OptionalDouble averageOptional = validEntities.stream().mapToDouble(XpGeoQueryEntity::getSeconds).average();
            if (averageOptional.isPresent()) {
                double average = averageOptional.getAsDouble();
                return (int) average;
            } else {
                return 1;
            }
        }
    }
 
 
    @Override
    public String saveOrder(XpOrderDto dto, String uid) {
        //校验白名单用户
//        UcUserInfo userInfo = userResourcesClient.getBaseUserByUid(uid);
//        XpWhitelist xpWhitelist = xpWhitelistMapper.selectOne(new QueryWrapper<XpWhitelist>().eq("account", userInfo.getMobile())
//                .eq("status", 1).last(" and (DATE(expire_time) < DATE(NOW()) or expire_time is null)"));
//        if(null == xpWhitelist){
//            throw new ServiceException("未授权用户,不能使用该功能", I18nKeyConstant.NO_PERMISSION);
//        }
        //判断最大下单数
//        if(null != xpWhitelist.getMaxOrderNum() && xpWhitelist.getMaxOrderNum()>0){
//            Long count = xpOrderClient.getUserOrderNum(Long.valueOf(uid)).getData();
//            if(xpWhitelist.getMaxOrderNum() <= count){
//                throw new ServiceException("已达到最大下单数量,不能使用该功能", I18nKeyConstant.MAX_ORDER_NUM);
//            }
//        }
 
        // 是否在运营区域时间内
        XpElectronicFenceArea xpElectronicFenceArea = xpElectronicFenceAreaMapper.selectById(dto.getXpElectronicFenceAreaId());
        XpElectronicFence xpElectronicFence = xpElectronicFenceMapper.selectById(xpElectronicFenceArea.getElectronicId());
        String operationStrategy = xpElectronicFence.getOperationStrategy();
        Boolean inOperationTime = isInOperationTime(operationStrategy);
        if (!inOperationTime && xpElectronicFence.getFailureType()!=1) {
            log.info("当前区域暂未开通,下单失败 {}", uid);
            throw new ServiceException("当前区域未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
        }
 
 
        Integer cancelCount = xpTradingStrategyMapper.selectOne(null).getCancleCount();
        // 判断是否有进行的订单
        R<Long> r = xpOrderClient.hasUnPaodOrder(uid);
        if (r.getData() > 0) {
            throw new ServiceException("有正在进行的订单");
        }
 
        // 获取起点经纬度
        XpPoint xpPoint = xpPointService.getById(dto.getStartPointId());
        if(xpPoint ==null || xpPoint.getOperationStatus()!=2){
            return null;
        }
 
        // 终点
        XpPoint endXpPoint = xpPointService.getById(dto.getEndPointId());
        if(endXpPoint ==null || endXpPoint.getOperationStatus()!=2){
//            throw new ServiceException("该站点已停止运营", 200,I18nKeyConstant.NO_CURRENT_POINT);
            return null;
        }
 
        // 判断点位是否在运营区域内
        boolean inTime = xpPointClient.nowInTimePoint(dto.getStartPointId()).getData();
        if (!inTime) {
            throw new ServiceException("当前点位未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
        }
        inTime = xpPointClient.nowInTimePoint(dto.getEndPointId()).getData();
        if (!inTime) {
            throw new ServiceException("当前点位未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
        }
 
        // 判断当天取消的次数 后台配置 redis 查询
        // 静默处理
        boolean isSilent = false;
        Integer num = xpOrderClient.todayCancellationCount(Long.valueOf(uid)).getData();
        if (null != num && num >= cancelCount) {
            isSilent = true;
        }
 
 
        // 获取起步里程
//        XpPricing xpPricing = this.baseMapper.selectOne(new LambdaQueryWrapper<XpPricing>().eq(XpPricing::getCityId, dto.getOpenCityId()));
//        Integer startMileage = XpPricingUtil.getStartMileage(xpPricing.getStartingFareJson());
//        if (startMileage == null) {
//            throw new ServiceException("当前区域未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
//        }
 
 
        //判断是否在白名单且优先派单
        boolean isPriorityDispatch = false;
        UcUserInfo ucUserInfo = userResourcesClient.getBaseUserByUid(uid);
        if (null != ucUserInfo) {
            Long count = xpWhitelistMapper.selectCount(new LambdaQueryWrapper<XpWhitelist>().eq(XpWhitelist::getAccount, ucUserInfo.getMobile()).eq(XpWhitelist::getIsPriorityDispatch, 1));
            if (count > 0) {
                isPriorityDispatch = true;
            }
        }
 
        // 生成订单数据
        XpOrder xpOrder = new XpOrder();
        xpOrder.setMileageKilometers(0.0);
        xpOrder.setAppUserId(Long.valueOf(uid));
        xpOrder.setOrderNumber("XP" + System.currentTimeMillis());
        xpOrder.setPlacementLon(dto.getPlacementLon());
        xpOrder.setPlacementLat(dto.getPlacementLat());
        xpOrder.setPlacementAddress(dto.getPlacementAddress());
 
        xpOrder.setCityId(dto.getOpenCityId());
        xpOrder.setXpElectronicFenceAreaId(dto.getXpElectronicFenceAreaId());
 
        xpOrder.setStartPointId(dto.getStartPointId());
        xpOrder.setStartLon(xpPoint.getPointLon());
        xpOrder.setStartLat(xpPoint.getPointLat());
        xpOrder.setBoardingLon(xpPoint.getPointLon());
        xpOrder.setBoardingLat(xpPoint.getPointLat());
        xpOrder.setBoardingAddress(xpPoint.getPointAddress());
        xpOrder.setStartAddress(xpPoint.getPointAddress());
        xpOrder.setStartPointName(xpPoint.getPointName());
 
        xpOrder.setEndPointId(dto.getEndPointId());
        xpOrder.setEndLon(endXpPoint.getPointLon());
        xpOrder.setEndLat(endXpPoint.getPointLat());
        xpOrder.setEndAddress(endXpPoint.getPointAddress());
        xpOrder.setEndPointName(endXpPoint.getPointName());
        xpOrder.setXpuStartTaskId(snowflake.nextId() + "");
        xpOrder.setXpuEndTaskId(snowflake.nextId() + "");
 
 
        // 当前时段
        String currentTimeSlot = DateUtils.getCurrentTimeSlot();
        xpOrder.setTimeSlot(currentTimeSlot);
        xpOrder.setCreateTime(LocalDateTime.now());
 
 
        XpOpenCity openCities = xpOpenCityMapper.selectById(dto.getOpenCityId());
        if (openCities == null) {
            throw new ServiceException("城市暂未开通服务", I18nKeyConstant.CITY_NOT_FOUND);
        }
        XpOpenCity xpOpenCity = openCities;
        dto.setOpenCityId(xpOpenCity.getId());
        xpOrder.setCityCode(xpOpenCity.getCityCode());
        XpUserRoutePlanRule xpUserRoutePlanRule = userRoutePlanRuleClient.getXpUserRoutePlanRule(uid).getData();
        XpRoutePlan xpRoutePlan = null;
        if(null != xpUserRoutePlanRule){
            xpRoutePlan = xpRoutePlanMapper.selectOne(new LambdaQueryWrapper<XpRoutePlan>().eq(XpRoutePlan::getAuditStatus, 2)
                    .eq(XpRoutePlan::getCityCode, xpOpenCity.getCityCode()).eq(XpRoutePlan::getRoutePlanRule, xpUserRoutePlanRule.getRoutePlanRule()));
            xpRoutePlanMapper.selectById(xpUserRoutePlanRule.getRoutePlanRule());
        }else{
            xpRoutePlan = xpRoutePlanMapper.selectOne(new LambdaQueryWrapper<XpRoutePlan>().eq(XpRoutePlan::getAuditStatus, 2)
                    .eq(XpRoutePlan::getCityCode, xpOpenCity.getCityCode()).last(" limit 1"));
        }
 
        Integer distance = 0;
        Integer duration = 0;
        if (dto.getNavigationSnapshotId() != null && dto.getSelectedRouteId() != null) {
            String snap = redisService.getCacheObject(CacheConstants.ORDER_MULTI_ROUTE_SNAPSHOT + uid + ":" + dto.getNavigationSnapshotId());
            if (snap != null) {
                MultiRouteSnapshotPayload snapPayload = JSON.parseObject(snap, MultiRouteSnapshotPayload.class);
                RouteOptionVo opt = snapPayload.getRoutes().stream()
                        .filter(r2 -> dto.getSelectedRouteId().equals(r2.getPathFingerprint()))
                        .findFirst()
                        .orElse(null);
                if (opt != null && opt.getRoute() != null) {
                    distance = opt.getRoute().getDistance();
                    duration = opt.getRoute().getDuration();
                    redisService.setCacheObject("ORDER_NAVIGATION_END_DATA:" + uid, JSON.toJSONString(opt.getRoute()), 24L, TimeUnit.HOURS);
                    if (dto.getSelectedRoutePlanRule() == null) {
                        dto.setSelectedRoutePlanRule(opt.getPrimaryRoutePlanRule());
                    }
                }
            }
        }
        if (distance == 0 && duration == 0) {
            Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_END_DATA:" + uid);
            if (cacheObject != null) {
                RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class);
                distance = routeVo.getDistance();
                duration = routeVo.getDuration();
            }
        }
        final Integer planRuleForQueue = dto.getSelectedRoutePlanRule() != null ? dto.getSelectedRoutePlanRule()
                : (xpRoutePlan != null ? xpRoutePlan.getRoutePlanRule() : null);
        Long couponRef = dto.getUserCouponId();
        xpOrder.setCouponId(couponRef != null && couponRef > 0L ? couponRef : null);
        // 产生订单
        R<Long> addOrderR = xpOrderClient.addOrder(xpOrder);
        if (addOrderR.getCode() != R.SUCCESS) {
            throw new ServiceException("订单生成失败", I18nKeyConstant.ORDER_CREATE_FAIL);
        }
        Long orderId = addOrderR.getData();
        xpOrder.setId(orderId);
 
        // 静默处理 不推单
        if (isSilent) {
            log.info("订单取消次数达到上限,执行静默处理,不推单。");
            return orderId.toString();
        }
 
        // 最大距离
        Object maxPushDistanceObj = redisService.getCacheObject(CacheConstants.MAX_PUSH_DISTANCE);
        Double maxPushDistance = 0.0;
        if (maxPushDistanceObj != null) {
            maxPushDistance = Double.valueOf(maxPushDistanceObj.toString());
        } else {
            XpTradingStrategy xpTradingStrategy = xpTradingStrategyMapper.selectOne(null);
            List<XpTradingStrategyPush> xpTradingStrategyPushes = xpTradingStrategyPushMapper.selectList(new QueryWrapper<XpTradingStrategyPush>().eq("trading_strategy_id", xpTradingStrategy.getId()));
            long now = Long.parseLong(LocalDateTime.now().format(DateTimeFormatter.ofPattern("HHmm")));
            XpTradingStrategyPush xpTradingStrategyPush = xpTradingStrategyPushes.stream().filter(item -> {
                long start = Long.parseLong(item.getStartTime().replace(":", ""));
                long end = Long.parseLong(item.getEndTime().replace(":", ""));
                return now >= start && now < end;
            }).findFirst().orElse(null);
            maxPushDistance = null != xpTradingStrategyPush ? xpTradingStrategyPush.getFivePushDistance() : xpTradingStrategy.getFivePushDistance();
        }
 
        //获取定向服务配置
        R<List<XpTargetedServiceVehicleVo>> targetedServiceUserR = xpTargetedServiceClient.findListByUserIdAndCityCode(xpOrder.getAppUserId(), xpOrder.getCityCode());
        if(targetedServiceUserR.getCode() == R.SUCCESS && CollUtil.isNotEmpty(targetedServiceUserR.getData()) ){
            XpTargetedServiceVehicleVo xpTargetedServiceVehicleVo = targetedServiceUserR.getData().get(0);
            return addSpOrderPush(xpOrder, xpPoint, xpRoutePlan, maxPushDistance, xpTargetedServiceVehicleVo, orderId, currentTimeSlot, distance, duration, uid);
        }else{
            return addOrderPush(xpOrder, xpPoint, xpRoutePlan, maxPushDistance, isPriorityDispatch, orderId, currentTimeSlot, distance, duration, uid);
        }
    }
 
 
    /**
     * 构建普通订单推送数据
     * @param xpOrder
     * @param xpPoint
     * @param xpRoutePlan
     * @param maxPushDistance
     * @param isPriorityDispatch
     * @param orderId
     * @param currentTimeSlot
     * @param distance
     * @param duration
     * @param uid
     * @return
     */
    private String addOrderPush(XpOrder xpOrder, XpPoint xpPoint, XpRoutePlan xpRoutePlan, double maxPushDistance,
                                Boolean isPriorityDispatch, Long orderId, String currentTimeSlot, Integer distance, Integer duration, String uid){
        // 找出现在所有的队列 通过计算距离 算出目前应该放在那个队列  如果没有符合的新增一个队列  符合后看是否白名单账号 是则直接放在队列前端
        Set<String> quoteMatchName = redisService.getQuoteMatchName(CacheConstants.QUEUE_POINT_ID);
        log.info("当前队列:{}", quoteMatchName);
        for (String s : quoteMatchName) {
//            QUEUE_POINT_ID-1-30.233,120.233
            String lonLat = s.split("-")[2];
            // 和上车点进行判断是否<maxPushDistance
            double v = FastDistanceCalculator.distanceFast(Double.parseDouble(lonLat.split(",")[1]), Double.parseDouble(lonLat.split(",")[0]), xpPoint.getPointLat(), xpPoint.getPointLon());
            if (v < maxPushDistance) {
                String value = orderId + "_" + xpPoint.getPointLon() + "," + xpPoint.getPointLat() + "_0" + "_" + uid + "_" + xpOrder.getOrderNumber() + "_" + distance + "_" + duration + "_" + xpRoutePlan.getRoutePlanRule();
                if (isPriorityDispatch) {
                    // 头部  value 订单_经纬度_状态_用户id_订单编号_距离_时间
                    redisService.addToHead(s, value);
                    log.info("添加到队列头部:{} {}", s, value);
                } else {
                    // 尾部
                    redisService.addToTail(s, value);
                    log.info("添加到队列尾部:{} {}", s, value);
                }
                xpOrder.setQueueName(s);
                xpOrderClient.updateOrder(xpOrder);
 
                // 存储mongodb 数据
                XpGeoQueryEntity xpGeoQueryEntity = new XpGeoQueryEntity();
                xpGeoQueryEntity.setOrderId(orderId.toString());
                xpGeoQueryEntity.setLocation(new GeoJsonPoint(xpPoint.getPointLon(), xpPoint.getPointLat()));
                // 计算2个时间的秒数
                xpGeoQueryEntity.setTime(currentTimeSlot);
                xpGeoQueryEntity.setStatus(0);
                xpGeoQueryEntity.setTimestamp(System.currentTimeMillis());
                geoQueryEntityService.save(xpGeoQueryEntity);
 
                return orderId.toString();
            }
        }
 
        String key = CacheConstants.QUEUE_POINT_ID + "-" + xpPoint.getId() + "-" + xpPoint.getPointLon() + "," + xpPoint.getPointLat();
        if(xpRoutePlan==null){
            redisService.setCacheList(key, Arrays.asList(orderId + "_" + xpPoint.getPointLon() + "," + xpPoint.getPointLat() + "_0" + "_" + uid + "_" + xpOrder.getOrderNumber() + "_" + distance + "_" + duration + "_" + null));
 
        }else {
            redisService.setCacheList(key, Arrays.asList(orderId + "_" + xpPoint.getPointLon() + "," + xpPoint.getPointLat() + "_0" + "_" + uid + "_" + xpOrder.getOrderNumber() + "_" + distance + "_" + duration + "_" + xpRoutePlan.getRoutePlanRule()));
        }
        log.info("订单推单:{}   {}", key, redisService.getCacheList(key));
        xpOrder.setQueueName(key);
        xpOrderClient.updateOrder(xpOrder);
 
 
        // 存储mongodb 数据
        XpGeoQueryEntity xpGeoQueryEntity = new XpGeoQueryEntity();
        xpGeoQueryEntity.setOrderId(orderId.toString());
        xpGeoQueryEntity.setLocation(new GeoJsonPoint(xpPoint.getPointLon(), xpPoint.getPointLat()));
        // 计算2个时间的秒数
        xpGeoQueryEntity.setTime(currentTimeSlot);
        xpGeoQueryEntity.setStatus(0);
        xpGeoQueryEntity.setTimestamp(System.currentTimeMillis());
        geoQueryEntityService.save(xpGeoQueryEntity);
 
        return orderId.toString();
    }
 
    /**
     * 构建定向服务订单推送数据
     * @param xpOrder
     * @param xpPoint
     * @param xpRoutePlan
     * @param maxPushDistance
     * @param xpTargetedServiceVehicleVo
     * @param orderId
     * @param currentTimeSlot
     * @param distance
     * @param duration
     * @param uid
     * @return
     */
    private String addSpOrderPush(XpOrder xpOrder, XpPoint xpPoint, XpRoutePlan xpRoutePlan, double maxPushDistance,
                                  XpTargetedServiceVehicleVo xpTargetedServiceVehicleVo, Long orderId, String currentTimeSlot, Integer distance, Integer duration, String uid){
        // 找出现在所有的队列 通过计算距离 算出目前应该放在那个队列  如果没有符合的新增一个队列  符合后看是否白名单账号 是则直接放在队列前端
        Set<String> quoteMatchName = redisService.getQuoteMatchName(CacheConstants.SP_QUEUE_POINT_ID);
        log.info("当前队列:{}", quoteMatchName);
 
        for (String s : quoteMatchName) {
//            QUEUE_POINT_ID-1-30.233,120.233
            String lonLat = s.split("-")[2];
            // 和上车点进行判断是否<maxPushDistance
            double v = FastDistanceCalculator.distanceFast(Double.parseDouble(lonLat.split(",")[1]), Double.parseDouble(lonLat.split(",")[0]), xpPoint.getPointLat(), xpPoint.getPointLon());
            if (v < maxPushDistance) {
                //在范围内,将队列中的数据转换成对象进行排序
                List<String> cacheList = redisService.getCacheList(s);
                List<RedisQueuePointIdVo> ll = new ArrayList<>();
                for (String string : cacheList) {
                    String[] split = string.split("_");
                    RedisQueuePointIdVo redisQueuePointIdVo = new RedisQueuePointIdVo();
                    redisQueuePointIdVo.setOrderId(split[0]);
                    redisQueuePointIdVo.setPointLonLat(split[1]);
                    redisQueuePointIdVo.setPushStatus(split[2]);
                    redisQueuePointIdVo.setUid(split[3]);
                    redisQueuePointIdVo.setOrderNumber(split[4]);
                    redisQueuePointIdVo.setDistance(split[5]);
                    redisQueuePointIdVo.setDuration(split[6]);
                    redisQueuePointIdVo.setPlanRuleForQueue(split[7]);
                    redisQueuePointIdVo.setTargetedVal(Integer.valueOf(split[8]));
                    ll.add(redisQueuePointIdVo);
                }
 
                RedisQueuePointIdVo redisQueuePointIdVo = new RedisQueuePointIdVo();
                redisQueuePointIdVo.setOrderId(orderId.toString());
                redisQueuePointIdVo.setPointLonLat(xpPoint.getPointLon() + "," + xpPoint.getPointLat());
                redisQueuePointIdVo.setPushStatus("0");
                redisQueuePointIdVo.setUid(uid);
                redisQueuePointIdVo.setOrderNumber(xpOrder.getOrderNumber());
                redisQueuePointIdVo.setDistance(distance.toString());
                redisQueuePointIdVo.setDuration(duration.toString());
                redisQueuePointIdVo.setPlanRuleForQueue(xpRoutePlan.getRoutePlanRule().toString());
                redisQueuePointIdVo.setTargetedVal(xpTargetedServiceVehicleVo.getWeight());
                ll.add(redisQueuePointIdVo);
                //排序
                ll.sort((o1, o2) -> o2.getTargetedVal() - o1.getTargetedVal());
                List<String> collect = ll.stream().map(item -> {
                    String value = item.getOrderId() + "_" + item.getPointLonLat() + "_" + item.getPushStatus() + "_" + item.getUid() + "_" + item.getOrderNumber()
                            + "_" + item.getDistance() + "_" + item.getDuration() + "_" + item.getPlanRuleForQueue() + "_" + item.getTargetedVal();
                    return value;
                }).collect(Collectors.toList());
                redisService.setCacheList(s, cacheList);
 
                xpOrder.setQueueName(s);
                xpOrderClient.updateOrder(xpOrder);
 
                // 存储mongodb 数据
                XpGeoQueryEntity xpGeoQueryEntity = new XpGeoQueryEntity();
                xpGeoQueryEntity.setOrderId(orderId.toString());
                xpGeoQueryEntity.setLocation(new GeoJsonPoint(xpPoint.getPointLon(), xpPoint.getPointLat()));
                // 计算2个时间的秒数
                xpGeoQueryEntity.setTime(currentTimeSlot);
                xpGeoQueryEntity.setStatus(0);
                xpGeoQueryEntity.setTimestamp(System.currentTimeMillis());
                geoQueryEntityService.save(xpGeoQueryEntity);
 
                return orderId.toString();
            }
        }
 
        String key = CacheConstants.SP_QUEUE_POINT_ID + "-" + xpPoint.getId() + "-" + xpPoint.getPointLon() + "," + xpPoint.getPointLat();
        if(xpRoutePlan==null){
            redisService.setCacheList(key, Arrays.asList(orderId + "_" + xpPoint.getPointLon() + "," + xpPoint.getPointLat() + "_0" + "_" + uid + "_" + xpOrder.getOrderNumber() + "_" + distance + "_" + duration + "_" + null + "_" + xpTargetedServiceVehicleVo.getWeight()));
        }else {
            redisService.setCacheList(key, Arrays.asList(orderId + "_" + xpPoint.getPointLon() + "," + xpPoint.getPointLat() + "_0" + "_" + uid + "_" + xpOrder.getOrderNumber() + "_" + distance + "_" + duration + "_" + xpRoutePlan.getRoutePlanRule() + "_" + xpTargetedServiceVehicleVo.getWeight()));
        }
        log.info("订单推单:{}   {}", key, redisService.getCacheList(key));
        xpOrder.setQueueName(key);
        xpOrderClient.updateOrder(xpOrder);
 
 
        // 存储mongodb 数据
        XpGeoQueryEntity xpGeoQueryEntity = new XpGeoQueryEntity();
        xpGeoQueryEntity.setOrderId(orderId.toString());
        xpGeoQueryEntity.setLocation(new GeoJsonPoint(xpPoint.getPointLon(), xpPoint.getPointLat()));
        // 计算2个时间的秒数
        xpGeoQueryEntity.setTime(currentTimeSlot);
        xpGeoQueryEntity.setStatus(0);
        xpGeoQueryEntity.setTimestamp(System.currentTimeMillis());
        geoQueryEntityService.save(xpGeoQueryEntity);
 
        return orderId.toString();
    }
 
 
 
 
    @Override
    public Boolean isInOperationTime(String operationStrategy) {
        JSONArray jsonArray = JSONArray.parseArray(operationStrategy);
        Integer holidayToday = holidayUtil.isHolidayToday();
        for (Object o : jsonArray) {
            JSONObject jsonObject = JSONObject.parseObject(o.toString());
            String type = jsonObject.getString("type");
            if (type.equals(holidayToday.toString())) {
                JSONArray time = jsonObject.getJSONArray("time");
                // 判断当前时间 是否在time时间段中
                for (Object object : time) {
                    String[] timeArray = object.toString().split("-");
                    String startTime = timeArray[0];
                    String endTime = timeArray[1];
                    boolean currentTimeInRange = DateUtils.isCurrentTimeInRange(startTime, endTime);
                    if (currentTimeInRange) {
                        return true;
                    }
                }
            } else if (type.equals("3")) {
                // 判断是什么类型 0每天  1-7 想起
                String typeStr = jsonObject.getString("typeStr");
                // 查看今天周几
                Integer week = DateUtils.getWeek();
                // 包含每天 或者  包含当天
                if (typeStr.contains("0") || typeStr.contains(week.toString())) {
                    JSONArray time = jsonObject.getJSONArray("time");
                    // 判断当前时间 是否在time时间段中
                    for (Object object : time) {
                        String[] timeArray = object.toString().split("-");
                        String startTime = timeArray[0];
                        String endTime = timeArray[1];
                        boolean currentTimeInRange = DateUtils.isCurrentTimeInRange(startTime, endTime);
                        if (currentTimeInRange) {
                            return true;
                        }
                    }
                }
 
            }
        }
        return false;
    }
 
 
    /**
     * 单次路径计价(不含区域诊断、不写导航缓存),供多路线合并调用。
     */
    private RouteAndFareVo pricingForRoute(RouteAndFareDto routeAndFareDto, RouteVo routeVo, XpPricing xpPricing, String userId) {
        XpPricingCardVo xpPricingCardVo;
        if (xpPricing.getPricingType() == 1) {
            xpPricingCardVo = xpPricingUtil.calculationPrice(userId,
                    JSON.parseObject(xpPricing.getBasicPricingJson()),
                    JSON.parseArray(xpPricing.getStartingFareJson()),
                    routeVo.getDistance(), routeVo.getDuration(), 0, 0.0, xpPricing.getIsDynamicPrice(), routeAndFareDto.getOpenCityId(), xpPricing.getDynamicPricingJson()
            );
        } else {
            xpPricingCardVo = xpPricingUtil.calculationFixedPrice(userId,
                    JSON.parseObject(xpPricing.getBasicPricingJson()),
                    JSON.parseArray(xpPricing.getStartingFareJson()),
                    routeVo.getDistance(), routeVo.getDuration(), 0, 0.0, xpPricing.getIsDynamicPrice(), routeAndFareDto.getOpenCityId(), xpPricing.getDynamicPricingJson()
            );
        }
        R<UserCouponVo> coupon = null;
        CompletableFuture<R<UserCouponVo>> couponFuture = null;
        Long ucId = routeAndFareDto.getUserCouponId();
        if (Long.valueOf(0L).equals(ucId)) {
            coupon = R.ok(new UserCouponVo());
        } else if (ucId != null && ucId > 0L) {
            coupon = xpAppUserCouponClient.getUserCoupon(
                    new GetUserCouponDto(Long.valueOf(userId), xpPricingCardVo.getTotalPrice(), routeAndFareDto.getOpenCityId(), ucId));
            if (coupon.getCode() == 200 && coupon.getData().getStatus() == 2) {
                throw new ServiceException("优惠券不满足条件", I18nKeyConstant.COUPON_NOT_SATISFY);
            }
        } else {
            couponFuture = CompletableFuture.supplyAsync(
                    () -> xpAppUserCouponClient.getUserCoupon(
                            new GetUserCouponDto(Long.valueOf(userId), xpPricingCardVo.getTotalPrice(), routeAndFareDto.getOpenCityId(), null)),
                    executorService);
        }
        R<UserCouponVo> couponResult;
        if (Long.valueOf(0L).equals(ucId) || (ucId != null && ucId > 0L)) {
            couponResult = coupon;
        } else {
            couponResult = couponFuture.join();
        }
        Integer minOrderDistance;
        Object minOrderDistanceObj = redisService.getCacheObject(CacheConstants.MIN_ORDER_DISTANCE);
        if (minOrderDistanceObj != null) {
            minOrderDistance = Integer.valueOf(minOrderDistanceObj.toString());
        } else {
            minOrderDistance = systemSettingMapper.selectOne(null).getMinOrderDistance();
        }
        if (routeVo.getDistance() < minOrderDistance) {
            throw new ServiceException("当前上下车点距离过近,建议步行前往", I18nKeyConstant.TO_WORK);
        }
        if (couponResult.getCode() != R.SUCCESS) {
            throw new RuntimeException(couponResult.getMsg());
        }
        UserCouponVo userCouponVo = couponResult.getData();
        if (userCouponVo != null) {
            Double money = userCouponVo.getMoney();
            money = new BigDecimal(money).setScale(2, RoundingMode.HALF_UP).doubleValue();
            xpPricingCardVo.setCoupon(String.valueOf(money));
            if (xpPricingCardVo.getTotalPrice() > money) {
                xpPricingCardVo.setSystemDiscount(xpPricingCardVo.getTotalPrice() - money);
            }
        }
        RouteAndFareVo routeAndFareVo = new RouteAndFareVo();
        routeAndFareVo.setRoute(routeVo);
        xpPricingCardVo.setTotalPriceStr(new BigDecimal(xpPricingCardVo.getTotalPrice()).setScale(2, RoundingMode.HALF_UP).toString());
        xpPricingCardVo.setStartPriceStr(new BigDecimal(xpPricingCardVo.getStartPrice()).setScale(2, RoundingMode.HALF_UP).toString());
        xpPricingCardVo.setDistancePriceStr(new BigDecimal(xpPricingCardVo.getDistancePrice()).setScale(2, RoundingMode.HALF_UP).toString());
        xpPricingCardVo.setDurationPriceStr(new BigDecimal(xpPricingCardVo.getDurationPrice()).setScale(2, RoundingMode.HALF_UP).toString());
        xpPricingCardVo.setSystemDiscountStr(new BigDecimal(xpPricingCardVo.getSystemDiscount()).setScale(2, RoundingMode.HALF_UP).toString());
        routeAndFareVo.setXpPricingCard(xpPricingCardVo);
        FareVo fareVo = new FareVo();
        fareVo.setEstimatedDistanceKm(routeVo.getDistance().doubleValue() / 1000);
        fareVo.setEstimatedDurationMin(routeVo.getDuration() / 60);
        fareVo.setOriginalFare(xpPricingCardVo.getTotalPrice());
        fareVo.setOriginalFareStr(new BigDecimal(fareVo.getOriginalFare()).setScale(2, RoundingMode.HALF_UP).toString());
        if (userCouponVo != null) {
            if (userCouponVo.getMoney() > fareVo.getOriginalFare()) {
                fareVo.setDiscountAmount(fareVo.getOriginalFare());
                fareVo.setFinalFare(0.00);
            } else {
                fareVo.setDiscountAmount(userCouponVo.getMoney());
                fareVo.setFinalFare(fareVo.getOriginalFare() - fareVo.getDiscountAmount());
            }
            if (fareVo.getFinalFare() > 0) {
                fareVo.setSystemDiscount(fareVo.getFinalFare());
                fareVo.setFinalFare(0.00);
            }
            fareVo.setCouponId(userCouponVo.getId());
        } else {
            fareVo.setDiscountAmount(0.00);
            fareVo.setFinalFare(fareVo.getOriginalFare());
            fareVo.setSystemDiscount(fareVo.getFinalFare());
            fareVo.setFinalFare(0.00);
        }
        Integer waitTime = getWaitTime(routeAndFareDto.getOrigin().getLongitude(), routeAndFareDto.getOrigin().getLatitude());
        fareVo.setWaitingTime(waitTime);
        fareVo.setFinalFareStr(new BigDecimal(fareVo.getFinalFare()).setScale(2, RoundingMode.HALF_UP).toString());
        fareVo.setOriginalFare(new BigDecimal(fareVo.getOriginalFare()).setScale(2, RoundingMode.HALF_UP).doubleValue());
        fareVo.setSystemDiscount(new BigDecimal(fareVo.getSystemDiscount() == null ? 0 : fareVo.getSystemDiscount()).setScale(2, RoundingMode.HALF_UP).doubleValue());
        routeAndFareVo.setFareEstimate(fareVo);
        routeAndFareVo.getXpPricingCard().setFinalFareStr(fareVo.getFinalFareStr());
//        routeAndFareVo.setTrafficLights();//交通信号灯 todo 20260508 红绿灯功能还没有
        fareVo.setId(null);
        return routeAndFareVo;
    }
 
    @Override
    public MultiRouteAndFareVo getMultiRouteAndFare(RouteAndFareDto routeAndFareDto, String userId) {
        Map<String, String> geocode = gdMapGeocodingUtil.geocode(routeAndFareDto.getOrigin().getLongitude().toString(), routeAndFareDto.getOrigin().getLatitude().toString());
        if (null == geocode) {
            throw new ServiceException("获取城市信息失败", I18nKeyConstant.CITY_NOT_FOUND);
        }
        Long cityCode = routeAndFareDto.getOpenCityId();
        XpOpenCity openCities = xpOpenCityMapper.selectById(cityCode);
        if (openCities == null) {
            throw new ServiceException("获取城市信息失败", I18nKeyConstant.CITY_NOT_FOUND);
        }
        routeAndFareDto.setOpenCityId(openCities.getId());
 
        XpPricing xpPricing = this.baseMapper.selectOne(new LambdaQueryWrapper<XpPricing>().eq(XpPricing::getCityId, routeAndFareDto.getOpenCityId()));
        if (xpPricing == null) {
            throw new ServiceException("当前区域未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
        }
        R<Boolean> diagnosisResult = xpMapClient.diagnosisArea(routeAndFareDto);
        if (diagnosisResult.getCode() != R.SUCCESS) {
            throw new RuntimeException(diagnosisResult.getMsg());
        }
        if (!Boolean.TRUE.equals(diagnosisResult.getData())) {
            throw new ServiceException("当前区域未开通服务", I18nKeyConstant.NO_CURRENT_REGION);
        }
 
        StrategyPlanningConfig planningConfig = this.buildStrategyKindsForPlanning(userId, routeAndFareDto.getOpenCityId());
        List<RouteStrategyKind> kinds = planningConfig.getKinds();
        Map<String, RouteVo> routeById = new LinkedHashMap<>();
        Map<String, Set<RouteStrategyKind>> strategiesById = new LinkedHashMap<>();
        for (RouteStrategyKind kind : kinds) {
            RouteAndFareDto copy = new RouteAndFareDto();
            copy.setOrigin(routeAndFareDto.getOrigin());
            copy.setDestination(routeAndFareDto.getDestination());
            copy.setRoutePlanRule(kind.getAmapStrategy());
            R<RouteVo> r = xpMapClient.getDistanceAndDuration(copy);
            if (r.getCode() != 200 || r.getData() == null || r.getData().getRouteID() == null) {
                continue;
            }
            String fid = r.getData().getRouteID();
            routeById.putIfAbsent(fid, r.getData());
            strategiesById.computeIfAbsent(fid, k -> new LinkedHashSet<>()).add(kind);
        }
        if (routeById.isEmpty()) {
            throw new ServiceException("获取导肮信息失败", I18nKeyConstant.PATH_NOT_FOUND);
        }
 
        XpSystemSetting xpSystemSetting = systemSettingMapper.selectOne(null);
 
        Integer operateFree = 0 ;
        if(null != xpSystemSetting) {
            operateFree = xpSystemSetting.getOperateFree();
        }
 
        List<RouteOptionVo> options = new ArrayList<>();
        for (Map.Entry<String, RouteVo> e : routeById.entrySet()) {
            String fid = e.getKey();
            RouteVo rv = e.getValue();
            Set<RouteStrategyKind> kindsOnRoute = strategiesById.get(fid);
            RouteStrategyKind primary = RouteStrategyKind.primaryOf(kindsOnRoute);
            RouteAndFareDto pricingDto = new RouteAndFareDto();
            pricingDto.setOrigin(routeAndFareDto.getOrigin());
            pricingDto.setDestination(routeAndFareDto.getDestination());
            pricingDto.setOpenCityId(routeAndFareDto.getOpenCityId());
            pricingDto.setUserCouponId(routeAndFareDto.getUserCouponId());
            pricingDto.setVehicleId(routeAndFareDto.getVehicleId());
            pricingDto.setRoutePlanRule(primary.getAmapStrategy());
            RouteAndFareVo part = pricingForRoute(pricingDto, rv, xpPricing, userId);
            RouteOptionVo opt = new RouteOptionVo();
            opt.setPathFingerprint(fid);
            List<String> labels = RouteStrategyKind.orderedOf(kindsOnRoute).stream()
                    .map(kind -> resolveStrategyDisplayName(kind, planningConfig.getDisplayNames()))
                    .collect(Collectors.toList());
            opt.setStrategyLabels(labels);
            opt.setPrimaryStrategyLabel(resolveStrategyDisplayName(primary, planningConfig.getDisplayNames()));
            opt.setPrimaryRoutePlanRule(primary.getAmapStrategy());
            opt.setPrimaryRoutePlanName(primary.getDisplayName());
            opt.setRoute(part.getRoute());
            opt.setFareEstimate(part.getFareEstimate());
            opt.setXpPricingCard(part.getXpPricingCard());
            if (part.getFareEstimate() != null && part.getFareEstimate().getCouponId() == null) {
                opt.setCouponMessage("当前路线不满足优惠券使用条件");
            }
            opt.setOperateFree(operateFree);
            options.add(opt);
        }
 
        // 根据 pathFingerprint 去重,保留第一次出现的路线
//        options = new ArrayList<>(options.stream()
//                .collect(Collectors.toMap(
//                        RouteOptionVo::getPathFingerprint,
//                        option -> option,
//                        (existing, replacement) -> existing,
//                        LinkedHashMap::new
//                )).values());
 
        RouteStrategyKind userPref = resolveUserPreferredKind(userId).orElse(RouteStrategyKind.SMART);
        options.sort((a, b) -> compareRouteOptionByUserPreference(a, b, userPref, strategiesById));
 
        String snapshotId = UUID.randomUUID().toString().replace("-", "");
        long now = System.currentTimeMillis();
        MultiRouteSnapshotPayload payload = new MultiRouteSnapshotPayload();
        payload.setSnapshotId(snapshotId);
        payload.setGeneratedAtMillis(now);
        payload.setUserId(userId);
        payload.setOpenCityId(routeAndFareDto.getOpenCityId());
        payload.setOrigin(routeAndFareDto.getOrigin());
        payload.setDestination(routeAndFareDto.getDestination());
        payload.setUserCouponId(routeAndFareDto.getUserCouponId());
        payload.setRoutes(options);
 
        R<XpTradingStrategyDetailVO>  strategyDetailVOR = xpTradingStrategyClient.queryById();
        XpTradingStrategyDetailVO strategyDetailVO = strategyDetailVOR.getData();
        redisService.setCacheObject(CacheConstants.ORDER_MULTI_ROUTE_SNAPSHOT + userId + ":" + snapshotId, JSON.toJSONString(payload), Long.valueOf(strategyDetailVO.getMaxStandingTiming()), TimeUnit.SECONDS);
 
 
        MultiRouteAndFareVo vo = new MultiRouteAndFareVo();
        vo.setSnapshotId(snapshotId);
        vo.setGeneratedAtMillis(now);
        vo.setShowRouteSelector(options.size() >= 2);
        vo.setRoutes(options);
        vo.setRoutePreferences(listRoutePreferences(routeAndFareDto.getOpenCityId(), userId));
        vo.setStaleCheckSeconds(navigationStaleSeconds);
        vo.setPriceWaveThresholdPercent(navigationPriceWavePercent);
        vo.setOperateFree(operateFree);
 
 
        RouteOptionVo first = options.get(0);
        redisService.setCacheObject("ORDER_NAVIGATION_END_DATA:" + userId, JSON.toJSONString(first.getRoute()), 24L, TimeUnit.HOURS);
        return vo;
    }
    @Override
    public void selectNavigationRoute(SelectNavigationRouteDto dto, String userId) {
        String json = redisService.getCacheObject(CacheConstants.ORDER_MULTI_ROUTE_SNAPSHOT + userId + ":" + dto.getSnapshotId());
        if (json == null) {
            throw new ServiceException("路线已失效,请重新规划", I18nKeyConstant.PATH_NOT_FOUND);
        }
        MultiRouteSnapshotPayload payload = JSON.parseObject(json, MultiRouteSnapshotPayload.class);
        RouteOptionVo selected = payload.getRoutes().stream()
                .filter(r -> dto.getPathFingerprint().equals(r.getPathFingerprint()))
                .findFirst()
                .orElseThrow(() -> new ServiceException("未找到对应路线", I18nKeyConstant.PATH_NOT_FOUND));
        redisService.setCacheObject("ORDER_NAVIGATION_END_DATA:" + userId, JSON.toJSONString(selected.getRoute()), 24L, TimeUnit.HOURS);
        String id = CacheConstants.ORD + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0, 8);
        if (selected.getXpPricingCard() != null) {
            redisService.setCacheObject(CacheConstants.ORD + id + userId, selected.getXpPricingCard(), 1L, TimeUnit.DAYS);
        }
        if (selected.getFareEstimate() != null) {
            selected.getFareEstimate().setId(id);
        }
    }
 
    @Override
    public RouteStaleCheckVo checkRouteStale(RouteStaleCheckDto dto, String userId) {
        RouteStaleCheckVo out = new RouteStaleCheckVo();
        out.setNeedRefreshNotice(false);
        long elapsed = System.currentTimeMillis() - dto.getClientSnapshotMillis();
        if (elapsed <= navigationStaleSeconds * 1000L) {
            return out;
        }
        String snapJson = redisService.getCacheObject(CacheConstants.ORDER_MULTI_ROUTE_SNAPSHOT + userId + ":" + dto.getSnapshotId());
        if (snapJson == null) {
            out.setNeedRefreshNotice(true);
            out.setNoticeMessage("路况已变,价格已更新");
            out.setReplacedSnapshot(true);
            return out;
        }
        MultiRouteSnapshotPayload oldSnap = JSON.parseObject(snapJson, MultiRouteSnapshotPayload.class);
        RouteAndFareDto refreshDto = new RouteAndFareDto();
        refreshDto.setOrigin(oldSnap.getOrigin());
        refreshDto.setDestination(oldSnap.getDestination());
        refreshDto.setOpenCityId(oldSnap.getOpenCityId());
        refreshDto.setUserCouponId(oldSnap.getUserCouponId());
        MultiRouteAndFareVo fresh = getMultiRouteAndFare(refreshDto, userId);
        out.setLatestMultiRoute(fresh);
        if (dto.getClientFinalFare() == null || fresh.getRoutes() == null) {
            return out;
        }
        String fp = dto.getPathFingerprint();
        RouteOptionVo newOpt = fresh.getRoutes().stream()
                .filter(r -> fp == null || fp.equals(r.getPathFingerprint()))
                .findFirst()
                .orElse(fresh.getRoutes().get(0));
        Double nf = newOpt.getFareEstimate() != null ? newOpt.getFareEstimate().getFinalFare() : null;
        if (nf == null) {
            return out;
        }
        double diff = Math.abs(nf - dto.getClientFinalFare()) / Math.max(1e-6, dto.getClientFinalFare()) * 100.0;
        if (diff > navigationPriceWavePercent) {
            out.setNeedRefreshNotice(true);
            out.setNoticeMessage("路况已变,价格已更新");
            out.setReplacedSnapshot(true);
        }
        return out;
    }
 
    @Override
    public List<RoutePreferenceItemVo> listRoutePreferences(Long openCityId, String userId) {
        Set<String> enabledCodes = parseEnabledPreferenceCodes();
        Optional<RouteStrategyKind> savedPref = resolveUserPreferredKind(userId);//获取用户设置的路线偏好
        List<XpRoutePlan> routePlans = findRoutePlans(openCityId);
 
        List<RoutePreferenceItemVo> list = new ArrayList<>();
        if(CollUtil.isNotEmpty(routePlans)){
            int i = 0;
            for (XpRoutePlan routePlan : routePlans) {
                RoutePreferenceItemVo v = new RoutePreferenceItemVo();
                RouteStrategyKind strategy = RouteStrategyKind.getStrategy(routePlan.getRoutePlanRule());
                if(strategy != null){
                    v.setCode(strategy.name());
                }
                v.setName(routePlan.getPlanName());
                v.setEnabled(true);
                v.setSortOrder(i++);
                v.setAmapStrategy(routePlan.getRoutePlanRule());
                v.setSelected(savedPref.isPresent() && strategy == savedPref.get());
                list.add(v);
            }
        }
        return list;
    }
 
    @Override
    public void saveUserRoutePreference(String code, String userId) {
        RouteStrategyKind k;
        try {
            k = RouteStrategyKind.valueOf(code.trim().toUpperCase(Locale.ROOT));
        } catch (Exception e) {
            throw new ServiceException("无效的路线偏好编码", I18nKeyConstant.PATH_NOT_FOUND);
        }
        redisService.setCacheObject(CacheConstants.USER_ROUTE_PREFERENCE + userId, String.valueOf(k.getAmapStrategy()), 365L, TimeUnit.DAYS);
    }
 
    private Set<String> parseEnabledPreferenceCodes() {
        Set<String> set = new LinkedHashSet<>();
        for (String part : navigationPreferenceEnabledCodes.split(",")) {
            String p = part.trim().toUpperCase(Locale.ROOT);
            if (!p.isEmpty()) {
                set.add(p);
            }
        }
        if (set.isEmpty()) {
            for (RouteStrategyKind kind : RouteStrategyKind.defaultPreferenceKinds()) {
                set.add(kind.name());
            }
        }
        return set;
    }
 
    private List<XpRoutePlan> findRoutePlans (Long openCityId){
        return xpRoutePlanMapper.selectList(new LambdaQueryWrapper<XpRoutePlan>().eq(XpRoutePlan::getOpenCityId, openCityId)
                .eq(XpRoutePlan::getAuditStatus, 2));
    }
 
    /**
     * 用户偏好策略优先请求,其余策略仍请求以便合并去重。
     */
    private StrategyPlanningConfig buildStrategyKindsForPlanning(String userId, Long openCityId) {
        List<XpRoutePlan> xpRoutePlans = xpRoutePlanMapper.selectList(new LambdaQueryWrapper<XpRoutePlan>().eq(XpRoutePlan::getOpenCityId, openCityId)
                .eq(XpRoutePlan::getAuditStatus, 2));
        Map<RouteStrategyKind, String> displayNames = new LinkedHashMap<>();
        List<RouteStrategyKind> configuredKinds = new ArrayList<>();
        if (xpRoutePlans.isEmpty()) {
            throw new ServiceException("未配置有效的导航策略", I18nKeyConstant.PATH_NOT_FOUND);
        } else {
            for (XpRoutePlan xpRoutePlan : xpRoutePlans) {
                Integer strategy = xpRoutePlan.getRoutePlanRule();
                if (strategy == null) {
                    continue;
                }
                RouteStrategyKind.fromAmapStrategy(strategy).ifPresent(kind -> {
                    configuredKinds.add(kind);
                    if (StringUtils.isNotBlank(xpRoutePlan.getPlanName())) {
                        displayNames.putIfAbsent(kind, xpRoutePlan.getPlanName().trim());
                    }
                });
            }
        }
        return new StrategyPlanningConfig(configuredKinds, displayNames);
    }
 
    private String resolveStrategyDisplayName(RouteStrategyKind kind, Map<RouteStrategyKind, String> displayNames) {
        String configuredName = displayNames.get(kind);
        return StringUtils.isNotBlank(configuredName) ? configuredName : kind.getDisplayName();
    }
 
    private void addIfAbsent(List<RouteStrategyKind> kinds, RouteStrategyKind kind) {
        if (!kinds.contains(kind)) {
            kinds.add(kind);
        }
    }
 
    private RouteStrategyKind resolveDefaultEnabledKind(Set<String> enabled) {
        for (RouteStrategyKind kind : RouteStrategyKind.defaultPreferenceKinds()) {
            if (enabled.contains(kind.name())) {
                return kind;
            }
        }
        for (RouteStrategyKind kind : RouteStrategyKind.orderedValues()) {
            if (enabled.contains(kind.name())) {
                return kind;
            }
        }
        return RouteStrategyKind.SMART;
    }
 
    private static final class StrategyPlanningConfig {
        private final List<RouteStrategyKind> kinds;
        private final Map<RouteStrategyKind, String> displayNames;
 
        private StrategyPlanningConfig(List<RouteStrategyKind> kinds, Map<RouteStrategyKind, String> displayNames) {
            this.kinds = kinds;
            this.displayNames = displayNames;
        }
 
        private List<RouteStrategyKind> getKinds() {
            return kinds;
        }
 
        private Map<RouteStrategyKind, String> getDisplayNames() {
            return displayNames;
        }
    }
 
    private Optional<RouteStrategyKind> resolveUserPreferredKind(String userId) {
        if (userId == null || userId.isEmpty()) {
            return Optional.empty();
        }
        Object raw = redisService.getCacheObject(CacheConstants.USER_ROUTE_PREFERENCE + userId);
        if (raw == null) {
            return Optional.empty();
        }
        try {
            int strategy = Integer.parseInt(raw.toString());
            return RouteStrategyKind.fromAmapStrategy(strategy);
        } catch (Exception e) {
            return Optional.empty();
        }
    }
 
    private int compareRouteOptionByUserPreference(RouteOptionVo a, RouteOptionVo b, RouteStrategyKind pref,
                                                   Map<String, Set<RouteStrategyKind>> strategiesById) {
        Set<RouteStrategyKind> sa = strategiesById.getOrDefault(a.getPathFingerprint(), Collections.emptySet());
        Set<RouteStrategyKind> sb = strategiesById.getOrDefault(b.getPathFingerprint(), Collections.emptySet());
        boolean aHas = sa.contains(pref);
        boolean bHas = sb.contains(pref);
        if (aHas != bHas) {
            return bHas ? 1 : -1;
        }
        boolean aPrimary = a.getPrimaryRoutePlanRule() != null && a.getPrimaryRoutePlanRule() == pref.getAmapStrategy();
        boolean bPrimary = b.getPrimaryRoutePlanRule() != null && b.getPrimaryRoutePlanRule() == pref.getAmapStrategy();
        if (aPrimary != bPrimary) {
            return bPrimary ? 1 : -1;
        }
        int da = a.getRoute() != null && a.getRoute().getDuration() != null ? a.getRoute().getDuration() : Integer.MAX_VALUE;
        int db = b.getRoute() != null && b.getRoute().getDuration() != null ? b.getRoute().getDuration() : Integer.MAX_VALUE;
        return Integer.compare(da, db);
    }
 
    @Override
    public QueueStatusVo getQueueStatus(String orderId, String uid) {
        QueueStatusVo queueStatusVo = new QueueStatusVo();
 
        try {
            // 从订单服务获取订单信息,获取queueName
            R<XpOrder> orderR = xpOrderClient.queryOrderById(Long.valueOf(orderId));
            if (orderR.getCode() != R.SUCCESS || orderR.getData() == null) {
                throw new ServiceException("订单不存在");
            }
 
            XpOrder xpOrder = orderR.getData();
            String queueName = xpOrder.getQueueName();
 
            if (StringUtils.isBlank(queueName)) {
                // 订单不在排队中
                queueStatusVo.setStatus(1);
                queueStatusVo.setCurrentPosition(0);
                queueStatusVo.setTotalCount(0);
                queueStatusVo.setEstimatedWaitTime(0);
                return queueStatusVo;
            }
 
            // 从Redis获取队列数据
            List<String> queueItems = redisService.getCacheList(queueName);
            if (queueItems == null || queueItems.isEmpty()) {
                // 队列为空,说明已分配车辆
                queueStatusVo.setStatus(1);
                queueStatusVo.setCurrentPosition(0);
                queueStatusVo.setTotalCount(0);
                queueStatusVo.setEstimatedWaitTime(0);
                return queueStatusVo;
            }
 
            // 遍历队列,找到当前订单的位置
            int currentPosition = 0;
            int totalCount = queueItems.size();
 
            for (int i = 0; i < queueItems.size(); i++) {
                String item = queueItems.get(i);
                if (item.startsWith(orderId + "_")) {
                    currentPosition = i + 1; // 位置从1开始
                    break;
                }
            }
 
            Integer waitTimeTimeSlot = getWaitTimeTimeSlot(xpOrder.getStartLon(), xpOrder.getStartLat(), xpOrder.getTimeSlot());
            queueStatusVo.setStatus(0); // 0-排队中
            queueStatusVo.setCurrentPosition(currentPosition);
            queueStatusVo.setTotalCount(totalCount);
            queueStatusVo.setEstimatedWaitTime((waitTimeTimeSlot*currentPosition)/60);
 
        } catch (Exception e) {
            log.error("查询排队状态失败: {}", e.getMessage(), e);
            // 异常情况下返回默认值
            queueStatusVo.setStatus(0);
            queueStatusVo.setCurrentPosition(0);
            queueStatusVo.setTotalCount(0);
            queueStatusVo.setEstimatedWaitTime(0);
        }
 
        return queueStatusVo;
    }
 
}