liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
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
package com.xiaopeng.vehicleConvoy.util;
 
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.concurrent.TimeUnit;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.vehicleConvoy.config.MinistryTransportProperties;
import com.xiaopeng.vehicleConvoy.model.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
 
 
/**
 * 交通部接口对接
 */
@Slf4j
@Component
public class MinistryOfTransport {
 
    /**
     * 公司名称:广州易点智慧出行科技有限公司
     *
     * 平台简称:“有鹏出行”
     * 标识:XPQCB67N
     * key:gEhROt7lg4DEBanR
     *
     * 对接ip:127.0.0.1,125.71.236.64,47.98.173.145,116.62.136.48,39.108.249.22,47.105.208.135,47.112.126.81,114.55.179.11,39.170.77.50,120.26.242.215
     *
     * 接口地址:https://wycapi.jtj.gz.gov.cn/api/app/common/binapi
     */
    public static String DEFAULT_COMPANY_ID = "4401XPQCB67N";
 
    public static String DEFAULT_SYMBOL = "XPQCB67N_S0612018007357G";
 
    public static String DEFAULT_PROTOCOL_VERSION = "V2.0";
 
    private static final DateTimeFormatter FILE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-ddHHmmssSSS");
 
    public static final DateTimeFormatter DATA_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
 
    private static final String TOKEN_CACHE_KEY = "ministry_of_transport:token";
 
    private static final long TOKEN_EXPIRE_SECONDS = 20 * 60;
 
    private static final long TOKEN_REFRESH_THRESHOLD_SECONDS = 5 * 60;
 
    @Autowired
    private RedisService redisService;
 
    @Autowired
    private MinistryTransportProperties ministryTransportProperties;
 
    @PostConstruct
    public void initDefaultConstants() {
        DEFAULT_COMPANY_ID = defaultIfBlank(ministryTransportProperties.getCompanyId(), DEFAULT_COMPANY_ID);
        DEFAULT_SYMBOL = defaultIfBlank(ministryTransportProperties.getSymbol(), DEFAULT_SYMBOL);
        DEFAULT_PROTOCOL_VERSION = defaultIfBlank(ministryTransportProperties.getProtocolVersion(), DEFAULT_PROTOCOL_VERSION);
    }
 
    /**
     * 获取token
     * @return
     */
    public String getToken(){
        try {
            String cachedToken = redisService.getCacheObject(TOKEN_CACHE_KEY);
 
            if (StringUtils.isNotBlank(cachedToken)) {
                Long expireTime = redisService.getExpire(TOKEN_CACHE_KEY);
                if (expireTime > TOKEN_REFRESH_THRESHOLD_SECONDS) {
                    return cachedToken;
                } else {
                    log.info("token即将过期,剩余{}秒,开始刷新", expireTime);
                }
            } else {
                log.info("未找到缓存token,开始获取新token");
            }
 
            String format = LocalDateTime.now().format(FILE_TIME_FORMATTER);
            String filename = ministryTransportProperties.getPlatform() + "_TOKEN_ACCESS_REQ_" + format + ".json";
            JSONObject body = new JSONObject();
            body.put("platform", ministryTransportProperties.getPlatform());
            body.put("key", ministryTransportProperties.getKey());
            String response = uploadJsonFile(filename, body.toJSONString(), ministryTransportProperties.getPlatform());
 
            String token = parseToken(response);
            redisService.setCacheObject(TOKEN_CACHE_KEY, token, TOKEN_EXPIRE_SECONDS, TimeUnit.SECONDS);
            log.info("成功获取新token并缓存,有效期{}秒", TOKEN_EXPIRE_SECONDS);
 
            return token;
        }catch (Exception e){
            log.error("获取交通部token失败", e);
        }
        return null;
    }
 
    private String defaultIfBlank(String value, String defaultValue) {
        return StringUtils.isBlank(value) ? defaultValue : value;
    }
 
    /**
     * 报送网约车平台公司基本信息(PTJB)
     *
     * @return 监管平台原始响应
     */
    public String pushPlatformBaseInfo() {
        JSONObject item = buildPlatformBaseInfo();
        return pushBasicReqInfo(item,"PTJB");
    }
 
 
    /**
     * 报送网约车平台公司运营规模信息(PTYYGM)
     *
     * @return 监管平台原始响应
     */
    public String pushPlatformOperationScaleInfo() {
        JSONObject item = buildPlatformOperationScaleInfo();
        return pushBasicReqInfo(item,"PTYYGM");
    }
 
