TrafficManageServiceImpl.java
56 KB
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
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
package com.bsth.service.impl;
import com.bsth.data.BasicData;
import com.bsth.email.SendEmailController;
import com.bsth.email.entity.EmailBean;
import com.bsth.entity.*;
import com.bsth.entity.realcontrol.ChildTaskPlan;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.entity.schedule.TTInfo;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.entity.sys.SysUser;
import com.bsth.entity.traffic.SKBUploadLogger;
import com.bsth.repository.*;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.repository.schedule.*;
import com.bsth.repository.traffic.SKBUploadLoggerRepository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.TrafficManageService;
import com.bsth.service.traffic.YgcBasicDataService;
import com.bsth.util.TimeUtils;
import com.bsth.util.db.DBUtils_MS;
import com.bsth.webService.trafficManage.org.tempuri.Results;
import com.bsth.webService.trafficManage.org.tempuri.WebServiceLocator;
import com.bsth.webService.trafficManage.org.tempuri.WebServiceSoap;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import java.io.*;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @ClassName: TrafficManageServiceImpl(运管处接口service业务层实现类)
*
* @Extends : BaseService
*
* @Description: TODO(运管处接口service业务层)
*
* @Author bsth@zq
*
* @Date 2016年10月28日 上午9:21:17
*
* @Version 公交调度系统BS版 0.1
*
*/
@Service
public class TrafficManageServiceImpl implements TrafficManageService{
Logger logger = LoggerFactory.getLogger(this.getClass());
// 线路repository
@Autowired
private LineRepository lineRepository;
@Autowired
private LineInformationRepository lineInformationRepository;
// 站点路由repository
@Autowired
private StationRouteRepository stationRouteRepository;
// 历史站点路由repository
@Autowired
private LsStationRouteRepository lsStationRouteRepository;
@Autowired
private SectionRepository sectionRepository;
// 车辆repository
@Autowired
private CarsRepository carsRepository;
// 人员repository
@Autowired
private PersonnelRepository personnelRepository;
// 时刻模板repository
@Autowired
private TTInfoRepository ttInfoRepository;
// 时刻模板明细repository
@Autowired
private TTInfoDetailRepository ttInfoDetailRepository;
// 排班计划明细repository
@Autowired
private SchedulePlanInfoRepository schedulePlanInfoRepository;
// 实际排班计划明细repository
@Autowired
private ScheduleRealInfoRepository scheduleRealInfoRepository;
// 时刻表上传记录repository
@Autowired
private SKBUploadLoggerRepository skbUploadLoggerRepository;
// 线路站点repository
@Autowired
private YgcBasicDataService ygcBasicDataService;
// 发送邮件
@Autowired
private SendEmailController sendEmailController;
// 运管处上传接口
private com.bsth.webService.trafficManage.up.org.tempuri.WebServiceSoap webServiceSoapUp;
private WebServiceSoap ssop ;
{
try {
ssop = new WebServiceLocator().getWebServiceSoap();
} catch (Exception e) {
e.printStackTrace();
}
}
// 格式化 年月日时分秒 nyrsfm是年月日时分秒的拼音首字母
private SimpleDateFormat sdfnyrsfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化 年月日
private SimpleDateFormat sdfnyr = new SimpleDateFormat("yyyy-MM-dd");
// 数字格式化
DecimalFormat format = new DecimalFormat("0.00");
// 用户名
private final String userNameOther = "user";
// 密码
private final String passwordOther = "user";
// 用户名
private final String userNameUp = "user";
// 密码
private final String passwordUp = "user";
// 接收邮件人
private final String emailSendToAddress = "175912183@qq.com";
// 记录路单上线的成功、失败线路数
private Integer countSuccess,countFailure;
private synchronized com.bsth.webService.trafficManage.up.org.tempuri.WebServiceSoap getWebServiceSoapUp(){
try {
if(webServiceSoapUp == null){
webServiceSoapUp = new com.bsth.webService.trafficManage.up.org.tempuri.WebServiceLocator().getWebServiceSoap();
}
}catch (Exception e){
e.printStackTrace();
}finally {
return webServiceSoapUp;
}
}
/**
* 上传线路信息
*/
@Override
public String setXL(String ids) {
String result = "failure";
StringBuffer sBuffer = new StringBuffer();
String[] idArray = ids.split(",");
try {
for (String id : idArray) {
if(id == null || id.trim().equals("")){
continue;
}
Map<String,Object> map = new HashMap<>();
map.put("lineCode_eq", id);
Line line ;
LineInformation lineInformation;
line = lineRepository.findOne(new CustomerSpecs<Line>(map));
if(line == null){
continue;
}
List<StationRoute> upStationsList ;// 上行站点路由集
List<StationRoute> downStationsList;// 下行站点路由集
List<Object[]> downPointList;// 下行站点集
List<Object[]> upPointList;// 上行站点集
sBuffer.append("<XLs>");
sBuffer.append("<XL>");
if(BasicData.lineId2ShangHaiCodeMap.get(line.getId()) == null){
return result;
}
map = new HashMap<>();
map.put("line.id_eq",line.getId());
lineInformation = lineInformationRepository.findOne(new CustomerSpecs<LineInformation>(map));
if(lineInformation == null){
continue;
}
sBuffer.append("<XLBM>").append(BasicData.lineId2ShangHaiCodeMap.get(line.getId())).append("</XLBM>");
sBuffer.append("<XLMC>").append(line.getName()).append("</XLMC>");
sBuffer.append("<QDZ>").append(line.getStartStationName()).append("</QDZ>");
sBuffer.append("<ZDZ>").append(line.getEndStationName()).append("</ZDZ>");
sBuffer.append("<QZLC>").append(lineInformation.getUpMileage()).append("</QZLC>");
sBuffer.append("<ZQLC>").append(lineInformation.getDownMileage()).append("</ZQLC>");
sBuffer.append("<XLGH>").append(line.getLinePlayType()).append("</XLGH>");
sBuffer.append("<UPDATE_DATE>").append(sdfnyr.format(new Date())).append("</UPDATE_DATE>");
// 循环添加站点信息
sBuffer.append("<StationList>");
// 先查上行
upStationsList = stationRouteRepository.findByLine(line.getLineCode(), 0);
Map<String, Integer> stationNumMap = getStationName2YgcNumberMap(line.getLineCode());
int startId = 1;
startId = packagStationXml(upStationsList, sBuffer, startId,stationNumMap);
// 环线不查下行
if(line.getLinePlayType() != 1){
// 再查下行
downStationsList = stationRouteRepository.findByLine(line.getLineCode(), 1);
packagStationXml(downStationsList, sBuffer, startId,stationNumMap);
}
sBuffer.append("</StationList>");
// 循环添加站点点位信息
sBuffer.append("<LinePointList>");
upPointList = sectionRepository.getSectionDirByLineId(line.getId(),0);
startId = 1;
startId = packagStationPointXml(upPointList, sBuffer, startId);
// 环线不查下行
if(line.getLinePlayType() != 1){
downPointList = sectionRepository.getSectionDirByLineId(line.getId(),1);
packagStationPointXml(downPointList, sBuffer, startId);
}
sBuffer.append("</LinePointList>");
sBuffer.append("</XL>");
sBuffer.append("</XLs>");
// 临时添加,后面删除
if(sBuffer.indexOf("<ZDXH>0</ZDXH>") != -1){
return "0";
}
// 调用上传方法
if(getWebServiceSoapUp().setXL(userNameUp,passwordUp,sBuffer.toString()).isSuccess()){
result = "success";
}else{
result = "failure";
}
logger.info("setXL:"+sBuffer.toString());
logger.info("setXL:"+result);
}
} catch (Exception e) {
logger.error("setXL:",e);
e.printStackTrace();
}
return result;
}
/**
* 加载运管处的站点及序号
* 上行从1开始,下行顺序续编
*/
private Map<String, Integer> getStationName2YgcNumberMap (String lineCode){
Map<String, Integer> resultMap = new HashMap<>();
List<Map<String, String>> ygcLines = stationRouteRepository.findLineWithYgcByLine(lineCode);
if(ygcLines != null && ygcLines.size() > 0){
int size = ygcLines.size();
Map<String, String> tempMap ;
int num = 1;
String key;
for (int i = 0; i < size; i ++){
tempMap = ygcLines.get(i);
key = tempMap.get("lineCode") + "_"+String.valueOf(tempMap.get("directions"))
+ "_"+tempMap.get("stationCode")+ "_"+tempMap.get("stationMark");
resultMap.put(key,num++);
}
}
return resultMap;
}
/**
* 上传线路信息(按in_use上传)
*/
@Override
public String setXLByInUse(String inUse) {
StringBuffer result = new StringBuffer();
try {
Map<String,Object> map = new HashMap<>();
if(inUse != null && inUse.equals("1")){
map.put("inUse_eq", inUse);
}
List<Line> lines ;
Line line;
lines = lineRepository.findAll(new CustomerSpecs<Line>(map));
if(lines != null && lines.size() > 0){
for(int i = 0 ; i < lines.size() ; i ++){
line = lines.get(i);
if(line != null && line.getId() != null){
result.append(line.getLineCode()).append(":").append(setXL(line.getLineCode())).append(";");
}
}
}
} catch (Exception e) {
result.append("failure");
logger.error("setXLByInUse:",e);
e.printStackTrace();
}
return result.toString();
}
/**
* 上传车辆信息
*/
@Override
public String setCL() {
String result = "failure";
StringBuffer sBuffer =new StringBuffer();
try {
sBuffer.append("<CLs>");
Cars cars = null;
String company;
Iterator<Cars> carsIterator = carsRepository.findAll().iterator();
while(carsIterator.hasNext()){
cars = carsIterator.next();
sBuffer.append("<CL>");
company = cars.getCompany();
setCompanyName(company);// 统一公司名称
sBuffer.append("<GSJC>").append(company).append("</GSJC>");
sBuffer.append("<NBH>").append(cars.getInsideCode()).append("</NBH>");
sBuffer.append("<CPH>").append(cars.getCarPlate()).append("</CPH>");
sBuffer.append("<YYZBH>").append(cars.getServiceNo()).append("</YYZBH>");
sBuffer.append("<CZCPH>").append(cars.getCarPlate()).append("</CZCPH>");//******这个数据没有***********
sBuffer.append("<CZZDBH>").append(cars.getEquipmentCode()).append("</CZZDBH>");
sBuffer.append("<UPDT>").append(sdfnyrsfm.format(new Date())).append("</UPDT>");
sBuffer.append("</CL>");
}
sBuffer.append("</CLs>");
if(ssop.setCL(userNameOther, passwordOther, sBuffer.toString()).isSuccess()){
result = "success";
}
} catch (Exception e) {
logger.error("setCL:",e);
e.printStackTrace();
}finally{
logger.info("setCL:"+sBuffer.toString());
logger.info("setCL:"+result);
}
return result;
}
/**
* 上传司机信息
*/
@Override
public String setSJ() {
String result = "failure";
StringBuffer sBuffer =new StringBuffer();
try {
sBuffer.append("<SJs>");
Personnel personnel = null;
String company;
Iterator<Personnel> personIterator = personnelRepository.findAll().iterator();
while(personIterator.hasNext()){
personnel = personIterator.next();
sBuffer.append("<SJ>");
company = personnel.getCompany();
setCompanyName(company);// 统一公司名称
sBuffer.append("<GSJC>").append(company).append("</GSJC>");
sBuffer.append("<SJGH>").append(personnel.getJobCode()).append("</SJGH>");
sBuffer.append("<CYZGZH>").append(personnel.getPapersCode()).append("</CYZGZH>");//***********
sBuffer.append("<XM>").append(personnel.getPersonnelName()).append("</XM>");
sBuffer.append("<UPDT>").append(sdfnyrsfm.format(new Date())).append("</UPDT>");
sBuffer.append("</SJ>");
}
sBuffer.append("</SJs>");
if(ssop.setSJ(userNameOther, passwordOther, sBuffer.toString()).isSuccess()){
result = "success";
};
} catch (Exception e) {
logger.error("setSJ:",e);
e.printStackTrace();
}finally{
logger.info("setSJ:"+sBuffer.toString());
logger.info("setSJ:"+result);
}
return result;
}
/**
* 上传路单 指定日期 yyyy-MM-dd
* @param theDate
* @return
*/
public String setLD(String theDate){
return uploadLD(theDate);
}
/**
* 上传路单 上传前一天的路单
* @return
*/
public String setLD(){
return uploadLD(null);
}
/**
* 上传路单
* @return 上传成功标识
*/
private String uploadLD(String theDate){
String result = "failure";
countSuccess = 0 ;countFailure = 0;
Line line;
// 取昨天 的日期
String date = theDate == null ?sdfnyr.format(DateUtils.addDays(new Date(), -1)) : theDate;
StringBuffer sf = new StringBuffer();
StringBuffer logSuccess = new StringBuffer("成功:");
StringBuffer logFailure = new StringBuffer("失败:");
HashMap logXlbmSuccessMap = new HashMap();
HashMap logXlbmFailureMap = new HashMap();
HashMap logXlbmMap = new HashMap();
Results results = null;
String str = "",xlbm;
try {
int counter = 0; // 计数器
int per = 10; // 每几条线路上传一次路单
List<ScheduleRealInfo> list = scheduleRealInfoRepository.setLD(date);
List<Map<String,Object>> listGroup = scheduleRealInfoRepository.setLDGroup(date);
Map<String,Object> map = new HashMap();
HashMap<String,String> paramMap;
HashMap<String,String> otherMap = new HashMap();
for(Map<String,Object> schRealInfo:listGroup){
if(schRealInfo != null){
//根据车辆自编号查询车牌号
map.put("insideCode_eq", schRealInfo.get("clZbh")+"");
Cars car = carsRepository.findOne(new CustomerSpecs<Cars>(map));
// 获取线路是否使用标识,如果未使用,则不查该线路数据
line = lineRepository.findByLineCode(schRealInfo.get("xlBm")+"");
if(line == null || line.getInUse() == null || line.getInUse() == 0){
continue;
}
if(counter % per == 0){
sf = new StringBuffer();
sf.append("<DLDS>");
}
counter ++;
xlbm = BasicData.lineCode2ShangHaiCodeMap.get(schRealInfo.get("xlBm")+"");
// 保存一次路单的线路编码,用于发送邮箱
if(logXlbmMap.get(xlbm) == null){
logXlbmMap.put(xlbm,xlbm);
}
sf.append("<DLD>");
sf.append("<RQ>"+date+"</RQ>");
sf.append("<XLBM>"+xlbm+"</XLBM>");
sf.append("<LPBH>"+schRealInfo.get("lpName")+"</LPBH>");
sf.append("<CPH>"+car.getCarPlate()+"</CPH>");
sf.append("<UPDT>"+sdfnyrsfm.format(new Date())+"</UPDT>");
sf.append("<LDList>");
for(ScheduleRealInfo scheduleRealInfo:list){
if((schRealInfo.get("xlBm")+"").equals(scheduleRealInfo.getXlBm()) && (schRealInfo.get("lpName")+"")
.equals(scheduleRealInfo.getLpName())
&& (schRealInfo.get("clZbh")+"").equals(scheduleRealInfo.getClZbh())){
if(scheduleRealInfo.isDestroy()){
if(scheduleRealInfo.isReissue()){
scheduleRealInfo.setFcsjActualAll(scheduleRealInfo.getDfsj());
scheduleRealInfo.setZdsjActualAll(scheduleRealInfo.getZdsj());
}
else
continue;
}
if(scheduleRealInfo.getBcType().equals("in")
|| scheduleRealInfo.getBcType().equals("out")){
continue;
}
sf.append("<LD>");
sf.append("<SJGH>"+scheduleRealInfo.getjGh()+"</SJGH>");
sf.append("<SXX>"+scheduleRealInfo.getXlDir()+"</SXX>");
sf.append("<FCZDMC>"+scheduleRealInfo.getQdzName()+"</FCZDMC>");
// 起点站的参数
otherMap.put("stationMark","B");
paramMap = packageYgcStationNumParam(scheduleRealInfo,otherMap);
sf.append("<FCZDXH>" + getYgcStationNumByLineCodeAndDirectionAndStationName(paramMap,null) + "</FCZDXH>");
sf.append("<FCZDBM>"+scheduleRealInfo.getQdzCode()+"</FCZDBM>");
sf.append("<JHFCSJ>"+scheduleRealInfo.getFcsj()+"</JHFCSJ>");
sf.append("<DFSJ>"+scheduleRealInfo.getDfsj()+"</DFSJ>");
sf.append("<SJFCSJ>"+scheduleRealInfo.getFcsjActual()+"</SJFCSJ>");
sf.append("<FCZDLX>"+""+"</FCZDLX>");
sf.append("<DDZDMC>"+scheduleRealInfo.getZdzName()+"</DDZDMC>");
// 终点站的参数
otherMap.put("stationMark","E");
paramMap = packageYgcStationNumParam(scheduleRealInfo,otherMap);
sf.append("<DDZDXH>"+ getYgcStationNumByLineCodeAndDirectionAndStationName(paramMap,null) +"</DDZDXH>");
sf.append("<DDZDBM>"+scheduleRealInfo.getZdzCode()+"</DDZDBM>");
sf.append("<JHDDSJ>"+scheduleRealInfo.getZdsj()+"</JHDDSJ>");
sf.append("<SJDDSJ>"+scheduleRealInfo.getZdsjActual()+"</SJDDSJ>");
sf.append("<DDZDLX>"+""+"</DDZDLX>");
sf.append("<LDSCBZ>"+0+"</LDSCBZ>");
sf.append("<DDBZ>"+scheduleRealInfo.getRemarks()+"</DDBZ>");
sf.append("</LD>");
}
}
sf.append("</LDList>");
sf.append("</DLD>");
}
if(counter % per == per - 1){
counter = 0;
sf.append("</DLDS>");
str = sf.toString().replace("'","");// 去掉'号
results = ssop.setLD(userNameOther, passwordOther, StringEscapeUtils.unescapeHtml(str));
// 记录日志
result = logRecord(results,logXlbmMap,logXlbmSuccessMap,logXlbmFailureMap,logSuccess,logFailure,str);
}
}
// 每per条线路上传后剩下的数据再上传
if(counter > 0){
sf.append("</DLDS>");
str = sf.toString().replace("'","");// 去掉'号
results = ssop.setLD(userNameOther, passwordOther, StringEscapeUtils.unescapeHtml(str));
}
// 记录日志
result = logRecord(results,logXlbmMap,logXlbmSuccessMap,logXlbmFailureMap,logSuccess,logFailure,str);
} catch (Exception e) {
logger.error("setLD:",e);
logFailure.append(e).append("<br/>");
for (StackTraceElement traceElement : e.getStackTrace()){
logFailure.append("\r\t").append(traceElement);
}
e.printStackTrace();
}finally{
try {
//发送邮件
EmailBean mail = new EmailBean();
mail.setSubject(InetAddress.getLocalHost().getHostAddress()+":路单日志数据"+date);
mail.setContent(logSuccess+"<br/>成功数:"+countSuccess+"<br/>" +logFailure+"<br/>失败数:"+countFailure);
sendEmailController.sendMail(emailSendToAddress, mail);
logger.info("setLD-sendMail:邮件发送成功!");
}catch (Exception e){
e.printStackTrace();
logger.error("setLD-sendMail:",e);
}
}
return result;
}
/**
* 记录日志
* @param results
* @param logXlbmMap
* @param logXlbmSuccessMap
* @param logXlbmFailureMap
* @param logSuccess
* @param logFailure
* @param str
*/
private String logRecord(Results results,HashMap logXlbmMap,HashMap logXlbmSuccessMap,HashMap logXlbmFailureMap,StringBuffer logSuccess,
StringBuffer logFailure,String str){
String result = "failure";
// 记录日志
if(results != null){
if(results.isSuccess()){// 上传成功
// 把上线成功的线路编码放入 logXlbmSuccessMap,并记录logSuccess
countSuccess += fillMailXlbmMap(logXlbmMap,logXlbmSuccessMap,logSuccess);
result = "success";
}else{// 上传失败
// 把上线失败的线路编码放入 logXlbmFailureMap,并记录logFailure
countFailure += fillMailXlbmMap(logXlbmMap,logXlbmFailureMap,logFailure);
result = "failure";
}
logger.info("setLD:"+str);
logger.info("setLD:"+result);
results = null;
logXlbmMap = new HashMap();
}
return result;
}
/**
* 填充线路编码到相应的map
* @param fromMap
* @param toMap
*/
private int fillMailXlbmMap(HashMap fromMap,HashMap toMap,StringBuffer logStr){
int tmpCount = 0;
for (Object key : fromMap.keySet()) {
if(toMap.get(key) == null){
toMap.put(key,fromMap.get(key));
logStr.append(key).append(",");
tmpCount ++;
}
}
fromMap = new HashMap();
return tmpCount;
}
/**
* 上传路单 xml来自文件
* @return 上传成功标识
*/
public String setLDFile(){
String result = "failure";
try {
String tmp = readXmlFromFile("E:/ld.txt");
Results rss = ssop.setLD(userNameOther, passwordOther, StringEscapeUtils.unescapeHtml(tmp));
if(rss.isSuccess()){
result = "success";
}
} catch (Exception e) {
logger.error("setLD:",e);
e.printStackTrace();
}finally{
}
return result;
}
/**
* 从文件中读取xml
* @param fileName 例:D:/test.txt
* @return
* @throws Exception
*/
private String readXmlFromFile(String fileName) throws Exception {
StringBuffer sf = new StringBuffer("");
File file = new File(fileName);
InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"GBK");
BufferedReader bufferedReader = new BufferedReader(reader);
String lineTxt = "";
while((lineTxt = bufferedReader.readLine()) != null){
sf.append(lineTxt);
}
reader.close();
return sf.toString().replaceAll("\t","");
}
/**
* 上传里程油耗
* @return 上传成功标识
*/
public String setLCYH(){
String result = "failure";
// 取昨天 的日期
String date = sdfnyr.format(DateUtils.addDays(new Date(), -1));
StringBuffer sf = new StringBuffer();
try {
sf.append("<LCYHS>");
List<Map<String,Object>> listGroup = scheduleRealInfoRepository.setLCYHGroup(date);
List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDate(date);
Map<String,Object> map = new HashMap<String,Object>();
for(Map<String,Object> schRealInfo:listGroup){
if(schRealInfo != null){
map.put("insideCode_eq", schRealInfo.get("clZbh")+"");
Cars car = carsRepository.findOne(new CustomerSpecs<Cars>(map));
/**
* 如果car==null,则说明该车辆是从线调中换车功能中加进去的,
* 在cars基础信息中查不到车辆的信息,所以忽略该车辆
*/
if(car == null){
continue;
}
//计算总公里和空驶公里,营运公里=总公里-空驶公里
double totalKilometers = 0,emptyKilometers =0;
sf.append("<LCYH>");
sf.append("<RQ>"+date+"</RQ>");
sf.append("<XLBM>"+BasicData.lineCode2ShangHaiCodeMap.get(schRealInfo.get("xlBm"))+"</XLBM>");
sf.append("<CPH>"+car.getCarPlate()+"</CPH>");
if(list != null && list.size() > 0){
for(ScheduleRealInfo scheduleRealInfo:list){
if((schRealInfo.get("xlBm")+"").equals(scheduleRealInfo.getXlBm()) && (schRealInfo.get("clZbh")+"")
.equals(scheduleRealInfo.getClZbh())){
Set<ChildTaskPlan> childTaskPlans = scheduleRealInfo.getcTasks();
//如果没有子任务,里程就是已执行(Status=2);有子任务的,忽略主任务,子任务的烂班
if(childTaskPlans.isEmpty()){
if(scheduleRealInfo.getStatus() == 2){
totalKilometers += scheduleRealInfo.getJhlc()==null?0.0:scheduleRealInfo.getJhlc();
if(scheduleRealInfo.getBcType().equals("in") || scheduleRealInfo.getBcType().equals("out")
|| scheduleRealInfo.getBcType().equals("venting")){
emptyKilometers += scheduleRealInfo.getJhlc()==null?0.0:scheduleRealInfo.getJhlc();
}
}
}else{
Iterator<ChildTaskPlan> it = childTaskPlans.iterator();
while(it.hasNext()){
ChildTaskPlan childTaskPlan = it.next();
if(!childTaskPlan.isDestroy()){
totalKilometers += childTaskPlan.getMileage()==null?0.0:childTaskPlan.getMileage();
if(childTaskPlan.getMileageType().equals("empty")){
emptyKilometers += childTaskPlan.getMileage()==null?0.0:childTaskPlan.getMileage();;
}
}
}
}
}
}
}
sf.append("<ZLC>"+totalKilometers+"</ZLC>");
sf.append("<YYLC>"+emptyKilometers+"</YYLC>");
sf.append("<YH>"+""+"</YH>");
sf.append("<JZYL>"+""+"</JZYL>");
sf.append("<DH>"+""+"</DH>");
sf.append("<UPDT>"+sdfnyrsfm.format(new Date())+"</UPDT>");
sf.append("<BBSCBZ>"+0+"</BBSCBZ>");
sf.append("</LCYH>");
}
}
sf.append("</LCYHS>");
if(ssop.setLCYH(userNameOther, passwordOther, sf.toString()).isSuccess()){
result = "success";
}
} catch (Exception e) {
logger.error("setLCYH:",e);
e.printStackTrace();
}finally{
logger.info("setLCYH:"+sf.toString());
logger.info("setLCYH:"+result);
}
return result;
}
/**
* 上传线路调度日报
* @return
*/
public String setDDRB(){
String result = "failure";
// 取昨天 的日期
String date = sdfnyr.format(DateUtils.addDays(new Date(), -1));
StringBuffer sf = new StringBuffer();
try {
sf.append("<DDRBS>");
List<Map<String,Object>> listGroup = scheduleRealInfoRepository.setDDRBGroup(date);
List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDate(date);
for(Map<String,Object> schRealInfo:listGroup){
if(schRealInfo != null){
double jhlc = 0,zlc = 0,jhkslc = 0,sjkslc = 0;
int jhbc = 0,sjbc = 0,jhzgfbc = 0,sjzgfbc = 0,jhwgfbc = 0,sjwgfbc = 0;
sf.append("<DDRB>");
sf.append("<RQ>"+date+"</RQ>");
sf.append("<XLBM>"+BasicData.lineCode2ShangHaiCodeMap.get(schRealInfo.get("xlBm"))+"</XLBM>");
for(ScheduleRealInfo scheduleRealInfo:list){
if(scheduleRealInfo != null){
if((schRealInfo.get("xlBm")+"").equals(scheduleRealInfo.getXlBm())){
//计划
if(!scheduleRealInfo.isSflj()){
jhlc += scheduleRealInfo.getJhlc()==null?0.0:scheduleRealInfo.getJhlc();
//计划空驶
if(scheduleRealInfo.getBcType().equals("in") || scheduleRealInfo.getBcType().equals("out")){
jhkslc += scheduleRealInfo.getJhlc()==null?0.0:scheduleRealInfo.getJhlc();
}
//计划早高峰,计划晚高峰
if(TimeUtils.morningPeak(scheduleRealInfo.getFcsj())){
jhzgfbc++;
} else if(TimeUtils.evenignPeak(scheduleRealInfo.getFcsj())){
jhwgfbc++;
}
}
jhbc++;
//实际
Set<ChildTaskPlan> childTaskPlans = scheduleRealInfo.getcTasks();
//如果没有子任务,里程就是已执行(Status=2);有子任务的,忽略主任务,子任务的烂班
if(childTaskPlans.isEmpty()){
if(scheduleRealInfo.getStatus() == 2){
sjbc++;
zlc += scheduleRealInfo.getJhlc()==null?0.0:scheduleRealInfo.getJhlc();
if(scheduleRealInfo.getBcType().equals("in") || scheduleRealInfo.getBcType().equals("out")
|| scheduleRealInfo.getBcType().equals("venting")){
sjkslc += scheduleRealInfo.getJhlc()==null?0.0:scheduleRealInfo.getJhlc();;
}
}
}else{
sjbc++;
Iterator<ChildTaskPlan> it = childTaskPlans.iterator();
while(it.hasNext()){
ChildTaskPlan childTaskPlan = it.next();
if(!childTaskPlan.isDestroy()){
zlc += childTaskPlan.getMileage()==null?0.0:childTaskPlan.getMileage();
if(childTaskPlan.getMileageType().equals("empty")){
sjkslc += childTaskPlan.getMileage()==null?0.0:childTaskPlan.getMileage();;
}
}
}
}
//实际早高峰,计划晚高峰
if(scheduleRealInfo.getFcsjActual() != null){
if(TimeUtils.morningPeak(scheduleRealInfo.getFcsj())){
sjzgfbc++;
} else if(TimeUtils.evenignPeak(scheduleRealInfo.getFcsj())){
sjwgfbc++;
}
}
}
}
}
sf.append("<JHLC>"+format.format(jhlc)+"</JHLC>");
sf.append("<SSLC>"+format.format((zlc-sjkslc))+"</SSLC>");
sf.append("<JHKSLC>"+format.format(jhkslc)+"</JHKSLC>");
sf.append("<SJKSLC>"+format.format(sjkslc)+"</SJKSLC>");
sf.append("<JHBC>"+jhbc+"</JHBC>");
sf.append("<SJBC>"+sjbc+"</SJBC>");
sf.append("<JHZGFBC>"+jhzgfbc+"</JHZGFBC>");
sf.append("<SJZGFBC>"+sjzgfbc+"</SJZGFBC>");
sf.append("<JHWGFBC>"+jhwgfbc+"</JHWGFBC>");
sf.append("<SJWGFBC>"+sjwgfbc+"</SJWGFBC>");
sf.append("<UPDT>"+sdfnyrsfm.format(new Date())+"</UPDT>");
sf.append("<RBSCBZ>"+0+"</RBSCBZ>");
sf.append("</DDRB>");
}
}
sf.append("</DDRBS>");
if(ssop.setDDRB(userNameOther, passwordOther, sf.toString()).isSuccess()){
result = "success";
}
} catch (Exception e) {
logger.error("setDDRB:",e);
e.printStackTrace();
}finally{
logger.info("setDDRB:"+sf.toString());
logger.info("setDDRB:"+result);
}
return result;
}
/**
* 上传计划班次 指定日期 yyyy-MM-dd
* @param theDate
* @return
*/
public String setJHBC(String theDate){
return uploadJHBC(theDate);
}
/**
* 上传计划班次
* @return
*/
public String setJHBC(){
return uploadJHBC(null);
}
/**
* 上传线路计划班次表
*/
private String uploadJHBC(String theDate) {
String result = "failure";
Line line;
StringBuffer sBuffer =new StringBuffer();
try {
sBuffer.append("<JHBCs>");
// 声明变量
SchedulePlanInfo schedulePlanInfo;
String xlbm,zbh = "";
Long lp = 0L;
// 取得计划班次时间
String tomorrow = theDate == null ? sdfnyr.format(DateUtils.addDays(new Date(), +1)) : theDate;
// 查询所有班次
List<SchedulePlanInfo> schedulePlanList = schedulePlanInfoRepository.findLineScheduleBc(tomorrow);
int j = 0; // 初始化标识
if(schedulePlanList != null ){
HashMap<String,String> paramMap;
HashMap<String,String> otherMap = new HashMap<String, String>();
int size = schedulePlanList.size();
for (int i = 0; i < size; i++) {
schedulePlanInfo = schedulePlanList.get(i);
xlbm = schedulePlanInfo.getXlBm();
// 获取线路是否使用标识,如果未使用,则不查该线路数据
line = lineRepository.findByLineCode(xlbm);
if(line.getInUse() == null || line.getInUse() == 0){
continue;
}
if(++j == 1){// 第一次,则初始化值
zbh = schedulePlanInfo.getClZbh();
lp = schedulePlanInfo.getLp();
// 拼装XML
assembleJHBC(sBuffer, schedulePlanInfo, xlbm, zbh, lp);
}
// 比较是否为同一条线路同一辆车
if(xlbm.equals(schedulePlanInfo.getXlBm())
&& zbh.equals(schedulePlanInfo.getClZbh())
&& lp == schedulePlanInfo.getLp()){
if(schedulePlanInfo.getBcType().equals("in") || schedulePlanInfo.getBcType().equals("out")){
continue;
}
sBuffer.append("<BC>");
sBuffer.append("<SJGH>").append(schedulePlanInfo.getjGh()).append("</SJGH>");
sBuffer.append("<SXX>").append(schedulePlanInfo.getXlDir()).append("</SXX>");
sBuffer.append("<FCZDMC>").append(schedulePlanInfo.getQdzName()).append("</FCZDMC>");
// 起点站的参数
otherMap.put("stationMark","B");
paramMap = packageYgcStationNumParam(schedulePlanInfo,otherMap);
sBuffer.append("<ZDXH>").append(getYgcStationNumByLineCodeAndDirectionAndStationName(paramMap,null)).append("</ZDXH>");
sBuffer.append("<JHFCSJ>").append(schedulePlanInfo.getFcsj()).append("</JHFCSJ>");
sBuffer.append("<DDZDMC>").append(schedulePlanInfo.getZdzName()).append("</DDZDMC>");
// 起点站的参数
otherMap.put("stationMark","E");
paramMap = packageYgcStationNumParam(schedulePlanInfo,otherMap);
sBuffer.append("<DDXH>").append(getYgcStationNumByLineCodeAndDirectionAndStationName(paramMap,null)).append("</DDXH>");
sBuffer.append("<JHDDSJ>").append(calcDdsj(schedulePlanInfo.getFcsj(),schedulePlanInfo.getBcsj()))
.append("</JHDDSJ>");
sBuffer.append("</BC>");
if(i == size -1 ){
sBuffer.append("</BCList>");
sBuffer.append("</JHBC>");
}
}else{
zbh = schedulePlanInfo.getClZbh();
lp = schedulePlanInfo.getLp();
sBuffer.append("</BCList>");
sBuffer.append("</JHBC>");
// 拼装XML
assembleJHBC(sBuffer, schedulePlanInfo, xlbm, zbh, lp);
}
}
}
// 判断XML是否以</BCList>结尾,如果不是,则加上
String regex = "^*</JHBC>$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sBuffer);
boolean isEndWithTrueFlag = false;
while (m.find()) {
isEndWithTrueFlag = true;
}
// 加上缺失的标签
if(!isEndWithTrueFlag){
sBuffer.append("</BCList>");
sBuffer.append("</JHBC>");
}
sBuffer.append("</JHBCs>");
if(ssop.setJHBC(userNameOther, passwordOther, sBuffer.toString()).isSuccess()){
result = "success";
}
} catch (Exception e) {
logger.error("setJHBC:",e);
e.printStackTrace();
}finally{
logger.info("setJHBC:"+sBuffer.toString());
logger.info("setJHBC:"+result);
}
return result;
}
/**
* 上传线路班次时刻表数据
*/
@Override
public String setSKB(String ids) {
String result = "failure";
StringBuffer sBuffer = new StringBuffer();
DecimalFormat df = new DecimalFormat("######0.000");
Map<String,String> lsStationCode2NameMap;
Map<String, Integer> lsStationName2YgcNumber;
try {
String[] idArray = ids.split(",");
StringBuffer sBufferA ,sBufferB ,sBufferC ;
double zlc,yylc,singleLc,emptyLc;// 总里程、营运里程、单程、空放里程
String bcType,sxx;// 班次类型、上下行
// 上传的时刻表集合
List<TTInfo> ttinfoList = new ArrayList<>();
TTInfo ttInfo;
TTInfoDetail ttInfoDetail;
LineInformation lineInformation;
Iterator<TTInfoDetail> ttInfoDetailIterator;
HashMap<String,Object> param ;
sBuffer.append("<SKBs>");
HashMap<String,String> paramMap;
HashMap<String,String> otherMap = new HashMap<>();
for (int i = 0; i < idArray.length; i++) {
long ttinfoId = Long.valueOf(idArray[i]);
ttInfo = ttInfoRepository.findOne(ttinfoId);
if(ttInfo == null)
continue;
ttinfoList.add(ttInfo); // 保存时刻表
// 得到时刻表版本号
int lineVersion = ttInfo.getLineVersion();
// 查询历史站点路由
lsStationCode2NameMap = getLsStationCode(ttInfo.getXl().getLineCode(),lineVersion);
// 查询历史站点路由
lsStationName2YgcNumber = getLsStationRoute(ttInfo.getXl().getLineCode(),lineVersion);
zlc = 0.0f;
yylc = 0.0f;
// 获得时刻表
param = new HashMap();
param.put("ttinfo.id_eq", ttInfo.getId());
ttInfoDetailIterator = ttInfoDetailRepository.findAll(new CustomerSpecs<TTInfoDetail>(param),
new Sort(Direction.ASC, "xlDir")).iterator();
// 获得lineInformation
param = new HashMap();
param.put("line.id_eq", ttInfo.getXl().getId());
lineInformation = lineInformationRepository.findOne(new CustomerSpecs<LineInformation>(param));
if(ttInfoDetailIterator.hasNext()){
sBuffer.append("<SKB>");
sBuffer.append("<XLBM>").append(BasicData.lineId2ShangHaiCodeMap.get(ttInfo.getXl().getId()))
.append("</XLBM>");
sBufferB = new StringBuffer();
sBufferC = new StringBuffer();
sBufferB.append("<KSRQ>").append(sdfnyr.format(ttInfo.getQyrq())).append("</KSRQ>");
// 结束日期暂时不要,节假日的班次表才需要,如春节的班次表
sBufferB.append("<JSRQ>").append("").append("</JSRQ>");
sBufferB.append("<ZJZX>").append(changeRuleDay(ttInfo.getRule_days())).append("</ZJZX>");
sBufferB.append("<TBYY>").append("").append("</TBYY>");
sBufferB.append("<UPDT>").append(sdfnyrsfm.format(new Date())).append("</UPDT>");
sBufferB.append("<BCList>");
while (ttInfoDetailIterator.hasNext()) {
ttInfoDetail = ttInfoDetailIterator.next();
bcType = ttInfoDetail.getBcType();
sxx = ttInfoDetail.getXlDir();
// 进出场班次
if(bcType.equals("in") || bcType.equals("out")){
// 进出班次的计划里程,算空驶里程
emptyLc = ttInfoDetail.getJhlc();
// 总里程需要加上空驶里程
zlc += emptyLc;
continue;
}
// 如果发车时间格式错误,忽略此条
if(changeTimeFormat(ttInfoDetail) == null){
continue;
}
sBufferC.append("<BC>");
sBufferC.append("<LPBH>").append(ttInfoDetail.getLp().getLpNo()).append("</LPBH>");
sBufferC.append("<SXX>").append(sxx).append("</SXX>");
sBufferC.append("<FCZDMC>").append(lsStationCode2NameMap.get(ttInfoDetail.getXl().getLineCode()+"_"+ttInfoDetail.getXlDir()
+"_"+ttInfoDetail.getQdzCode())).append("</FCZDMC>");
// 起点站的参数
otherMap.put("stationMark","B");
paramMap = packageYgcStationNumParam(ttInfoDetail,otherMap);
sBufferC.append("<ZDXH>").append(getYgcStationNumByLineCodeAndDirectionAndStationName(paramMap,lsStationName2YgcNumber)).append("</ZDXH>");
sBufferC.append("<JHFCSJ>").append(changeTimeFormat(ttInfoDetail)).append("</JHFCSJ>");
sBufferC.append("<DDZDMC>").append(lsStationCode2NameMap.get(ttInfoDetail.getXl().getLineCode()+"_"+ttInfoDetail.getXlDir()
+"_"+ttInfoDetail.getZdzCode())).append("</DDZDMC>");
// 起点站的参数
otherMap.put("stationMark","E");
paramMap = packageYgcStationNumParam(ttInfoDetail,otherMap);
sBufferC.append("<DDXH>").append(getYgcStationNumByLineCodeAndDirectionAndStationName(paramMap,lsStationName2YgcNumber)).append("</DDXH>");
sBufferC.append("<JHDDSJ>").append(calcDdsj(ttInfoDetail.getFcsj(),ttInfoDetail.getBcsj())).append("</JHDDSJ>");
sBufferC.append("</BC>");
// 0:上行;1:下行
if("0".equals(sxx)){
singleLc = lineInformation.getUpMileage();
}else{
singleLc = lineInformation.getDownMileage();
}
zlc += singleLc ;
yylc += singleLc;
}
sBufferC.append("</BCList>");
sBufferC.append("</SKB>");
sBufferA = new StringBuffer();
sBufferA.append("<JHZLC>").append(df.format(zlc)).append("</JHZLC>");
sBufferA.append("<JHYYLC>").append(df.format(yylc)).append("</JHYYLC>");
sBuffer.append(sBufferA).append(sBufferB).append(sBufferC);
}
}
sBuffer.append("</SKBs>");
if(ssop.setSKB(userNameOther, passwordOther, sBuffer.toString()).isSuccess()){
result = "success";
SKBUploadLogger skbUploadLogger ;
SysUser user = SecurityUtils.getCurrentUser();
// 保存时刻表上传记录
for(TTInfo ttInfo1 : ttinfoList){
skbUploadLogger = new SKBUploadLogger();
skbUploadLogger.setTtInfo(ttInfo1);
skbUploadLogger.setUser(user);
skbUploadLoggerRepository.save(skbUploadLogger);
}
}
} catch (Exception e) {
logger.error("setSKB:", e);
e.printStackTrace();
}finally{
logger.info("setSKB:"+sBuffer.toString());
logger.info("setSKB:"+result);
}
return result;
}
/**
* 上传线路人员车辆配置信息
*/
@Override
public String setXLPC() {
String result = "failure";
StringBuffer sBuffer =new StringBuffer();
try {
sBuffer.append("<XLPCs>");
// 声明变量
Line line = null;
Cars cars = null;
List<Personnel> personnelList = null;
List<Cars> carsList = null;
int totalPersonnel,totalCar ;// 人员数量。车辆数量
// 查询所有线路
Iterator<Line> lineIterator = lineRepository.findAll().iterator();
// 循环查找线路下的信息
while(lineIterator.hasNext()){
line = lineIterator.next();
sBuffer.append("<XLPC>");
sBuffer.append("<XLBM>").append(BasicData.lineId2ShangHaiCodeMap.get(line.getId())).append("</XLBM>");
// 查询驾驶员数量
personnelList = personnelRepository.findJsysByLineId(line.getId());
totalPersonnel = personnelList != null ? personnelList.size():0;
sBuffer.append("<SJRS>").append(totalPersonnel).append("</SJRS>");
// 查询售票员人员数量
personnelList = personnelRepository.findSpysByLineId(line.getId());
totalPersonnel = personnelList != null ? personnelList.size():0;
sBuffer.append("<SPYRS>").append(totalPersonnel).append("</SPYRS>");
// 查询车辆
carsList = carsRepository.findCarsByLineId(line.getId());
totalCar = carsList != null ? carsList.size():0;
sBuffer.append("<PCSL>").append(totalCar).append("</PCSL>");
sBuffer.append("<UPDT>").append(sdfnyrsfm.format(new Date())).append("</UPDT>");
int carsNum = 0;
// 取车牌号
if(carsList != null){
carsNum = carsList.size();
sBuffer.append("<CPHList>");
for (int i = 0; i < carsNum; i++) {
cars = carsList.get(i);
sBuffer.append("<CPH>").append("沪").append(cars.getCarCode()).append("</CPH>");
}
sBuffer.append("</CPHList>");
}
sBuffer.append("</XLPC>");
}
sBuffer.append("</XLPCs>");
if(ssop.setXLPC(userNameOther, passwordOther, sBuffer.toString()).isSuccess()){
result = "success";
}
} catch (Exception e) {
logger.error("setXLPC:",e);
e.printStackTrace();
}finally{
logger.info("setXLPC:"+sBuffer.toString());
logger.info("setXLPC:"+result);
}
return result;
}
/**
* 上传超速数据
*/
@Override
public String setCS() {
String result = "failure";
StringBuffer sBuffer =new StringBuffer();
sBuffer.append("<CSs>");
String sql = "SELECT * FROM bsth_c_speeding where DATE_FORMAT(create_date,'%Y-%m-%d') = ? order by create_date ";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 取昨天 的日期
String yesterday = sdfnyr.format(DateUtils.addDays(new Date(), -1));
try {
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, yesterday);
rs = ps.executeQuery();
Float lon, lat;
String kssk;
String speed;
while (rs.next()) {
kssk = sdfnyrsfm.format(rs.getLong("TIMESTAMP"));
speed = rs.getString("SPEED");
// 经纬度
lon = rs.getFloat("LON");
lat = rs.getFloat("LAT");
sBuffer.append("<CS>");
sBuffer.append("<RQ>").append(sdfnyr.format(rs.getDate("CREATE_DATE"))).append("</RQ>");
sBuffer.append("<XLBM>").append(BasicData.lineCode2ShangHaiCodeMap.get(rs.getString("LINE"))).append("</XLBM>");////////
sBuffer.append("<CPH>").append(rs.getString("VEHICLE")).append("</CPH>");
sBuffer.append("<KSSK>").append(kssk).append("</KSSK>");
sBuffer.append("<KSDDJD>").append(lon).append("</KSDDJD>");
sBuffer.append("<KSDDWD>").append(lat).append("</KSDDWD>");
sBuffer.append("<KSLD>").append("").append("</KSLD>");//**********************
sBuffer.append("<JSSK>").append(kssk).append("</JSSK>");
sBuffer.append("<JSDDJD>").append(lon).append("</JSDDJD>");
sBuffer.append("<JSDDWD>").append(lat).append("</JSDDWD>");
sBuffer.append("<JSLD>").append("").append("</JSLD>");//**********************
sBuffer.append("<PJSD>").append(speed).append("</PJSD>");
sBuffer.append("<ZGSS>").append(speed).append("</ZGSS>");
sBuffer.append("<UPDT>").append(sdfnyrsfm.format(new Date())).append("</UPDT>");
sBuffer.append("</CS>");
}
sBuffer.append("</CSs>");
if(ssop.setCS(userNameOther, passwordOther, sBuffer.toString()).isSuccess()){
result = "success";
}
} catch (Exception e) {
logger.error("setCS:",e);
e.printStackTrace();
} finally {
logger.info("setCS:"+sBuffer.toString());
logger.info("setCS:"+result);
DBUtils_MS.close(rs, ps, conn);
}
return result;
}
/**
* 下载全量的公交基础数据
*/
public String getDownLoadAllDataFile() {
String result = "failure";
try {
try {
if(ygcBasicDataService.download("admin","000000","abc.zip")){
result = "success";
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 下载增量的公交基础数据
*/
public String getDownLoadIncreaseDataFile() {
String result = "success";
try {
//System.out.println(portType.downloadIncreaseDataFile(args0, args1, args2));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 指定线路查询方式公交基础数据下载
*/
public String getDownLoadWarrantsBusLineStation() {
String result = "success";
try {
//portType.setXL(userNameXl, passwordXl, sBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 计算结束时间
* @param fcsj 发车时间
* @param bcsj 班次历时
* @return
*/
private String calcDdsj(String fcsj,Integer bcsj){
String result = "";
if(fcsj.indexOf(":") != -1){
if(bcsj == null){
return fcsj;
}
// 时和分隔开
String[] fcsjArray = fcsj.split(":");
// 分和历时时间相加
Integer fullTime = Integer.valueOf(fcsjArray[1])+ bcsj;
int hour,min,sumHour;
hour = fullTime / 60;
min = fullTime % 60;
sumHour = Integer.valueOf(fcsjArray[0])+hour;
if(sumHour >= 24){
result = String.format("%02d",sumHour - 24);
}else{
result = String.format("%02d",sumHour);;
}
result +=":"+String.format("%02d", min);
}else{
result = fcsj;
}
return result;
}
/**
* 改变时间格式
* @param ttInfoDetail 时刻表详细
* @return xx:yy
*/
private String changeTimeFormat(TTInfoDetail ttInfoDetail){
String result = "00:00";
String fcsj = ttInfoDetail.getFcsj();
if(fcsj.indexOf(":") != -1){
// 时和分隔开
String[] fcsjArray = fcsj.split(":");
result = String.format("%02d", Integer.valueOf(fcsjArray[0]))+":";
result +=String.format("%02d", Integer.valueOf(fcsjArray[1]));
}else{
result = null;
logger.info("setSKB:发车时间错误:ttInfoDetail.id="+ttInfoDetail.getId());
}
return result;
}
/**
* 拼装线路计划班次表的XML
* @param sBuffer
* @param schedulePlanInfo
* @param xlbm
* @param zbh
* @param lp
*/
private void assembleJHBC(StringBuffer sBuffer,SchedulePlanInfo schedulePlanInfo,String xlbm,String zbh,Long lp){
sBuffer.append("<JHBC>");
sBuffer.append("<RQ>").append(sdfnyr.format(schedulePlanInfo.getScheduleDate())).append("</RQ>");
sBuffer.append("<XLBM>").append(BasicData.lineCode2ShangHaiCodeMap.get(xlbm)).append("</XLBM>");
sBuffer.append("<CPH>").append("沪"+zbh).append("</CPH>");
sBuffer.append("<LPBH>").append(lp).append("</LPBH>");
sBuffer.append("<UPDT>").append(sdfnyrsfm.format(new Date())).append("</UPDT>");
sBuffer.append("<BCList>");
}
/**
* 转换排班规则
* @param ruleDay
* @return
*/
private String changeRuleDay(String ruleDay){
String result = "";
int flag ;
String[] ruleDayArray = ruleDay.split(",");
for (int i = 0; i < ruleDayArray.length; i++) {
if(ruleDayArray[i].equals("1")){
flag = i+1;
}else{
flag = 0;
}
if(flag > 0){
result += flag + ",";
}
}
// 去掉最后一个字符
if(StringUtils.endsWith(result,",")){
result = StringUtils.removeEnd(result,",");
}
return result;
}
/**
* 设置统一的公司名称
* @param company
*/
private void setCompanyName(String company){
if(company.equals("闵行公司")){
company = "浦东闵行公交公司";
}else if(company.equals("杨高公司")){
company = "浦东杨高公交公司";
}else if(company.equals("上南公司")){
company = "浦东上南公交公司";
}else if(company.equals("金高公司")){
company = "浦东金高公交公司";
}else if(company.equals("南汇公司")){
company = "浦东南汇公交公司";
}else if(company.equals("青浦公交")){
company = "浦东青浦公交公司";
}
}
/**
* @param stationsList 站点路由集
* @param sBuffer sBuffer
* @param startId 站点序号起始ID
*
* @return 站点序号累加后的ID
*/
private int packagStationXml(List<StationRoute> stationsList,StringBuffer sBuffer,int startId,Map<String, Integer> stationNumMap){
int size = stationsList.size();
StationRoute srRoute;
HashMap<String,String> paraMap;
String zdlx ;// 站点类型:0:起点站、1:终点站、2:中途站
for (int i = 0; i < size; i++) {
srRoute = stationsList.get(i);
zdlx = srRoute.getStationMark();
if(zdlx.equals("B")){
zdlx = "0";
}else if(zdlx.equals("E")){
zdlx = "1";
}else{
zdlx = "2";
}
paraMap = packageYgcStationNumParam(srRoute,null);
sBuffer.append("<Station>");
sBuffer.append("<ZDXH>").append(getYgcStationNumByLineCodeAndDirectionAndStationName(paraMap,stationNumMap)).append("</ZDXH>");
sBuffer.append("<SXX>").append(srRoute.getDirections()).append("</SXX>");
sBuffer.append("<ZDMC>").append(srRoute.getStationName()).append("</ZDMC>");
sBuffer.append("<ZDBM>").append(srRoute.getStationCode()).append("</ZDBM>");
sBuffer.append("<ZDJD>").append(srRoute.getStation().getgLonx()).append("</ZDJD>");
sBuffer.append("<ZDWD>").append(srRoute.getStation().getgLaty()).append("</ZDWD>");
sBuffer.append("<ZZ>").append(srRoute.getStation().getAddr() == null ? "" : srRoute.getStation().getAddr()).append("</ZZ>");
sBuffer.append("<ZDLX>").append(zdlx).append("</ZDLX>");
sBuffer.append("<ZJLC>").append(srRoute.getDistances()).append("</ZJLC>");
sBuffer.append("</Station>");
startId++;
}
return startId;
}
/**
* @param pointList 站点点位集
* @param sBuffer sBuffer
* @param startId 站点序号起始ID
*
* @return 站点序号累加后的ID
*/
private int packagStationPointXml(List<Object[]> pointList,StringBuffer sBuffer,int startId){
int size = pointList.size();
Object[] objs;
String bsection,dir,section;
String[] sections ;
for (int i = 0; i < size; i++) {
objs = pointList.get(i);
bsection = objs[0]+"";
dir = objs[1]+"";
// 取括号内的内容
Pattern pattern = Pattern.compile("(?<=\\()(.+?)(?=\\))");
Matcher matcher = pattern.matcher(bsection);
if(matcher.find()){
sections = matcher.group().split(",");
for (int j = 0 ; j < sections.length ; j ++){
section = sections[j];
sBuffer.append("<LinePoint>");
sBuffer.append("<ZDXH>").append(startId).append("</ZDXH>");
sBuffer.append("<SXX>").append(dir).append("</SXX>");
sBuffer.append("<ZDJD>").append(section.split(" ")[0]).append("</ZDJD>");
sBuffer.append("<ZDWD>").append(section.split(" ")[1]).append("</ZDWD>");
sBuffer.append("</LinePoint>");
startId++;
}
}
}
return startId;
}
/**
* 获取运管处站点序号
* @param map
* @return 运管处站点序号
*/
private Integer getYgcStationNumByLineCodeAndDirectionAndStationName(HashMap<String,String> map,Map<String, Integer> stationNumMap){
// 线路编码
String lineCode = map.get("lineCode");
// 线路走向 0:上行 1:下行
String direction = map.get("direction");
// 站点编码
String stationCode = map.get("stationCode");
// 站点类型:B:起点站 Z:中途站 E:终点站 T:停车场
String stationMark = map.get("stationMark");
String[] marks = null;
// 起点站,先从起点找,找不到再从中途站找,最后从终点找
if(stationMark.equals("B")){
marks= new String[]{"B","Z","E"};
}else if(stationMark.equals("E")){// 终点站相反
marks= new String[]{"E","Z","B"};
}else if(stationMark.equals("Z")){
marks= new String[]{"Z"};
}
// 默认从缓存BasicData.stationName2YgcNumber
Map<String, Integer> tempMap = BasicData.stationName2YgcNumber;
// 如果传入的stationNumMap不为空,则不是缓存取,而从stationNumMap取
if(stationNumMap != null){
tempMap = stationNumMap;
}
Integer number = null;
for (int i = 0 ;i < marks.length ; i ++){
number = tempMap.get(lineCode+"_"+direction+"_"+stationCode+"_"+marks[i]);
if(number != null){
break;
}
}
return number == null ? 0 : number;
}
/**
* 封装查询站序条件
* @param obj
* @return
*/
private HashMap packageYgcStationNumParam(Object obj,HashMap<String,String> otherParam){
HashMap<String,String> map = new HashMap<String,String>();
String lineCode = "",direction = "",stationCode = "",stationMark = "";
// 站点路由
if(obj instanceof StationRoute){
StationRoute sr = (StationRoute)obj;
lineCode = sr.getLineCode();
direction = String.valueOf(sr.getDirections());
stationCode = sr.getStationCode();
stationMark = sr.getStationMark();
}else if(obj instanceof ScheduleRealInfo){ //实际排班计划明细。
ScheduleRealInfo sri = (ScheduleRealInfo)obj;
lineCode = sri.getXlBm();
direction = sri.getXlDir();
if(otherParam != null && otherParam.get("stationMark") != null){
stationMark = otherParam.get("stationMark");
if(stationMark.equals("B")){ // 起点站
stationCode = sri.getQdzCode();
}else if(stationMark.equals("E")){ // 终点站
stationCode = sri.getZdzCode();
}
}
}else if(obj instanceof SchedulePlanInfo){ //排班计划明细
SchedulePlanInfo spi = (SchedulePlanInfo)obj;
lineCode = spi.getXlBm();
direction = spi.getXlDir();
if(otherParam != null && otherParam.get("stationMark") != null){
stationMark = otherParam.get("stationMark");
if(stationMark.equals("B")){ // 起点站
stationCode = spi.getQdzCode();
}else if(stationMark.equals("E")){ // 终点站
stationCode = spi.getZdzCode();
}
}
}else if(obj instanceof TTInfoDetail){ //时刻表明细
TTInfoDetail ttid = (TTInfoDetail)obj;
lineCode = ttid.getXl().getLineCode();
direction = ttid.getXlDir();
if(otherParam != null && otherParam.get("stationMark") != null){
stationMark = otherParam.get("stationMark");
if(stationMark.equals("B")){ // 起点站
stationCode = ttid.getQdzCode();
}else if(stationMark.equals("E")){ // 终点站
stationCode = ttid.getZdzCode();
}
}
}
map.put("lineCode",lineCode);// 站点编码
map.put("direction",direction); // 上下行
map.put("stationCode",stationCode); // 站点编号
map.put("stationMark",stationMark); // 站点类型
return map;
}
/**
* 取得历史站点编码和站点名称的对应关系
* @return
*/
private Map<String, String> getLsStationCode(String lineCode,int lineVersion){
Map<String,Object> map = new HashMap<>();
map.put("lineCode_eq", lineCode);
map.put("versions_eq",lineVersion);
LsStationRoute lsroute;
Iterator<LsStationRoute> iterator = lsStationRouteRepository.findAll(new CustomerSpecs<LsStationRoute>(map)).iterator();
Map<String, String> stationCode2Name = new HashMap<>();
while (iterator.hasNext()) {
lsroute = iterator.next();
stationCode2Name.put(lsroute.getLineCode() + "_" + lsroute.getDirections() + "_" + lsroute.getStationCode(), lsroute.getStationName());
}
return stationCode2Name;
}
private Map<String, Integer> getLsStationRoute(String xlbm,int lineVersion){
Map<String, Integer> tempStationName2YgcNumber = new HashMap<String, Integer>();
/**
* 加载运管处的站点及序号
* 上行从1开始,下行顺序续编
*/
List<Map<String, String>> ygcLines = lsStationRouteRepository.findLineWithLineCode4Ygc(xlbm,lineVersion);
if(ygcLines != null && ygcLines.size() > 0){
int size = ygcLines.size();
Map<String, String> tempMap ;
int num = 1;
String key;
String lineCode = "";
for (int i = 0; i < size; i ++){
tempMap = ygcLines.get(i);
if(lineCode.equals("")){
lineCode = tempMap.get("lineCode");
}else if(!lineCode.equals(tempMap.get("lineCode"))){
num = 1;
lineCode = tempMap.get("lineCode");
}
key = tempMap.get("lineCode") + "_"+String.valueOf(tempMap.get("directions"))
+ "_"+tempMap.get("stationCode")+ "_"+tempMap.get("stationMark");
tempStationName2YgcNumber.put(key,num++);
}
}
return tempStationName2YgcNumber;
}
}