    /**
     * 报送网约车车辆基本信息(CLJB)
     *
     * @param dto 车辆基本信息
     * @return 监管平台原始响应
     */
    public String pushVehicleBaseInfo(RegulationVehicleBaseInfoDTO dto) {
        JSONObject item = buildVehicleBaseInfo(dto);
        return pushBasicReqInfo(item, "CLJB");
    }
 
    /**
     * 报送网约车车辆保险信息(CLBX)
     *
     * @param dto 车辆保险信息
     * @return 监管平台原始响应
     */
    public String pushVehicleInsuranceInfo(RegulationVehicleInsuranceInfoDTO dto) {
        JSONObject item = buildVehicleInsuranceInfo(dto);
        return pushBasicReqInfo(item, "CLBX");
    }
 
    /**
     * 报送网约车车辆里程信息(CLLC)
     *
     * @param dto 车辆里程信息
     * @return 监管平台原始响应
     */
    public String pushVehicleMileageInfo(RegulationVehicleMileageInfoDTO dto) {
        JSONObject item = buildVehicleMileageInfo(dto);
        return pushBasicReqInfo(item, "CLLC");
    }
 
    /**
     * 报送网约车驾驶员基本信息(JSYJB)
     *
     * @param dto 驾驶员基本信息
     * @return 监管平台原始响应
     */
    public String pushDriverBaseInfo(RegulationDriverBaseInfoDTO dto) {
        JSONObject item = buildDriverBaseInfo(dto);
        return pushBasicReqInfo(item, "JSYJB");
    }
 
    /**
     * 报送网约车驾驶员培训信息(JSYPX)
     *
     * @param dto 驾驶员培训信息
     * @return 监管平台原始响应
     */
    public String pushDriverTrainingInfo(RegulationDriverTrainingInfoDTO dto) {
        JSONObject item = buildDriverTrainingInfo(dto);
        return pushBasicReqInfo(item, "JSYPX");
    }
 
    /**
     * 报送网约车驾驶员移动终端信息(JSYYDZD)
     *
     * @param dto 驾驶员移动终端信息
     * @return 监管平台原始响应
     */
    public String pushDriverMobileTerminalInfo(RegulationDriverMobileTerminalInfoDTO dto) {
        JSONObject item = buildDriverMobileTerminalInfo(dto);
        return pushBasicReqInfo(item, "JSYYDZD");
    }
 
    /**
     * 报送网约车驾驶员统计信息(JSYTJ)
     *
     * @param dto 驾驶员统计信息
     * @return 监管平台原始响应
     */
    public String pushDriverStatisticsInfo(RegulationDriverStatisticsInfoDTO dto) {
        JSONObject item = buildDriverStatisticsInfo(dto);
        return pushBasicReqInfo(item, "JSYTJ");
    }
 
    /**
     * 报送网约车乘客基本信息(CKJB)
     *
     * @param dto 乘客基本信息
     * @return 监管平台原始响应
     */
    public String pushPassengerBaseInfo(RegulationPassengerBaseInfoDTO dto) {
        JSONObject item = buildPassengerBaseInfo(dto);
        return pushBasicReqInfo(item, "CKJB");
    }
 
    /**
     * 报送网约车平台投资人信息(PTTZR)
     *
     * @param dto 平台投资人信息
     * @return 监管平台原始响应
     */
    public String pushPlatformInvestorInfo(RegulationPlatformInvestorInfoDTO dto) {
        JSONObject item = buildPlatformInvestorInfo(dto);
        return pushBasicReqInfo(item, "PTTZR");
    }
 
    /**
     * 报送驾驶员合同信息(GTJSYHT)
     *
     * @param dto 驾驶员合同信息
     * @return 监管平台原始响应
     */
    public String pushDriverContractInfo(RegulationDriverContractInfoDTO dto) {
        JSONObject item = buildDriverContractInfo(dto);
        return pushBasicReqInfo(item, "GTJSYHT");
    }
 
    /**
     * 报送订单发起(DDFQ)
     *
     * @param dto 订单发起信息
     * @return 监管平台原始响应
     */
    public String pushOrderCreateInfo(RegulationOrderCreateInfoDTO dto) {
        JSONObject item = buildOrderCreateInfo(dto);
        return pushOrderReqInfo(item, "DDFQ");
    }
 
    /**
     * 报送订单成功(DDCG)
     *
     * @param dto 订单成功信息
     * @return 监管平台原始响应
     */
    public String pushOrderMatchInfo(RegulationOrderMatchInfoDTO dto) {
        JSONObject item = buildOrderMatchInfo(dto);
        return pushOrderReqInfo(item, "DDCG");
    }
 
    /**
     * 报送订单撤销(DDCX)
     *
     * @param dto 订单撤销信息
     * @return 监管平台原始响应
     */
    public String pushOrderCancelInfo(RegulationOrderCancelInfoDTO dto) {
        JSONObject item = buildOrderCancelInfo(dto);
        return pushOrderReqInfo(item, "DDCX");
    }
 
    /**
     * 报送订单违约(DDWY)
     *
     * @param dto 订单违约信息
     * @return 监管平台原始响应
     */
    public String pushOrderViolationInfo(RegulationOrderViolationInfoDTO dto) {
        JSONObject item = buildOrderViolationInfo(dto);
        return pushOrderReqInfo(item, "DDWY");
    }
 
    /**
     * 报送车辆营运上线(CLYYSX)
     *
     * @param dto 车辆营运上线信息
     * @return 监管平台原始响应
     */
    public String pushVehicleOperateLoginInfo(RegulationVehicleOperateLoginInfoDTO dto) {
        JSONObject item = buildVehicleOperateLoginInfo(dto);
        return pushOperationReqInfo(item, "CLYYSX");
    }
 
    /**
     * 报送车辆营运下线(CLYYXX)
     *
     * @param dto 车辆营运下线信息
     * @return 监管平台原始响应
     */
    public String pushVehicleOperateLogoutInfo(RegulationVehicleOperateLogoutInfoDTO dto) {
        JSONObject item = buildVehicleOperateLogoutInfo(dto);
        return pushOperationReqInfo(item, "CLYYXX");
    }
 
    /**
     * 报送营运出发(YYCF)
     *
     * @param dto 营运出发信息
     * @return 监管平台原始响应
     */
    public String pushOperateDepartInfo(RegulationOperateDepartInfoDTO dto) {
        JSONObject item = buildOperateDepartInfo(dto);
        return pushOperationReqInfo(item, "YYCF");
    }
 
    /**
     * 报送营运到达(YYDD)
     *
     * @param dto 营运到达信息
     * @return 监管平台原始响应
     */
    public String pushOperateArriveInfo(RegulationOperateArriveInfoDTO dto) {
        JSONObject item = buildOperateArriveInfo(dto);
        return pushOperationReqInfo(item, "YYDD");
    }
 
    /**
     * 报送营运支付(YYZF)
     *
     * @param dto 营运支付信息
     * @return 监管平台原始响应
     */
    public String pushOperatePayInfo(RegulationOperatePayInfoDTO dto) {
        JSONObject item = buildOperatePayInfo(dto);
        return pushOperationReqInfo(item, "YYZF");
    }
 
    /**
     * 报送驾驶员定位信息(JSYDW)
     *
     * @param dto 驾驶员定位信息
     * @return 监管平台原始响应
     */
    public String pushDriverPositionInfo(RegulationDriverPositionInfoDTO dto) {
        JSONObject item = buildDriverPositionInfo(dto);
        return pushGpsReqInfo(item, "JSYDW");
    }
 
    /**
     * 报送乘客评价信息(CKPJ)
     *
     * @param dto 乘客评价信息
     * @return 监管平台原始响应
     */
    public String pushPassengerEvaluateInfo(RegulationPassengerEvaluateInfoDTO dto) {
        JSONObject item = buildPassengerEvaluateInfo(dto);
        return pushEvaluateReqInfo(item, "CKPJ");
    }
 
    /**
     * 报送乘客投诉处理信息(CKTSCL)
     *
     * @param dto 乘客投诉处理信息
     * @return 监管平台原始响应
     */
    public String pushPassengerComplaintInfo(RegulationPassengerComplaintInfoDTO dto) {
        JSONObject item = buildPassengerComplaintInfo(dto);
        return pushEvaluateReqInfo(item, "CKTSCL");
    }
 
    /**
     * 报送车辆注销报备(CLZXBB)
     *
     * @param dto 车辆注销报备信息
     * @return 监管平台原始响应
     */
    public String pushVehicleCancelReportInfo(RegulationVehicleCancelReportInfoDTO dto) {
        JSONObject item = buildVehicleCancelReportInfo(dto);
        return pushRegistReqInfo(item, "CLZXBB");
    }
 
    /**
     * 报送驾驶员处罚信息(JSYCF)
     *
     * @param dto 驾驶员处罚信息
     * @return 监管平台原始响应
     */
    public String pushDriverPunishInfo(RegulationDriverPunishInfoDTO dto) {
        JSONObject item = buildDriverPunishInfo(dto);
        return pushEvaluateReqInfo(item, "JSYCF");
    }
 
    /**
     * 报送驾驶员信誉信息(JSYXY)
     *
     * @param dto 驾驶员信誉信息
     * @return 监管平台原始响应
     */
    public String pushDriverReputationInfo(RegulationDriverReputationInfoDTO dto) {
        JSONObject item = buildDriverReputationInfo(dto);
        return pushEvaluateReqInfo(item, "JSYXY");
    }
 
    /**
     * 报送人车对应关系信息(RCDYGX)
     *
     * @param dto 人车对应关系信息
     * @return 监管平台原始响应
     */
    public String pushDriverVehicleRelationInfo(RegulationDriverVehicleRelationInfoDTO dto) {
        JSONObject item = buildDriverVehicleRelationInfo(dto);
        return pushRelationshipReqInfo(item, "RCDYGX");
    }
 
    /**
     * 报送平台营运报备(PTYYBB)
     *
     * @param dto 平台营运报备信息
     * @return 监管平台原始响应
     */
    public String pushPlatformOperationRegisterInfo(RegulationPlatformOperationRegisterInfoDTO dto) {
        JSONObject item = buildPlatformOperationRegisterInfo(dto);
        return pushRegistReqInfo(item, "PTYYBB");
    }
 
    /**
     * 报送平台暂停营运报备(PTZTYYBB)
     *
     * @param dto 平台暂停营运报备信息
     * @return 监管平台原始响应
     */
    public String pushPlatformPauseOperationRegisterInfo(RegulationPlatformPauseOperationRegisterInfoDTO dto) {
        JSONObject item = buildPlatformPauseOperationRegisterInfo(dto);
        return pushRegistReqInfo(item, "PTZTYYBB");
    }
 
    /**
     * 报送合作聚合平台信息(PTJHPTXX)
     *
     * @param dto 合作聚合平台信息
     * @return 监管平台原始响应
     */
    public String pushPlatformAggregationInfo(RegulationPlatformAggregationInfoDTO dto) {
        JSONObject item = buildPlatformAggregationInfo(dto);
        return pushRegistReqInfo(item, "PTJHPTXX");
    }
 
    /**
     * 报送车辆营运报备(CLYYBB)
     *
     * @param dto 车辆营运报备信息
     * @return 监管平台原始响应
     */
    public String pushVehicleOperationRegisterInfo(RegulationVehicleOperationRegisterInfoDTO dto) {
        JSONObject item = buildVehicleOperationRegisterInfo(dto);
        return pushRegistReqInfo(item, "CLYYBB");
    }
 
    /**
     * 报送驾驶员注册报备(JSYZCBB)
     *
     * @param dto 驾驶员注册报备信息
     * @return 监管平台原始响应
     */
    public String pushDriverRegisterInfo(RegulationDriverRegisterInfoDTO dto) {
        JSONObject item = buildDriverRegisterInfo(dto);
        return pushRegistReqInfo(item, "JSYZCBB");
    }
 
    /**
     * 报送驾驶员注销注册(JSYZXZC)
     *
     * @param dto 驾驶员注销注册信息
     * @return 监管平台原始响应
     */
    public String pushDriverCancelRegisterInfo(RegulationDriverCancelRegisterInfoDTO dto) {
        JSONObject item = buildDriverCancelRegisterInfo(dto);
        return pushRegistReqInfo(item, "JSYZXZC");
    }
 
    /**
     * 报送网约车平台公司支付信息(PTZF)
     *
     * @return 监管平台原始响应
     */
    public String pushPlatformPayInfo(RegulationPlatformPayInfoDTO dto) {
        JSONObject item = buildPlatformPayInfo(dto);
        return pushBasicReqInfo(item,"PTZF");
    }
 
    /**
     * 网约车平台公司服务机构(PTFWJG)
     * @return 监管平台原始响应
     */
    public String pushPlatformServiceInfo(RegulationPlatformServiceInfoDTO dto) {
        JSONObject item = buildPlatformServiceInfo(dto);
        return pushBasicReqInfo(item,"PTFWJG");
    }
 
    /**
     * 网约车平台公司经营许可(CPTJYXK)
     * @return 监管平台原始响应
     */
    public String pushPlatformLicenseInfo(RegulationPlatformLicenseInfoDTO dto) {
        JSONObject item = buildPlatformLicenseInfo(dto);
        return pushBasicReqInfo(item,"CPTJYXK");
    }
 
    /**
     * 网约车平台公司计程计价方式(PTYJ)
     * @return 监管平台原始响应
     */
    public String pushPlatformPriceInfo(RegulationPlatformPriceInfoDTO dto) {
        JSONObject item = buildPlatformPriceInfo(dto);
        return pushBasicReqInfo(item,"PTYJ");
    }
 
 
    /**
     * 推送基础信息
     * @param item 推送基础信息
     * @param command 推送基础信息类型
     * @return 监管平台原始响应
     */
    private String pushBasicReqInfo(JSONObject item,String command) {
        return pushReqInfo(item,"BASIC",command);
    }
 
    /**
     * 推送订单信息
     * @param item 推送基础信息
     * @param command 推送基础信息类型
     * @return 监管平台原始响应
     */
    private String pushOrderReqInfo(JSONObject item,String command) {
        return pushReqInfo(item,"ORDER",command);
    }
 
    /**
     * 推送营运信息
     * @param item 推送营运信息
     * @param command 推送营运信息类型
     * @return 监管平台原始响应
     */
    private String pushOperationReqInfo(JSONObject item,String command) {
        return pushReqInfo(item,"OPERATION",command);
    }
 
    /**
     * 推送服务质量信息
     * @param item 推送服务质量信息
     * @param command 推送服务质量信息类型
     * @return 监管平台原始响应
     */
    private String pushEvaluateReqInfo(JSONObject item, String command) {
        return pushReqInfo(item,"EVALUATE",command);
    }
 
    /**
     * 推送注册报备信息
     * @param item 推送注册报备信息
     * @param command 推送注册报备信息类型
     * @return 监管平台原始响应
     */
    private String pushRegistReqInfo(JSONObject item,String command) {
        return pushReqInfo(item,"REGIST",command);
    }
 
    /**
     * 推送GPS信息
     * @param item 推送信息
     * @param command 推送信息关键字
     * @return 监管平台原始响应
     */
    private String pushGpsReqInfo(JSONObject item,String command) {
        return pushReqInfo(item,"GPS",command);
    }
 
 
    /**
     * 推送人车关系信息
     * @param item 推送信息
     * @param command 推送信息关键字
     * @return 监管平台原始响应
     */
    private String pushRelationshipReqInfo(JSONObject item,String command) {
        return pushReqInfo(item,"RELATIONSHIP",command);
    }
 
 
 
    /**
     * 推送信息
     * @param item 推送信息
     * @param type 推送信息类型
     * @param command 推送信息关键字
     * @return 监管平台原始响应
     */
    private String pushReqInfo(JSONObject item,String type,String command) {
        String token = parseToken(getToken());
        JSONObject body = new JSONObject();
        JSONArray items = new JSONArray();
        items.add(item);
        body.put("items", items);
 
        String format = LocalDateTime.now().format(FILE_TIME_FORMATTER);
        String filename = ministryTransportProperties.getPlatform() + "_"+ type +"_"+ command +"_REQ_" + format + ".json";
        return uploadJsonFile(filename, body.toJSONString(), token);
    }
 
 
    private JSONObject buildPlatformBaseInfo() {
        JSONObject item = new JSONObject();
        item.put("symbol", ministryTransportProperties.getSymbol());
        item.put("companyId", ministryTransportProperties.getCompanyId());
        item.put("companyName", ministryTransportProperties.getCompanyName());
        item.put("identifier", ministryTransportProperties.getIdentifier());
        item.put("contactPhone", ministryTransportProperties.getContactPhone());
        item.put("address", ministryTransportProperties.getPlatformAddressCode());
        item.put("businessScope", ministryTransportProperties.getBusinessScope());
        item.put("contactAddress", ministryTransportProperties.getContactAddress());
        item.put("economicType", ministryTransportProperties.getEconomicType());
        item.put("regCapital", ministryTransportProperties.getRegCapital());
        item.put("legalName", ministryTransportProperties.getLegalName());
        item.put("legalId", ministryTransportProperties.getLegalId());
        item.put("legalPhone", ministryTransportProperties.getLegalPhone());
        item.put("legalPhoto", encodePdf(ministryTransportProperties.getLegalPhotoFile()));
        item.put("legalPhotoId", ministryTransportProperties.getLegalPhotoId());
        item.put("identifierPhoto", encodePdf(ministryTransportProperties.getIdentifierPhotoFile()));
        item.put("state", ministryTransportProperties.getPlatformState());
        item.put("flag", "1");
        item.put("updateTime", LocalDateTime.now().format(DATA_TIME_FORMATTER));
        item.put("serviceItem", ministryTransportProperties.getServiceItem());
        item.put("servicePromise", ministryTransportProperties.getServicePromise());
        item.put("protocolVersion", ministryTransportProperties.getProtocolVersion());
        return item;
    }
 
    private JSONObject buildPlatformOperationScaleInfo() {
        JSONObject item = new JSONObject();
        item.put("symbol", ministryTransportProperties.getSymbol());
        item.put("companyId", ministryTransportProperties.getCompanyId());
        item.put("vehicleNum", ministryTransportProperties.getOperationScaleVehicleNum());
        item.put("driverNum", ministryTransportProperties.getOperationScaleDriverNum());
        item.put("flag", "1");
        item.put("updateTime", LocalDateTime.now().format(DATA_TIME_FORMATTER));
        item.put("protocolVersion", ministryTransportProperties.getProtocolVersion());
        return item;
    }
 
    private JSONObject buildVehicleBaseInfo(RegulationVehicleBaseInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车车辆基本信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildVehicleInsuranceInfo(RegulationVehicleInsuranceInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车车辆保险信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildVehicleMileageInfo(RegulationVehicleMileageInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车车辆里程信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverBaseInfo(RegulationDriverBaseInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车驾驶员基本信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverTrainingInfo(RegulationDriverTrainingInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车驾驶员培训信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverMobileTerminalInfo(RegulationDriverMobileTerminalInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车驾驶员移动终端信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverStatisticsInfo(RegulationDriverStatisticsInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车驾驶员统计信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPassengerBaseInfo(RegulationPassengerBaseInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车乘客基本信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformInvestorInfo(RegulationPlatformInvestorInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车平台投资人信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverContractInfo(RegulationDriverContractInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("驾驶员合同信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildOrderCreateInfo(RegulationOrderCreateInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("订单发起信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildOrderMatchInfo(RegulationOrderMatchInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("订单成功信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildOrderCancelInfo(RegulationOrderCancelInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("订单撤销信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildOrderViolationInfo(RegulationOrderViolationInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("订单违约信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildVehicleOperateLoginInfo(RegulationVehicleOperateLoginInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("车辆营运上线信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildVehicleOperateLogoutInfo(RegulationVehicleOperateLogoutInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("车辆营运下线信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildOperateDepartInfo(RegulationOperateDepartInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("营运出发信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildOperateArriveInfo(RegulationOperateArriveInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("营运到达信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildOperatePayInfo(RegulationOperatePayInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("营运支付信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverPositionInfo(RegulationDriverPositionInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("驾驶员定位信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPassengerEvaluateInfo(RegulationPassengerEvaluateInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("乘客评价信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPassengerComplaintInfo(RegulationPassengerComplaintInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("乘客投诉处理信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildVehicleCancelReportInfo(RegulationVehicleCancelReportInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("车辆注销报备信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverPunishInfo(RegulationDriverPunishInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("驾驶员处罚信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverReputationInfo(RegulationDriverReputationInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("驾驶员信誉信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverVehicleRelationInfo(RegulationDriverVehicleRelationInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("人车对应关系信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformOperationRegisterInfo(RegulationPlatformOperationRegisterInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("平台营运报备信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformPauseOperationRegisterInfo(RegulationPlatformPauseOperationRegisterInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("平台暂停营运报备信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformAggregationInfo(RegulationPlatformAggregationInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("合作聚合平台信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildVehicleOperationRegisterInfo(RegulationVehicleOperationRegisterInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("车辆营运报备信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverRegisterInfo(RegulationDriverRegisterInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("驾驶员注册报备信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildDriverCancelRegisterInfo(RegulationDriverCancelRegisterInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("驾驶员注销注册信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformPayInfo(RegulationPlatformPayInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车平台公司支付信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformServiceInfo(RegulationPlatformServiceInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车平台公司服务机构信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformLicenseInfo(RegulationPlatformLicenseInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车平台公司经营许可信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private JSONObject buildPlatformPriceInfo(RegulationPlatformPriceInfoDTO dto) {
        if (dto == null) {
            throw new RuntimeException("网约车平台公司计程计价方式信息不能为空");
        }
        return (JSONObject) JSON.toJSON(dto);
    }
 
    private String encodePdf(String filename) {
        if (filename == null || filename.trim().isEmpty()) {
            return "";
        }
        String resourcePath = "files/" + filename.trim();
        ClassPathResource resource = new ClassPathResource(resourcePath);
        try {
            long contentLength = resource.contentLength();
            if (contentLength > 10 * 1024 * 1024) {
                throw new RuntimeException("文件超过10M: " + filename);
            }
            try (InputStream inputStream = resource.getInputStream()) {
                byte[] bytes = new byte[(int) contentLength];
                int offset = 0;
                int read;
                while (offset < bytes.length && (read = inputStream.read(bytes, offset, bytes.length - offset)) != -1) {
                    offset += read;
                }
                if (offset != bytes.length) {
                    throw new RuntimeException("文件读取不完整: " + filename);
                }
                return Base64.getEncoder().encodeToString(bytes);
            }
        } catch (Exception e) {
            throw new RuntimeException("读取PDF文件失败: " + filename, e);
        }
    }
 
    private String uploadJsonFile(String filename, String jsonBody, String auth) {
        try {
            byte[] bytes = jsonBody.getBytes("UTF-8");
            String md5 = hash(new ByteArrayInputStream(bytes));
            HttpPost post = new HttpPost(ministryTransportProperties.getUrl());
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            post.addHeader("binfile-md5", md5);
            post.addHeader("binfile-auth", auth);
            post.addHeader("binfile-gzip", "false");
            post.addHeader("binfile-reqlen", String.valueOf(bytes.length));
            builder.addBinaryBody("binFile", new ByteArrayInputStream(bytes), ContentType.DEFAULT_BINARY, filename);
            builder.addTextBody("filename", filename, ContentType.create(ContentType.DEFAULT_TEXT.getMimeType(), "UTF-8"));
            post.setEntity(builder.build());
 
            HttpClientBuilder clientBuilder = HttpClientBuilder.create();
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(10000)
                    .build();
            clientBuilder.setDefaultRequestConfig(requestConfig);
            clientBuilder.disableContentCompression();
            try (CloseableHttpClient client = clientBuilder.build()) {
                HttpResponse response = client.execute(post);
                String responseBody = response.getEntity() == null ? "" : EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("交通部接口响应 status={}, filename={}, responseLength={}",
                        response.getStatusLine().getStatusCode(), filename, responseBody.length());
                return responseBody;
            }
        } catch (Exception e) {
            throw new RuntimeException("上传交通部接口文件失败: " + filename, e);
        }
    }
 
    private String parseToken(String tokenResponse) {
        if (StringUtils.isBlank(tokenResponse)) {
            throw new RuntimeException("交通部token响应为空");
        }
        try {
            JSONObject response = JSON.parseObject(tokenResponse);
            String token = response.getString("token");
            if (StringUtils.isNotBlank(token)) {
                return token;
            }
            JSONArray items = response.getJSONArray("items");
            if (items != null && !items.isEmpty()) {
                token = items.getJSONObject(0).getString("token");
                if (StringUtils.isNotBlank(token)) {
                    return token;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("解析交通部token失败: " + tokenResponse, e);
        }
        throw new RuntimeException("交通部token响应中未找到token: " + tokenResponse);
    }
 
 
 
 
    private static char[] commonDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 
    public static String hash(InputStream in)
    {
        try
        {
            byte[] buffer = new byte[1024];
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            int numRead = 0;
            while ((numRead = in.read(buffer)) > 0)
            {
                md5.update(buffer, 0, numRead);
            }
            in.close();
            return toHexString(md5.digest());
        }
        catch (Exception e)
        {
            return "";
        }
    }
 
    private static String toHexString(byte[] b)
    {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++)
        {
            sb.append(commonDigit[(b[i] & 0xf0) >>> 4]);
            sb.append(commonDigit[b[i] & 0x0f]);
        }
        return sb.toString();
    }
 
}