DayOfSchedule.java
77.8 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
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
<<<<<<< HEAD
package com.bsth.data.schedule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.common.Constants;
import com.bsth.common.ResponseCode;
import com.bsth.data.LineConfigData;
import com.bsth.data.gpsdata_v2.GpsRealData;
import com.bsth.data.gpsdata_v2.utils.GpsDataRecovery;
import com.bsth.data.schedule.f_a_l.FirstAndLastHandler;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.service.schedule.SchedulePlanInfoService;
import com.bsth.util.ConfigUtil;
import com.bsth.websocket.handler.SendUtils;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
/**
* @author PanZhao
* @ClassName: DayOfSchedule
* @Description: TODO(当日实际排班)
* @date 2016年8月15日 上午10:16:12
*/
@Component
public class DayOfSchedule {
Logger logger = LoggerFactory.getLogger(this.getClass());
//按线路分组的 “原始计划” 排班数据
public static Map<String, List<SchedulePlanInfo>> schedulePlanMap;
// 按车辆索引的班次数据
private static ListMultimap<String, ScheduleRealInfo> nbbmScheduleMap;
// 按营运公司索引的班次数据
private static ListMultimap<String, ScheduleRealInfo> gsBmScheduleMap;
//按线路索引计划用车
private static HashMultimap<String, String> lineNbbmsMap;
//按路牌索引班次数据 线路编码_路牌名称 ——> 班次list
private static ArrayListMultimap<String, ScheduleRealInfo> lpScheduleMap;
// 班次主键映射
private static ConcurrentMap<Long, ScheduleRealInfo> id2SchedulMap;
//车辆 ——> 当前执行班次
private static ConcurrentMap<String, ScheduleRealInfo> carExecutePlanMap;
// 持久化
public static ConcurrentLinkedQueue<ScheduleRealInfo> pstBuffer;
// 排序器
private static ScheduleComparator.FCSJ schFCSJComparator;
private static ScheduleComparator.DFSJ schDFSJComparator;
private static Long sch_max_id=Long.parseLong(ConfigUtil.get("auto_increment_offset"));
@Autowired
LineConfigData lineConfigData;
@Autowired
ScheduleRealInfoRepository schRepository;
@Autowired
SchedulePlanInfoService schPlanService;
@Autowired
SchAttrCalculator schAttrCalculator;
@Autowired
SendUtils sendUtils;
@Autowired
GpsRealData gpsRealData;
/**
* 线路当前使用的排班的日期
*/
public static Map<String, String> currSchDateMap;
static {
nbbmScheduleMap = ArrayListMultimap.create();
nbbmScheduleMap = Multimaps.synchronizedListMultimap(nbbmScheduleMap);
gsBmScheduleMap = ArrayListMultimap.create();
lpScheduleMap = ArrayListMultimap.create();
lineNbbmsMap = HashMultimap.create();
id2SchedulMap = new ConcurrentHashMap<>();
pstBuffer = new ConcurrentLinkedQueue<>();
schFCSJComparator = new ScheduleComparator.FCSJ();
schDFSJComparator = new ScheduleComparator.DFSJ();
currSchDateMap = new HashMap<>();
carExecutePlanMap = new ConcurrentHashMap<>();
schedulePlanMap = new HashMap<>();
}
@Autowired
LineConfigData lineConfigs;
@Autowired
GpsDataRecovery gpsDataRecovery;
private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyy-MM-dd"), fmtHHmm = DateTimeFormat.forPattern("HH:mm");
//数据恢复
public void dataRecovery() {
Collection<LineConfig> confs = lineConfigs.getAll();
String lineCode, currSchDate;
for (LineConfig conf : confs) {
lineCode = conf.getLine().getLineCode();
currSchDate = calcSchDate(lineCode);
//加载班次数据
reloadSch(lineCode, currSchDate, false);
}
//恢复gps数据
gpsDataRecovery.recovery();
}
public Map<String, String> getCurrSchDate() {
return currSchDateMap;
}
/**
* @Title: calcSchDateB
* @Description: TODO(计算线路当前应该使用的排班日期)
*/
public String calcSchDate(String lineCode) {
Long t = System.currentTimeMillis();
LineConfig conf = lineConfigData.get(lineCode);
// 小于当天起始运营时间,则取前一天的排班
String ct = fmtHHmm.print(t);
if(ct.compareTo(conf.getStartOpt()) < 0)
t -= 1000 * 60 * 60 * 24;
String schDate = fmtyyyyMMdd.print(t);
return schDate;
}
/**
* @param @param lineCode 线路编码
* @param @param schDate 班次日期 yyyy-MM-dd
* @param @param forcePlan 强制从计划调度重新抓取
* @Title: reloadSch
* @Title: reloadSch
* @Description: TODO(重新载入排班)
*/
public int reloadSch(String lineCode, String schDate, boolean forcePlan) {
try {
List<ScheduleRealInfo> list;
if (forcePlan)
removeRealSch(lineCode, schDate);
else
clearRAMData(lineCode);
if (existRealSch(lineCode, schDate))
list = loadRealSch(lineCode, schDate);// 从实际排班表加载
else {
list = loadPlanSch(lineCode, schDate);// 从计划排班表加载
// 写入数据库
batchSave(list);
}
//更新线路和班次日期对照
currSchDateMap.put(lineCode, schDate);
//添加到缓存
putAll(list);
//标记首末班
FirstAndLastHandler.marks(list);
Set<String> lps = searchAllLP(list);
for (String lp : lps) {
//计算“起点站应到”时间
schAttrCalculator.calcQdzTimePlan(lpScheduleMap.get(lineCode + "_" + lp));
}
Set<String> cars = searchAllCars(list);
for (String nbbm : cars) {
//车辆 ——> 要执行的班次对照
reCalcExecPlan(nbbm);
}
//分组计划用车
reCalcLineNbbms();
// 页面 翻班通知
//sendUtils.shiftSchedule(lineCode);
} catch (Exception e) {
logger.error("", e);
return -1;
}
return 0;
}
public int reloadSch(String lineCode) {
return reloadSch(lineCode, calcSchDate(lineCode), true);
}
/**
* @Title: searchAllCars
* @Description: TODO(搜索班次集合中的车辆)
*/
private Set<String> searchAllCars(List<ScheduleRealInfo> list) {
Set<String> cars = new HashSet<>();
for (ScheduleRealInfo sch : list)
cars.add(sch.getClZbh());
return cars;
}
/**
* @Title: searchAllCars
* @Description: TODO(搜索班次集合中的路牌)
*/
private Set<String> searchAllLP(List<ScheduleRealInfo> list) {
Set<String> lps = new HashSet<>();
for (ScheduleRealInfo sch : list)
lps.add(sch.getLpName());
return lps;
}
private void putAll(List<ScheduleRealInfo> list) {
for (ScheduleRealInfo sch : list)
put(sch);
}
/**
* @param @param lineCode 线路编码
* @param @param schDate 班次日期 yyyy-MM-dd
* @Title: removeRealSch
* @Description: TODO(清除实际排班,包括数据库和内存数据)
*/
public void removeRealSch(String lineCode, String schDate) throws Exception {
try {
// 清理数据库数据
schRepository.deleteByLineCodeAndDate(lineCode + "", schDate);
// 清理内存数据
clearRAMData(lineCode + "");
} catch (Exception e) {
logger.error("removeRealSch error, " + lineCode + " -" + schDate, e);
throw e;
}
}
/**
* @Title: clearRAMData
* @Description: TODO(清理内存数据)
*/
public void clearRAMData(String lineCode) {
int count = 0;
List<ScheduleRealInfo> remList = new ArrayList<>();
Collection<ScheduleRealInfo> all = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : all) {
if (sch.getXlBm().equals(lineCode))
remList.add(sch);
}
for (ScheduleRealInfo sch : remList) {
if (null != sch) {
nbbmScheduleMap.remove(sch.getClZbh(), sch);
id2SchedulMap.remove(sch.getId());
count++;
//清理路牌对照
lpScheduleMap.removeAll(sch.getXlBm() + "_" + sch.getLpName());
//清除车辆 ——> 执行班次对照
carExecutePlanMap.remove(sch.getClZbh());
}
}
//清理计划排班
schedulePlanMap.remove(lineCode);
remList.clear();
remList = null;
logger.info(lineCode + "排班清理 " + count);
}
/**
* @Title: existRealSch
* @Description: TODO(实际排班是否已存在)
*/
public boolean existRealSch(String lineCode, String schDate) {
int count = schRepository.countByLineCodeAndDate(lineCode, schDate);
return count > 0;
}
/**
* @Title: loadRealSch
* @Description: TODO(从实际排班表加载数据)
*/
public List<ScheduleRealInfo> loadRealSch(String lineCode, String schDate) {
return schRepository.findByLineCodeAndDate(lineCode, schDate);
}
/**
* @Title: loadPlanSch
* @Description: TODO(从计划排班表加载数据)
*/
public List<ScheduleRealInfo> loadPlanSch(String lineCode, String schDate) {
logger.info("从计划排班表恢复排班,lineCode: " + lineCode + ", schDate: " + schDate);
List<ScheduleRealInfo> realList = new ArrayList<>();
try {
Map<String, Object> data = new HashMap<>();
data.put("scheduleDate_eq", fmtyyyyMMdd.parseDateTime(schDate).toDate());
data.put("xlBm_eq", lineCode);
// 查询计划排班
List<SchedulePlanInfo> planItr = cleanSchPlanItr(schPlanService.list(data).iterator());
//保存一份原始计划排班数据
schedulePlanMap.put(lineCode, planItr);
// 转换为实际排班
realList = JSONArray.parseArray(JSON.toJSONString(planItr), ScheduleRealInfo.class);
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String fcsj;
for (ScheduleRealInfo sch : realList) {
sch.setScheduleDateStr(fmtyyyyMMdd.print(sch.getScheduleDate().getTime()));
sch.setRealExecDate(sch.getScheduleDateStr());
sch.setCreateDate(d);
if (StringUtils.isEmpty(sch.getFcsj()))
sch.setFcsj("00:00");
if (sch.getFcsj().equals("24:00"))
sch.setFcsj("23:59");
if (sch.getFcsj().substring(0, 2).equals("24")) {
sch.setFcsj("00" + sch.getFcsj().substring(2));
}
fcsj = sch.getFcsj().trim();
//处理一下发车时间格式没有:号的问题
if (fcsj.indexOf(":") == -1 && fcsj.length() >= 4) {
sch.setFcsj(fcsj.substring(0, 2) + ":" + fcsj.substring(2, 4));
}
try {
sdf.parse(sch.getFcsj());
} catch (ParseException e) {
//发车时间仍然校验不过的,直接写成00:00
sch.setFcsj("00:00");
}
sch.setDfsj(sch.getFcsj());
// 计划终点时间
if (sch.getBcsj() != null) {
sch.setZdsj(fmtHHmm.print(fmtHHmm.parseMillis(sch.getFcsj()) + (sch.getBcsj() * 60 * 1000)));
sch.setLate(false);
}
//售票员为空设置为""字符串
if (StringUtils.isEmpty(sch.getsGh())) {
sch.setsGh("");
sch.setsName("");
}
sch.setJhlcOrig(sch.getJhlc());
//保留备注
if (StringUtils.isNotEmpty(sch.getRemark()))
sch.setRemarks(sch.getRemark());
}
} catch (Exception e) {
logger.error("", e);
}
return realList;
}
public synchronized long getId(){
long temp = sch_max_id;
if (sch_max_id < 0) {
sch_max_id = schRepository.getMaxId();
if(null == sch_max_id)
sch_max_id = 3000L;//留一点空间补数据用
if (temp == -1) {
sch_max_id += sch_max_id % 2 == 0 ? 1 : 0;
} else if (temp == -2) {
sch_max_id += sch_max_id % 2 == 0 ? 0 : 1;
}
} else {
sch_max_id += 2;
}
/*if(sch_max_id==-1){
sch_max_id = schRepository.getMaxId();
if(null == sch_max_id)
sch_max_id = 3000L;//留一点空间补数据用
sch_max_id += 5;
}
else
sch_max_id++;*/
return sch_max_id;
}
/**
* @Title: batchSave
* @Description: TODO(批量入库)
*/
private void batchSave(List<ScheduleRealInfo> list) {
SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
for (ScheduleRealInfo item : list) {
item.setSpId(item.getId());// 保留原始的计划ID
item.setId(getId());// 设置ID
item.setScheduleDateStr(sdfyyyyMMdd.format(item.getScheduleDate()));
}
//编程式事务
DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = tran.getTransaction(def);
try{
final List<ScheduleRealInfo> pstList = list;
//写入
jdbcTemplate.batchUpdate("insert into bsth_c_s_sp_info_real(id,bc_type,bcs,bcsj,cl_zbh,create_date,dfsj,directive_state,fcno,fcsj,fcsj_actual,j_gh,j_name,jhlc,lp_name,qdz_code,qdz_name,real_exec_date,remarks,s_gh,s_name,schedule_date,schedule_date_str,sflj,sp_id,status,update_date,xl_bm,xl_dir,xl_name,zdsj,zdsj_actual,zdz_code,zdz_name,ccno,df_auto,fgs_bm,fgs_name,gs_bm,gs_name,online,adjust_exps,reissue,jhlc_orig,sigin_compate,drift_status,cc_service,major_station_name)" +
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ScheduleRealInfo sch = pstList.get(i);
ps.setLong(1, sch.getId());
ps.setString(2, sch.getBcType());
ps.setInt(3, sch.getBcs()==null?0:sch.getBcs());
ps.setInt(4, sch.getBcsj()==null?0:sch.getBcsj());
ps.setString(5, sch.getClZbh());
ps.setTimestamp(6, new java.sql.Timestamp(sch.getCreateDate().getTime()));
ps.setString(7, sch.getDfsj());
ps.setInt(8, sch.getDirectiveState());
ps.setInt(9, sch.getFcno()==null?0:sch.getFcno());
ps.setString(10, sch.getFcsj());
ps.setString(11, sch.getFcsjActual());
ps.setString(12, sch.getjGh());
ps.setString(13, sch.getjName());
ps.setDouble(14, sch.getJhlc());
ps.setString(15, sch.getLpName());
ps.setString(16, sch.getQdzCode());
ps.setString(17, sch.getQdzName());
ps.setString(18, sch.getRealExecDate());
ps.setString(19, sch.getRemarks());
ps.setString(20, sch.getsGh());
ps.setString(21, sch.getsName());
ps.setTimestamp(22, new java.sql.Timestamp(sch.getScheduleDate().getTime()));
ps.setString(23, sch.getScheduleDateStr());
ps.setBoolean(24, sch.isSflj());
ps.setLong(25, sch.getSpId());
ps.setInt(26, sch.getStatus());
ps.setTimestamp(27, new java.sql.Timestamp(sch.getUpdateDate().getTime()));
ps.setString(28, sch.getXlBm());
ps.setString(29, sch.getXlDir());
ps.setString(30, sch.getXlName());
ps.setString(31, sch.getZdsj());
ps.setString(32, sch.getZdsjActual());
ps.setString(33, sch.getZdzCode());
ps.setString(34, sch.getZdzName());
ps.setInt(35, sch.getCcno()==null?0:sch.getCcno());
ps.setBoolean(36, sch.isDfAuto());
ps.setString(37, sch.getFgsBm());
ps.setString(38, sch.getFgsName());
ps.setString(39, sch.getGsBm());
ps.setString(40, sch.getGsName());
ps.setBoolean(41, sch.isOnline());
ps.setString(42, sch.getAdjustExps());
ps.setBoolean(43, sch.isReissue());
ps.setDouble(44, sch.getJhlcOrig());
ps.setInt(45, sch.getSiginCompate());
ps.setInt(46, sch.getDriftStatus());
ps.setBoolean(47, sch.isCcService());
ps.setString(48, sch.getMajorStationName());
}
@Override
public int getBatchSize() {
return pstList.size();
}
});
tran.commit(status);
}catch (Exception e){
tran.rollback(status);
logger.error("real schedule batchSave error...", e);
}
// 入库
//new BatchSaveUtils<ScheduleRealInfo>().saveList(list, ScheduleRealInfo.class);
}
public List<SchedulePlanInfo> cleanSchPlanItr(Iterator<SchedulePlanInfo> itrab) {
List<SchedulePlanInfo> list = new ArrayList<>();
SchedulePlanInfo sp;
while (itrab.hasNext()) {
sp = itrab.next();
sp.setSchedulePlan(null);
sp.setCreateBy(null);
sp.setUpdateBy(null);
list.add(sp);
}
return list;
}
/**
* @Title: findByLineCode
* @Description: TODO(lineCode 获取班次)
*/
public List<ScheduleRealInfo> findByLineCode(String lineCode) {
List<ScheduleRealInfo> rs = new ArrayList<>();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
if (sch.getXlBm().equals(lineCode))
rs.add(sch);
}
return rs;
}
/**
* @Title: findByLineCode
* @Description: TODO(lineList 获取班次)
*/
public Map<String, Collection<ScheduleRealInfo>> findByLineCodes(List<String> lineList) {
ArrayListMultimap<String, ScheduleRealInfo> mMap = ArrayListMultimap.create();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
if (lineList.contains(sch.getXlBm())) {
mMap.put(sch.getXlBm(), sch);
}
}
return mMap.asMap();
}
/**
* @Title: findCarByLineCode
* @Description: TODO(线路下运营的车辆)
*/
public Set<String> findCarByLineCode(String lineCode) {
/*Set<String> rs = new HashSet<>();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
if (sch.getXlBm().equals(lineCode))
rs.add(sch.getClZbh());
}
*/
return lineNbbmsMap.get(lineCode);
}
public List<ScheduleRealInfo> findByNbbm(String nbbm) {
return nbbmScheduleMap.get(nbbm);
}
/**
* @Title: findByLineAndUpDown
* @Description: TODO(lineCode 和走向获取班次)
*/
public List<ScheduleRealInfo> findByLineAndUpDown(String lineCode, Integer upDown) {
List<ScheduleRealInfo> list = findByLineCode(lineCode), rs = new ArrayList<>();
for (ScheduleRealInfo sch : list) {
if (sch.getXlDir().equals(upDown + ""))
rs.add(sch);
}
return rs;
}
public ScheduleRealInfo get(long id) {
return id2SchedulMap.get(id);
}
/**
* @Title: next
* @Description: TODO(下一个班次)
*/
public ScheduleRealInfo next(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
//排序
Collections.sort(list, schDFSJComparator);
return next(list, sch);
}
/**
* 下一个班次
*
* @param list 班次集合
* @param sch 当前班次
* @return
*/
private ScheduleRealInfo next(Collection<ScheduleRealInfo> list, ScheduleRealInfo sch) {
int outConfig = -1;
LineConfig config = lineConfigData.get(sch.getXlBm());
if (config != null)
outConfig = config.getOutConfig();
//限定出站既出场的停车场
List<String> parks = config.findTwinsParkList();
boolean limitPark = null != parks && parks.size() > 0;
boolean flag = false;
ScheduleRealInfo next = null;
for (ScheduleRealInfo temp : list) {
if (temp.getId() == sch.getId()) {
flag = true;
continue;
}
//忽略烂班
if (temp.isDestroy())
continue;
//出站既出场,忽略出场班次
if (outConfig == 2 && temp.getBcType().equals("out") && isEmptyMileage(temp)
&& (!limitPark || parks.contains(temp.getQdzCode())))
continue;
if (flag) {
next = temp;
break;
}
}
return next;
}
private boolean isEmptyMileage(ScheduleRealInfo sch) {
return sch.getBcsj() == 0 || sch.getJhlcOrig().intValue() == 0;
}
/**
* 下一个班次
*
* @param list 班次集合
* @param sch 当前班次
* @return
*/
private ScheduleRealInfo next2_lp(Collection<ScheduleRealInfo> list, ScheduleRealInfo sch) {
int outConfig = -1;
LineConfig config = lineConfigData.get(sch.getXlBm());
if (config != null)
outConfig = config.getOutConfig();
boolean flag = false;
ScheduleRealInfo next = null;
for (ScheduleRealInfo temp : list) {
if (temp.getId() == sch.getId()) {
flag = true;
continue;
}
if (flag) {
next = temp;
break;
}
}
return next;
}
private ScheduleRealInfo next3_lp(Collection<ScheduleRealInfo> list, ScheduleRealInfo sch) {
int outConfig = -1;
LineConfig config = lineConfigData.get(sch.getXlBm());
if (config != null)
outConfig = config.getOutConfig();
//限定出站既出场的停车场
List<String> parks = config.findTwinsParkList();
boolean limitPark = null != parks && parks.size() > 0;
boolean flag = false;
ScheduleRealInfo next = null;
for (ScheduleRealInfo temp : list) {
if (temp.getId() == sch.getId()) {
flag = true;
continue;
}
//出站既出场,忽略出场班次
if (outConfig == 2 && temp.getBcType().equals("out") && isEmptyMileage(temp)
&& (!limitPark || parks.contains(temp.getQdzCode())))
continue;
if (flag) {
next = temp;
break;
}
}
return next;
}
/**
* 上一个班次
*
* @param sch
* @return
*/
public ScheduleRealInfo prev(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
//boolean flag = false;
ScheduleRealInfo prev = null;
int size = list.size();
for (int i = 0; i < size; i++) {
if (list.get(i).isDestroy())
continue;
if (list.get(i).getId().equals(sch.getId())) {
return prev;
}
prev = list.get(i);
}
return prev;
}
/**
* 是否是首班出场
*
* @param sch
* @return
*/
public boolean isFirstOut(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
try {
if (list.get(0) == sch && sch.getBcType().equals("out"))
return true;
} catch (IndexOutOfBoundsException e) {
logger.error("小小的数组越界,无伤大雅!");
}
return false;
}
public void put(ScheduleRealInfo sch) {
schAttrCalculator
.calcRealDate(sch)
.calcAllTimeByFcsj(sch);
nbbmScheduleMap.put(sch.getClZbh(), sch);
//主键索引
id2SchedulMap.put(sch.getId(), sch);
//路牌对照表
addLPMapp(sch);
//跨24点的,再save一次
if (!sch.getRealExecDate().equals(sch.getScheduleDateStr()))
save(sch);
}
public void addLPMapp(ScheduleRealInfo sch) {
lpScheduleMap.put(sch.getXlBm() + "_" + sch.getLpName(), sch);
}
public void delete(ScheduleRealInfo sch) {
if (!sch.isSflj())
return;
nbbmScheduleMap.remove(sch.getClZbh(), sch);
id2SchedulMap.remove(sch.getId());
lpScheduleMap.remove(sch.getXlBm() + "_" + sch.getLpName(), sch);
//如果正在执行该班次
if (carExecutePlanMap.get(sch.getClZbh()) == sch) {
//重新计算车辆当前执行班次
reCalcExecPlan(sch.getClZbh());
}
}
public List<ScheduleRealInfo> updateQdzTimePlan(String lpName) {
List<ScheduleRealInfo> list = lpScheduleMap.get(lpName);
Collections.sort(list, schFCSJComparator);
return schAttrCalculator.updateQdzTimePlan(list);
}
public List<ScheduleRealInfo> updateQdzTimePlan(ScheduleRealInfo sch) {
return updateQdzTimePlan(sch.getXlBm() + "_" + sch.getLpName());
}
/**
* @Title: doneSum
* @Description: TODO(已完成班次总数)
*/
public int doneSum(String clZbh) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(clZbh);
int rs = 0;
for (ScheduleRealInfo sch : list) {
if (sch.getStatus() == 2 && !sch.isDestroy())
rs++;
}
return rs;
}
public void save(ScheduleRealInfo sch) {
sch.setUpdateDate(new Date());
pstBuffer.add(sch);
}
/**
* @Title: nextByBcType
* @Description: TODO(获取下一个指定班次类型的班次)
*/
public ScheduleRealInfo nextByBcType(String nbbm, String bcType) {
List<ScheduleRealInfo> list = findByBcType(nbbm, bcType);
Collections.sort(list, schFCSJComparator);
ScheduleRealInfo sch = null;
for (ScheduleRealInfo temp : list) {
if (temp.getFcsjActual() == null)
sch = temp;
}
return sch;
}
/**
* 搜索离当前时间最近的一个指定类型的班次
*
* @param nbbm
* @param bcType
* @return
*/
public ScheduleRealInfo searchNearByBcType(String nbbm, String bcType) {
List<ScheduleRealInfo> list = findByBcType(nbbm, bcType);
Collections.sort(list, schFCSJComparator);
long t = System.currentTimeMillis();
int distance = -1, diff;
ScheduleRealInfo sch = null;
for (ScheduleRealInfo temp : list) {
diff = (int) Math.abs(temp.getDfsjT() - t);
if (diff < distance || distance == -1) {
sch = temp;
distance = diff;
}
}
return sch;
}
public List<ScheduleRealInfo> findByBcType(String nbbm, String bcType) {
List<ScheduleRealInfo> all = nbbmScheduleMap.get(nbbm), outList = new ArrayList<>();
for (ScheduleRealInfo sch : all) {
if (sch.getBcType().equals(bcType))
outList.add(sch);
}
return outList;
}
public Collection<ScheduleRealInfo> findAll() {
return nbbmScheduleMap.values();
}
public Collection<ScheduleRealInfo> findAllByLpContainer() {
return lpScheduleMap.values();
}
public Collection<ScheduleRealInfo> findAllByIdContainer() {
return id2SchedulMap.values();
}
public int getPstSize() {
return pstBuffer.size();
}
public boolean addExecPlan(ScheduleRealInfo sch) {
ScheduleRealInfo oldExec = executeCurr(sch.getClZbh());
if (sch != null){
if(sch.getStatus()==2)
reCalcExecPlan(sch.getClZbh());
else
carExecutePlanMap.put(sch.getClZbh(), sch);
}
else
carExecutePlanMap.remove(sch.getClZbh());
return executeCurr(sch.getClZbh()) != oldExec;
}
public void removeExecPlan(String clzbh) {
carExecutePlanMap.remove(clzbh);
}
public Map<String, ScheduleRealInfo> execPlanMap() {
return carExecutePlanMap;
}
/**
* 车辆当前执行的班次
*
* @param nbbm
* @return
*/
public ScheduleRealInfo executeCurr(String nbbm) {
if(StringUtils.isEmpty(nbbm))
return null;
return carExecutePlanMap.get(nbbm);
}
/**
* @param @param sch
* @param @param newClZbh 新的车辆自编号
* @Title: changeCar
* @Description: TODO(班次换车) 返回有更新的班次
*/
public List<ScheduleRealInfo> changeCar(ScheduleRealInfo sch, String newClZbh) {
List<ScheduleRealInfo> ups = new ArrayList<>();
String oldClZbh = sch.getClZbh();
//变更相关映射信息
nbbmScheduleMap.remove(sch.getClZbh(), sch);
sch.setClZbh(newClZbh);
if (!nbbmScheduleMap.containsEntry(newClZbh, sch)) {
nbbmScheduleMap.put(newClZbh, sch);
}
//重新计算车辆当前执行班次
reCalcExecPlan(newClZbh);
reCalcExecPlan(oldClZbh);
//重新分组计划用车
reCalcLineNbbms();
return ups;
}
public void removeNbbm2SchMapp(ScheduleRealInfo sch) {
nbbmScheduleMap.remove(sch.getClZbh(), sch);
}
public void addNbbm2SchMapp(ScheduleRealInfo sch) {
nbbmScheduleMap.put(sch.getClZbh(), sch);
}
public void reCalcExecPlan(String nbbm) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(nbbm);
Collections.sort(list, schDFSJComparator);
ScheduleRealInfo sch = schAttrCalculator.calcCurrentExecSch(list);
if(null != sch)
carExecutePlanMap.put(nbbm, sch);
else
carExecutePlanMap.remove(nbbm);
}
/**
* 空驶任务?
* 出场、进场、直放、两点间空驶
* @param sch
* @return
*/
public static boolean emptyService(ScheduleRealInfo sch){
String type = sch.getBcType();
return type.equals("out") || type.equals("in") || type.equals("venting") || type.equals("ldks");
}
@Autowired
JdbcTemplate jdbcTemplate;
/**
* 删除实际排班
*
* @param lineCode
* @return
*/
public Map<String, Object> deleteRealSchedule(String lineCode) {
Map<String, Object> rs = new HashMap<>();
try {
String rq = currSchDateMap.get(lineCode);
if (StringUtils.isNotEmpty(rq)) {
List<ScheduleRealInfo> all = findByLineCode(lineCode);
if(null != all && all.size() > 0){
//解除gps 和班次之间的关联
List<ScheduleRealInfo> unions = calcUnion(all, carExecutePlanMap.values());
for (ScheduleRealInfo sch : unions) {
removeExecPlan(sch.getClZbh());
}
//解除调度指令和班次的外键约束
StringBuilder inStr = new StringBuilder("(");
for (ScheduleRealInfo sch : all) {
inStr.append(sch.getId() + ",");
}
inStr.deleteCharAt(inStr.length() - 1).append(")");
jdbcTemplate.update(Constants.MULTI_REMOVE_DIRECTIVE_SCH_FK + " " + inStr.toString());
}
//删除班次数据
removeRealSch(lineCode, rq);
}
rs.put("status", ResponseCode.SUCCESS);
} catch (Exception e) {
logger.error("", e);
rs.put("status", ResponseCode.ERROR);
if (e instanceof DataIntegrityViolationException)
rs.put("msg", "失败,违反数据约束!!");
else
rs.put("msg", e.getMessage());
}
return rs;
}
/**
* 计算并集
*
* @param all
* @param sub
* @return
*/
public List<ScheduleRealInfo> calcUnion(Collection<ScheduleRealInfo> c1, Collection<ScheduleRealInfo> c2) {
List<ScheduleRealInfo> rs = new ArrayList<>();
for (ScheduleRealInfo sch : c1) {
if (c2.contains(sch)) {
rs.add(sch);
}
}
return rs;
}
/**
* 覆盖一辆车的所有班次
*
* @param nbbm
* @param sets
*/
public void replaceByNbbm(String nbbm, Collection<ScheduleRealInfo> sets) {
nbbmScheduleMap.removeAll(nbbm);
nbbmScheduleMap.putAll(nbbm, sets);
}
/**
* 获取该班次所在路牌的下一个班次
*
* @param sch
* @return
*/
public ScheduleRealInfo nextByLp(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = lpScheduleMap.get(sch.getXlBm() + "_" + sch.getLpName());
Collections.sort(list, schFCSJComparator);
return next3_lp(list, sch);
}
/**
* 获取该班次所在路牌的下一个班次,不考虑场既是站
*
* @param sch
* @return
*/
public ScheduleRealInfo nextByLp2(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = lpScheduleMap.get(sch.getXlBm() + "_" + sch.getLpName());
Collections.sort(list, schFCSJComparator);
return next2_lp(list, sch);
}
public ArrayListMultimap<String, ScheduleRealInfo> getLpScheduleMap() {
return lpScheduleMap;
}
/**
* 重新全量计算路牌下的班次关联性
* 临时性的函数
*/
public void _test_reCalcLpSch() {
Map<String ,Collection<ScheduleRealInfo>> map = lpScheduleMap.asMap();
Set<String> ks = map.keySet();
for(String k : ks){
schAttrCalculator.calcQdzTimePlan(new ArrayList<ScheduleRealInfo>(map.get(k)));
}
}
public int dbCount(String lineCode, String currSchDate) {
int count = -1;
try{
count = jdbcTemplate.queryForObject("select count(*) from bsth_c_s_sp_info_real where schedule_date='"+currSchDate+"' and xl_bm='"+lineCode+"'", java.lang.Integer.class);
}catch (Exception e){
logger.error("", e);
}
return count;
}
/**
* 重新计算ID对照map
*/
public int reCalcIdMaps(){
Collection<ScheduleRealInfo> all = findAll();
ConcurrentMap<Long, ScheduleRealInfo> id2SchedulMapCopy = new ConcurrentHashMap<>();
for(ScheduleRealInfo sch : all){
id2SchedulMapCopy.put(sch.getId(), sch);
}
id2SchedulMap = id2SchedulMapCopy;
return id2SchedulMap.size();
}
/**
* 重新计算线路计划用车
*/
public void reCalcLineNbbms(){
HashMultimap<String, String> multimap = HashMultimap.create();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
multimap.put(sch.getXlBm(), sch.getClZbh());
}
lineNbbmsMap = multimap;
}
public String sizeString(){
return id2SchedulMap.size() + "/" + nbbmScheduleMap.size();
}
/**
* 按公司编码分组数据
*/
public void groupByGsbm(){
Collection<ScheduleRealInfo> all = findAll();
ListMultimap<String, ScheduleRealInfo> gsBmMaps = ArrayListMultimap.create();
for(ScheduleRealInfo sch : all){
gsBmMaps.put(sch.getGsBm(), sch);
}
if(gsBmMaps.size() > 0){
gsBmScheduleMap = null;
gsBmScheduleMap = gsBmMaps;
}
}
public Collection<ScheduleRealInfo> findByGsbm(String gsbm){
return gsBmScheduleMap.get(gsbm);
}
=======
package com.bsth.data.schedule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.common.Constants;
import com.bsth.common.ResponseCode;
import com.bsth.data.LineConfigData;
import com.bsth.data.gpsdata_v2.GpsRealData;
import com.bsth.data.gpsdata_v2.utils.GpsDataRecovery;
import com.bsth.data.schedule.f_a_l.FirstAndLastHandler;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.report.RepairReport;
import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.service.schedule.SchedulePlanInfoService;
import com.bsth.websocket.handler.SendUtils;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
/**
* @author PanZhao
* @ClassName: DayOfSchedule
* @Description: TODO(当日实际排班)
* @date 2016年8月15日 上午10:16:12
*/
@Component
public class DayOfSchedule {
Logger logger = LoggerFactory.getLogger(this.getClass());
//按线路分组的 “原始计划” 排班数据
public static Map<String, List<SchedulePlanInfo>> schedulePlanMap;
// 按车辆索引的班次数据
private static ListMultimap<String, ScheduleRealInfo> nbbmScheduleMap;
// 按营运公司索引的班次数据
private static ListMultimap<String, ScheduleRealInfo> gsBmScheduleMap;
//按线路索引计划用车
private static HashMultimap<String, String> lineNbbmsMap;
//按路牌索引班次数据 线路编码_路牌名称 ——> 班次list
private static ArrayListMultimap<String, ScheduleRealInfo> lpScheduleMap;
// 班次主键映射
private static ConcurrentMap<Long, ScheduleRealInfo> id2SchedulMap;
//车辆 ——> 当前执行班次
private static ConcurrentMap<String, ScheduleRealInfo> carExecutePlanMap;
// 持久化
public static ConcurrentLinkedQueue<ScheduleRealInfo> pstBuffer;
// 排序器
private static ScheduleComparator.FCSJ schFCSJComparator;
private static ScheduleComparator.DFSJ schDFSJComparator;
private static Long sch_max_id=-1L;
private Map<String, RepairReport> incode2report = new ConcurrentHashMap<String, RepairReport>();
@Autowired
LineConfigData lineConfigData;
@Autowired
ScheduleRealInfoRepository schRepository;
@Autowired
SchedulePlanInfoService schPlanService;
@Autowired
SchAttrCalculator schAttrCalculator;
@Autowired
SendUtils sendUtils;
@Autowired
GpsRealData gpsRealData;
/**
* 线路当前使用的排班的日期
*/
public static Map<String, String> currSchDateMap;
static {
nbbmScheduleMap = ArrayListMultimap.create();
nbbmScheduleMap = Multimaps.synchronizedListMultimap(nbbmScheduleMap);
gsBmScheduleMap = ArrayListMultimap.create();
lpScheduleMap = ArrayListMultimap.create();
lineNbbmsMap = HashMultimap.create();
id2SchedulMap = new ConcurrentHashMap<>();
pstBuffer = new ConcurrentLinkedQueue<>();
schFCSJComparator = new ScheduleComparator.FCSJ();
schDFSJComparator = new ScheduleComparator.DFSJ();
currSchDateMap = new HashMap<>();
carExecutePlanMap = new ConcurrentHashMap<>();
schedulePlanMap = new HashMap<>();
}
@Autowired
LineConfigData lineConfigs;
@Autowired
GpsDataRecovery gpsDataRecovery;
private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyy-MM-dd"), fmtHHmm = DateTimeFormat.forPattern("HH:mm");
//数据恢复
public void dataRecovery() {
Collection<LineConfig> confs = lineConfigs.getAll();
String lineCode, currSchDate;
for (LineConfig conf : confs) {
lineCode = conf.getLine().getLineCode();
currSchDate = calcSchDate(lineCode);
//加载班次数据
reloadSch(lineCode, currSchDate, false);
}
//恢复gps数据
gpsDataRecovery.recovery();
}
public Map<String, String> getCurrSchDate() {
return currSchDateMap;
}
/**
* @Title: calcSchDateB
* @Description: TODO(计算线路当前应该使用的排班日期)
*/
public String calcSchDate(String lineCode) {
Long t = System.currentTimeMillis();
LineConfig conf = lineConfigData.get(lineCode);
// 小于当天起始运营时间,则取前一天的排班
String ct = fmtHHmm.print(t);
if(ct.compareTo(conf.getStartOpt()) < 0)
t -= 1000 * 60 * 60 * 24;
String schDate = fmtyyyyMMdd.print(t);
return schDate;
}
/**
* @param @param lineCode 线路编码
* @param @param schDate 班次日期 yyyy-MM-dd
* @param @param forcePlan 强制从计划调度重新抓取
* @Title: reloadSch
* @Title: reloadSch
* @Description: TODO(重新载入排班)
*/
public int reloadSch(String lineCode, String schDate, boolean forcePlan) {
try {
List<ScheduleRealInfo> list;
if (forcePlan)
removeRealSch(lineCode, schDate);
else
clearRAMData(lineCode);
if (existRealSch(lineCode, schDate))
list = loadRealSch(lineCode, schDate);// 从实际排班表加载
else {
list = loadPlanSch(lineCode, schDate);// 从计划排班表加载
// 写入数据库
batchSave(list);
}
//更新线路和班次日期对照
currSchDateMap.put(lineCode, schDate);
//添加到缓存
putAll(list);
//标记首末班
FirstAndLastHandler.marks(list);
Set<String> lps = searchAllLP(list);
for (String lp : lps) {
//计算“起点站应到”时间
schAttrCalculator.calcQdzTimePlan(lpScheduleMap.get(lineCode + "_" + lp));
}
Set<String> cars = searchAllCars(list);
for (String nbbm : cars) {
//车辆 ——> 要执行的班次对照
reCalcExecPlan(nbbm);
}
//分组计划用车
reCalcLineNbbms();
// 页面 翻班通知
//sendUtils.shiftSchedule(lineCode);
} catch (Exception e) {
logger.error("", e);
return -1;
}
return 0;
}
public int reloadSch(String lineCode) {
return reloadSch(lineCode, calcSchDate(lineCode), true);
}
/**
* @Title: searchAllCars
* @Description: TODO(搜索班次集合中的车辆)
*/
private Set<String> searchAllCars(List<ScheduleRealInfo> list) {
Set<String> cars = new HashSet<>();
for (ScheduleRealInfo sch : list)
cars.add(sch.getClZbh());
return cars;
}
/**
* @Title: searchAllCars
* @Description: TODO(搜索班次集合中的路牌)
*/
private Set<String> searchAllLP(List<ScheduleRealInfo> list) {
Set<String> lps = new HashSet<>();
for (ScheduleRealInfo sch : list)
lps.add(sch.getLpName());
return lps;
}
private void putAll(List<ScheduleRealInfo> list) {
for (ScheduleRealInfo sch : list)
put(sch);
}
/**
* @param @param lineCode 线路编码
* @param @param schDate 班次日期 yyyy-MM-dd
* @Title: removeRealSch
* @Description: TODO(清除实际排班,包括数据库和内存数据)
*/
public void removeRealSch(String lineCode, String schDate) throws Exception {
try {
// 清理数据库数据
schRepository.deleteByLineCodeAndDate(lineCode + "", schDate);
// 清理内存数据
clearRAMData(lineCode + "");
} catch (Exception e) {
logger.error("removeRealSch error, " + lineCode + " -" + schDate, e);
throw e;
}
}
/**
* @Title: clearRAMData
* @Description: TODO(清理内存数据)
*/
public void clearRAMData(String lineCode) {
int count = 0;
List<ScheduleRealInfo> remList = new ArrayList<>();
Collection<ScheduleRealInfo> all = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : all) {
if (sch.getXlBm().equals(lineCode))
remList.add(sch);
}
for (ScheduleRealInfo sch : remList) {
if (null != sch) {
nbbmScheduleMap.remove(sch.getClZbh(), sch);
id2SchedulMap.remove(sch.getId());
count++;
//清理路牌对照
lpScheduleMap.removeAll(sch.getXlBm() + "_" + sch.getLpName());
//清除车辆 ——> 执行班次对照
carExecutePlanMap.remove(sch.getClZbh());
}
}
//清理计划排班
schedulePlanMap.remove(lineCode);
remList.clear();
remList = null;
logger.info(lineCode + "排班清理 " + count);
}
/**
* @Title: existRealSch
* @Description: TODO(实际排班是否已存在)
*/
public boolean existRealSch(String lineCode, String schDate) {
int count = schRepository.countByLineCodeAndDate(lineCode, schDate);
return count > 0;
}
/**
* @Title: loadRealSch
* @Description: TODO(从实际排班表加载数据)
*/
public List<ScheduleRealInfo> loadRealSch(String lineCode, String schDate) {
return schRepository.findByLineCodeAndDate(lineCode, schDate);
}
/**
* @Title: loadPlanSch
* @Description: TODO(从计划排班表加载数据)
*/
public List<ScheduleRealInfo> loadPlanSch(String lineCode, String schDate) {
//logger.info("从计划排班表恢复排班,lineCode: " + lineCode + ", schDate: " + schDate);
List<ScheduleRealInfo> realList = new ArrayList<>();
try {
Map<String, Object> data = new HashMap<>();
data.put("scheduleDate_eq", fmtyyyyMMdd.parseDateTime(schDate).toDate());
data.put("xlBm_eq", lineCode);
// 查询计划排班
List<SchedulePlanInfo> planItr = cleanSchPlanItr(schPlanService.list(data).iterator());
//保存一份原始计划排班数据
schedulePlanMap.put(lineCode, planItr);
// 转换为实际排班
realList = JSONArray.parseArray(JSON.toJSONString(planItr), ScheduleRealInfo.class);
logger.info("从计划排班表恢复排班,lineCode: " + lineCode + ", schDate: " + schDate + ", size:" + realList.size());
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String fcsj;
for (ScheduleRealInfo sch : realList) {
sch.setScheduleDateStr(fmtyyyyMMdd.print(sch.getScheduleDate().getTime()));
sch.setRealExecDate(sch.getScheduleDateStr());
sch.setCreateDate(d);
if (StringUtils.isEmpty(sch.getFcsj()))
sch.setFcsj("00:00");
if (sch.getFcsj().equals("24:00"))
sch.setFcsj("23:59");
if (sch.getFcsj().substring(0, 2).equals("24")) {
sch.setFcsj("00" + sch.getFcsj().substring(2));
}
fcsj = sch.getFcsj().trim();
//处理一下发车时间格式没有:号的问题
if (fcsj.indexOf(":") == -1 && fcsj.length() >= 4) {
sch.setFcsj(fcsj.substring(0, 2) + ":" + fcsj.substring(2, 4));
}
try {
sdf.parse(sch.getFcsj());
} catch (ParseException e) {
//发车时间仍然校验不过的,直接写成00:00
sch.setFcsj("00:00");
}
sch.setDfsj(sch.getFcsj());
// 计划终点时间
if (sch.getBcsj() != null) {
sch.setZdsj(fmtHHmm.print(fmtHHmm.parseMillis(sch.getFcsj()) + (sch.getBcsj() * 60 * 1000)));
sch.setLate(false);
}
//售票员为空设置为""字符串
if (StringUtils.isEmpty(sch.getsGh())) {
sch.setsGh("");
sch.setsName("");
}
sch.setJhlcOrig(sch.getJhlc());
//保留备注
if (StringUtils.isNotEmpty(sch.getRemark()))
sch.setRemarks(sch.getRemark());
}
} catch (Exception e) {
logger.error("", e);
}
return realList;
}
public synchronized long getId(){
if(sch_max_id==-1){
sch_max_id = schRepository.getMaxId();
if(null == sch_max_id)
sch_max_id = 3000L;//留一点空间补数据用
sch_max_id += 5;
}
else
sch_max_id ++;
return sch_max_id;
}
/**
* @Title: batchSave
* @Description: TODO(批量入库)
*/
private void batchSave(List<ScheduleRealInfo> list) {
SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
for (ScheduleRealInfo item : list) {
item.setSpId(item.getId());// 保留原始的计划ID
item.setId(getId());// 设置ID
item.setScheduleDateStr(sdfyyyyMMdd.format(item.getScheduleDate()));
}
//编程式事务
DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = tran.getTransaction(def);
try{
final List<ScheduleRealInfo> pstList = list;
//写入
jdbcTemplate.batchUpdate("insert into bsth_c_s_sp_info_real(id,bc_type,bcs,bcsj,cl_zbh,create_date,dfsj,directive_state,fcno,fcsj,fcsj_actual,j_gh,j_name,jhlc,lp_name,qdz_code,qdz_name,real_exec_date,remarks,s_gh,s_name,schedule_date,schedule_date_str,sflj,sp_id,status,update_date,xl_bm,xl_dir,xl_name,zdsj,zdsj_actual,zdz_code,zdz_name,ccno,df_auto,fgs_bm,fgs_name,gs_bm,gs_name,online,adjust_exps,reissue,jhlc_orig,sigin_compate,drift_status,cc_service,major_station_name,rfid_state,first_rfid_state)" +
" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ScheduleRealInfo sch = pstList.get(i);
ps.setLong(1, sch.getId());
ps.setString(2, sch.getBcType());
ps.setInt(3, sch.getBcs()==null?0:sch.getBcs());
ps.setInt(4, sch.getBcsj()==null?0:sch.getBcsj());
ps.setString(5, sch.getClZbh());
ps.setTimestamp(6, new java.sql.Timestamp(sch.getCreateDate().getTime()));
ps.setString(7, sch.getDfsj());
ps.setInt(8, sch.getDirectiveState());
ps.setInt(9, sch.getFcno()==null?0:sch.getFcno());
ps.setString(10, sch.getFcsj());
ps.setString(11, sch.getFcsjActual());
ps.setString(12, sch.getjGh());
ps.setString(13, sch.getjName());
ps.setDouble(14, sch.getJhlc());
ps.setString(15, sch.getLpName());
ps.setString(16, sch.getQdzCode());
ps.setString(17, sch.getQdzName());
ps.setString(18, sch.getRealExecDate());
ps.setString(19, sch.getRemarks());
ps.setString(20, sch.getsGh());
ps.setString(21, sch.getsName());
ps.setTimestamp(22, new java.sql.Timestamp(sch.getScheduleDate().getTime()));
ps.setString(23, sch.getScheduleDateStr());
ps.setBoolean(24, sch.isSflj());
ps.setLong(25, sch.getSpId());
ps.setInt(26, sch.getStatus());
ps.setTimestamp(27, new java.sql.Timestamp(sch.getUpdateDate().getTime()));
ps.setString(28, sch.getXlBm());
ps.setString(29, sch.getXlDir());
ps.setString(30, sch.getXlName());
ps.setString(31, sch.getZdsj());
ps.setString(32, sch.getZdsjActual());
ps.setString(33, sch.getZdzCode());
ps.setString(34, sch.getZdzName());
ps.setInt(35, sch.getCcno()==null?0:sch.getCcno());
ps.setBoolean(36, sch.isDfAuto());
ps.setString(37, sch.getFgsBm());
ps.setString(38, sch.getFgsName());
ps.setString(39, sch.getGsBm());
ps.setString(40, sch.getGsName());
ps.setBoolean(41, sch.isOnline());
ps.setString(42, sch.getAdjustExps());
ps.setBoolean(43, sch.isReissue());
ps.setDouble(44, sch.getJhlcOrig());
ps.setInt(45, sch.getSiginCompate());
ps.setInt(46, sch.getDriftStatus());
ps.setBoolean(47, sch.isCcService());
ps.setString(48, sch.getMajorStationName());
ps.setInt(49, sch.getRfidState());
ps.setInt(50, sch.getFirstRfidState());
}
@Override
public int getBatchSize() {
return pstList.size();
}
});
tran.commit(status);
}catch (Exception e){
tran.rollback(status);
logger.error("real schedule batchSave error...", e);
}
// 入库
//new BatchSaveUtils<ScheduleRealInfo>().saveList(list, ScheduleRealInfo.class);
}
public List<SchedulePlanInfo> cleanSchPlanItr(Iterator<SchedulePlanInfo> itrab) {
List<SchedulePlanInfo> list = new ArrayList<>();
SchedulePlanInfo sp;
while (itrab.hasNext()) {
sp = itrab.next();
sp.setSchedulePlan(null);
sp.setCreateBy(null);
sp.setUpdateBy(null);
list.add(sp);
}
return list;
}
/**
* @Title: findByLineCode
* @Description: TODO(lineCode 获取班次)
*/
public List<ScheduleRealInfo> findByLineCode(String lineCode) {
List<ScheduleRealInfo> rs = new ArrayList<>();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
if (sch.getXlBm().equals(lineCode))
rs.add(sch);
}
return rs;
}
/**
* @Title: findByLineCode
* @Description: TODO(lineList 获取班次)
*/
public Map<String, Collection<ScheduleRealInfo>> findByLineCodes(List<String> lineList) {
ArrayListMultimap<String, ScheduleRealInfo> mMap = ArrayListMultimap.create();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
if (lineList.contains(sch.getXlBm())) {
mMap.put(sch.getXlBm(), sch);
}
}
return mMap.asMap();
}
/**
* @Title: findCarByLineCode
* @Description: TODO(线路下运营的车辆)
*/
public Set<String> findCarByLineCode(String lineCode) {
/*Set<String> rs = new HashSet<>();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
if (sch.getXlBm().equals(lineCode))
rs.add(sch.getClZbh());
}
*/
return lineNbbmsMap.get(lineCode);
}
public List<ScheduleRealInfo> findByNbbm(String nbbm) {
return nbbmScheduleMap.get(nbbm);
}
/**
* @Title: findByLineAndUpDown
* @Description: TODO(lineCode 和走向获取班次)
*/
public List<ScheduleRealInfo> findByLineAndUpDown(String lineCode, Integer upDown) {
List<ScheduleRealInfo> list = findByLineCode(lineCode), rs = new ArrayList<>();
for (ScheduleRealInfo sch : list) {
if (sch.getXlDir().equals(upDown + ""))
rs.add(sch);
}
return rs;
}
public ScheduleRealInfo get(long id) {
return id2SchedulMap.get(id);
}
/**
* @Title: next
* @Description: TODO(下一个班次)
*/
public ScheduleRealInfo next(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
//排序
Collections.sort(list, schDFSJComparator);
return next(list, sch);
}
/**
* 下一个班次
*
* @param list 班次集合
* @param sch 当前班次
* @return
*/
private ScheduleRealInfo next(Collection<ScheduleRealInfo> list, ScheduleRealInfo sch) {
int outConfig = -1;
LineConfig config = lineConfigData.get(sch.getXlBm());
if (config != null)
outConfig = config.getOutConfig();
//限定出站既出场的停车场
List<String> parks = config.findTwinsParkList();
boolean limitPark = null != parks && parks.size() > 0;
boolean flag = false;
ScheduleRealInfo next = null;
for (ScheduleRealInfo temp : list) {
if (temp.getId() == sch.getId()) {
flag = true;
continue;
}
//忽略烂班
if (temp.isDestroy())
continue;
//出站既出场,忽略出场班次
if (outConfig == 2 && temp.getBcType().equals("out") && isEmptyMileage(temp)
&& (!limitPark || parks.contains(temp.getQdzCode())))
continue;
if (flag) {
next = temp;
break;
}
}
return next;
}
private boolean isEmptyMileage(ScheduleRealInfo sch) {
return sch.getBcsj() == 0 || sch.getJhlcOrig().intValue() == 0;
}
/**
* 下一个班次
*
* @param list 班次集合
* @param sch 当前班次
* @return
*/
private ScheduleRealInfo next2_lp(Collection<ScheduleRealInfo> list, ScheduleRealInfo sch) {
int outConfig = -1;
LineConfig config = lineConfigData.get(sch.getXlBm());
if (config != null)
outConfig = config.getOutConfig();
boolean flag = false;
ScheduleRealInfo next = null;
for (ScheduleRealInfo temp : list) {
if (temp.getId() == sch.getId()) {
flag = true;
continue;
}
if (flag) {
next = temp;
break;
}
}
return next;
}
private ScheduleRealInfo next3_lp(Collection<ScheduleRealInfo> list, ScheduleRealInfo sch) {
int outConfig = -1;
LineConfig config = lineConfigData.get(sch.getXlBm());
if (config != null)
outConfig = config.getOutConfig();
//限定出站既出场的停车场
List<String> parks = config.findTwinsParkList();
boolean limitPark = null != parks && parks.size() > 0;
boolean flag = false;
ScheduleRealInfo next = null;
for (ScheduleRealInfo temp : list) {
if (temp.getId() == sch.getId()) {
flag = true;
continue;
}
//出站既出场,忽略出场班次
if (outConfig == 2 && temp.getBcType().equals("out") && isEmptyMileage(temp)
&& (!limitPark || parks.contains(temp.getQdzCode())))
continue;
if (flag) {
next = temp;
break;
}
}
return next;
}
/**
* 上一个班次
*
* @param sch
* @return
*/
public ScheduleRealInfo prev(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
//boolean flag = false;
ScheduleRealInfo prev = null;
int size = list.size();
for (int i = 0; i < size; i++) {
if (list.get(i).isDestroy())
continue;
if (list.get(i).getId().equals(sch.getId())) {
return prev;
}
prev = list.get(i);
}
return prev;
}
/**
* 是否是首班出场
*
* @param sch
* @return
*/
public boolean isFirstOut(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
try {
if (list.get(0) == sch && sch.getBcType().equals("out"))
return true;
} catch (IndexOutOfBoundsException e) {
logger.error("小小的数组越界,无伤大雅!");
}
return false;
}
public void put(ScheduleRealInfo sch) {
schAttrCalculator
.calcRealDate(sch)
.calcAllTimeByFcsj(sch);
nbbmScheduleMap.put(sch.getClZbh(), sch);
//主键索引
id2SchedulMap.put(sch.getId(), sch);
//路牌对照表
addLPMapp(sch);
//跨24点的,再save一次
if (!sch.getRealExecDate().equals(sch.getScheduleDateStr()))
save(sch);
}
public void addLPMapp(ScheduleRealInfo sch) {
lpScheduleMap.put(sch.getXlBm() + "_" + sch.getLpName(), sch);
}
public void delete(ScheduleRealInfo sch) {
if (!sch.isSflj())
return;
nbbmScheduleMap.remove(sch.getClZbh(), sch);
id2SchedulMap.remove(sch.getId());
lpScheduleMap.remove(sch.getXlBm() + "_" + sch.getLpName(), sch);
//如果正在执行该班次
if (carExecutePlanMap.get(sch.getClZbh()) == sch) {
//重新计算车辆当前执行班次
reCalcExecPlan(sch.getClZbh());
}
}
public List<ScheduleRealInfo> updateQdzTimePlan(String lpName) {
List<ScheduleRealInfo> list = lpScheduleMap.get(lpName);
Collections.sort(list, schFCSJComparator);
return schAttrCalculator.updateQdzTimePlan(list);
}
public List<ScheduleRealInfo> updateQdzTimePlan(ScheduleRealInfo sch) {
return updateQdzTimePlan(sch.getXlBm() + "_" + sch.getLpName());
}
/**
* @Title: doneSum
* @Description: TODO(已完成班次总数)
*/
public int doneSum(String clZbh) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(clZbh);
int rs = 0;
for (ScheduleRealInfo sch : list) {
if (sch.getStatus() == 2 && !sch.isDestroy())
rs++;
}
return rs;
}
public void save(ScheduleRealInfo sch) {
sch.setUpdateDate(new Date());
pstBuffer.add(sch);
}
/**
* @Title: nextByBcType
* @Description: TODO(获取下一个指定班次类型的班次)
*/
public ScheduleRealInfo nextByBcType(String nbbm, String bcType) {
List<ScheduleRealInfo> list = findByBcType(nbbm, bcType);
Collections.sort(list, schFCSJComparator);
ScheduleRealInfo sch = null;
for (ScheduleRealInfo temp : list) {
if (temp.getFcsjActual() == null) {
sch = temp;
break;
}
}
return sch;
}
/**
* 搜索离当前时间最近的一个指定类型的班次
*
* @param nbbm
* @param bcType
* @return
*/
public ScheduleRealInfo searchNearByBcType(String nbbm, String bcType) {
List<ScheduleRealInfo> list = findByBcType(nbbm, bcType);
Collections.sort(list, schFCSJComparator);
long t = System.currentTimeMillis();
int distance = -1, diff;
ScheduleRealInfo sch = null;
for (ScheduleRealInfo temp : list) {
diff = (int) Math.abs(temp.getDfsjT() - t);
if (diff < distance || distance == -1) {
sch = temp;
distance = diff;
}
}
return sch;
}
public List<ScheduleRealInfo> findByBcType(String nbbm, String bcType) {
List<ScheduleRealInfo> all = nbbmScheduleMap.get(nbbm), outList = new ArrayList<>();
for (ScheduleRealInfo sch : all) {
if (sch.getBcType().equals(bcType))
outList.add(sch);
}
return outList;
}
public Collection<ScheduleRealInfo> findAll() {
return nbbmScheduleMap.values();
}
public Collection<ScheduleRealInfo> findAllByLpContainer() {
return lpScheduleMap.values();
}
public Collection<ScheduleRealInfo> findAllByIdContainer() {
return id2SchedulMap.values();
}
public int getPstSize() {
return pstBuffer.size();
}
public boolean addExecPlan(ScheduleRealInfo sch) {
ScheduleRealInfo oldExec = executeCurr(sch.getClZbh());
if (sch != null){
if(sch.getStatus()==2)
reCalcExecPlan(sch.getClZbh());
else
carExecutePlanMap.put(sch.getClZbh(), sch);
}
else
carExecutePlanMap.remove(sch.getClZbh());
return executeCurr(sch.getClZbh()) != oldExec;
}
public void removeExecPlan(String clzbh) {
carExecutePlanMap.remove(clzbh);
}
public Map<String, ScheduleRealInfo> execPlanMap() {
return carExecutePlanMap;
}
/**
* 车辆当前执行的班次
*
* @param nbbm
* @return
*/
public ScheduleRealInfo executeCurr(String nbbm) {
if(StringUtils.isEmpty(nbbm))
return null;
return carExecutePlanMap.get(nbbm);
}
/**
* @param @param sch
* @param @param newClZbh 新的车辆自编号
* @Title: changeCar
* @Description: TODO(班次换车) 返回有更新的班次
*/
public List<ScheduleRealInfo> changeCar(ScheduleRealInfo sch, String newClZbh) {
List<ScheduleRealInfo> ups = new ArrayList<>();
String oldClZbh = sch.getClZbh();
//变更相关映射信息
nbbmScheduleMap.remove(sch.getClZbh(), sch);
sch.setClZbh(newClZbh);
if (!nbbmScheduleMap.containsEntry(newClZbh, sch)) {
nbbmScheduleMap.put(newClZbh, sch);
}
//重新计算车辆当前执行班次
reCalcExecPlan(newClZbh);
reCalcExecPlan(oldClZbh);
//重新分组计划用车
reCalcLineNbbms();
return ups;
}
public void removeNbbm2SchMapp(ScheduleRealInfo sch) {
nbbmScheduleMap.remove(sch.getClZbh(), sch);
}
public void addNbbm2SchMapp(ScheduleRealInfo sch) {
nbbmScheduleMap.put(sch.getClZbh(), sch);
}
public void reCalcExecPlan(String nbbm) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(nbbm);
Collections.sort(list, schDFSJComparator);
ScheduleRealInfo sch = schAttrCalculator.calcCurrentExecSch(list);
if(null != sch)
carExecutePlanMap.put(nbbm, sch);
else
carExecutePlanMap.remove(nbbm);
}
/**
* 空驶任务?
* 出场、进场、直放、两点间空驶
* @param sch
* @return
*/
public static boolean emptyService(ScheduleRealInfo sch){
String type = sch.getBcType();
return type.equals("out") || type.equals("in") || type.equals("venting") || type.equals("ldks");
}
@Autowired
JdbcTemplate jdbcTemplate;
/**
* 删除实际排班
*
* @param lineCode
* @return
*/
public Map<String, Object> deleteRealSchedule(String lineCode) {
Map<String, Object> rs = new HashMap<>();
try {
String rq = currSchDateMap.get(lineCode);
if (StringUtils.isNotEmpty(rq)) {
List<ScheduleRealInfo> all = findByLineCode(lineCode);
if(null != all && all.size() > 0){
//解除gps 和班次之间的关联
List<ScheduleRealInfo> unions = calcUnion(all, carExecutePlanMap.values());
for (ScheduleRealInfo sch : unions) {
removeExecPlan(sch.getClZbh());
}
//解除调度指令和班次的外键约束
StringBuilder inStr = new StringBuilder("(");
for (ScheduleRealInfo sch : all) {
inStr.append(sch.getId() + ",");
}
inStr.deleteCharAt(inStr.length() - 1).append(")");
jdbcTemplate.update(Constants.MULTI_REMOVE_DIRECTIVE_SCH_FK + " " + inStr.toString());
}
//删除班次数据
removeRealSch(lineCode, rq);
}
rs.put("status", ResponseCode.SUCCESS);
} catch (Exception e) {
logger.error("", e);
rs.put("status", ResponseCode.ERROR);
if (e instanceof DataIntegrityViolationException)
rs.put("msg", "失败,违反数据约束!!");
else
rs.put("msg", e.getMessage());
}
return rs;
}
/**
* 计算并集
*
* @param c1
* @param c2
* @return
*/
public List<ScheduleRealInfo> calcUnion(Collection<ScheduleRealInfo> c1, Collection<ScheduleRealInfo> c2) {
List<ScheduleRealInfo> rs = new ArrayList<>();
for (ScheduleRealInfo sch : c1) {
if (c2.contains(sch)) {
rs.add(sch);
}
}
return rs;
}
/**
* 覆盖一辆车的所有班次
*
* @param nbbm
* @param sets
*/
public void replaceByNbbm(String nbbm, Collection<ScheduleRealInfo> sets) {
nbbmScheduleMap.removeAll(nbbm);
nbbmScheduleMap.putAll(nbbm, sets);
}
/**
* 获取该班次所在路牌的下一个班次
*
* @param sch
* @return
*/
public ScheduleRealInfo nextByLp(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = lpScheduleMap.get(sch.getXlBm() + "_" + sch.getLpName());
Collections.sort(list, schFCSJComparator);
return next3_lp(list, sch);
}
/**
* 获取该班次所在路牌的下一个班次,不考虑场既是站
*
* @param sch
* @return
*/
public ScheduleRealInfo nextByLp2(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = lpScheduleMap.get(sch.getXlBm() + "_" + sch.getLpName());
Collections.sort(list, schFCSJComparator);
return next2_lp(list, sch);
}
public ArrayListMultimap<String, ScheduleRealInfo> getLpScheduleMap() {
return lpScheduleMap;
}
/**
* 重新全量计算路牌下的班次关联性
* 临时性的函数
*/
public void _test_reCalcLpSch() {
Map<String ,Collection<ScheduleRealInfo>> map = lpScheduleMap.asMap();
Set<String> ks = map.keySet();
for(String k : ks){
schAttrCalculator.calcQdzTimePlan(new ArrayList<ScheduleRealInfo>(map.get(k)));
}
}
public int dbCount(String lineCode, String currSchDate) {
int count = -1;
try{
count = jdbcTemplate.queryForObject("select count(*) from bsth_c_s_sp_info_real where schedule_date='"+currSchDate+"' and xl_bm='"+lineCode+"'", java.lang.Integer.class);
}catch (Exception e){
logger.error("", e);
}
return count;
}
/**
* 重新计算ID对照map
*/
public int reCalcIdMaps(){
Collection<ScheduleRealInfo> all = findAll();
ConcurrentMap<Long, ScheduleRealInfo> id2SchedulMapCopy = new ConcurrentHashMap<>();
for(ScheduleRealInfo sch : all){
id2SchedulMapCopy.put(sch.getId(), sch);
}
id2SchedulMap = id2SchedulMapCopy;
return id2SchedulMap.size();
}
/**
* 重新计算线路计划用车
*/
public void reCalcLineNbbms(){
HashMultimap<String, String> multimap = HashMultimap.create();
Collection<ScheduleRealInfo> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
multimap.put(sch.getXlBm(), sch.getClZbh());
}
lineNbbmsMap = multimap;
}
public String sizeString(){
return id2SchedulMap.size() + "/" + nbbmScheduleMap.size();
}
/**
* 按公司编码分组数据
*/
public void groupByGsbm(){
Collection<ScheduleRealInfo> all = findAll();
ListMultimap<String, ScheduleRealInfo> gsBmMaps = ArrayListMultimap.create();
for(ScheduleRealInfo sch : all){
gsBmMaps.put(sch.getGsBm(), sch);
}
if(gsBmMaps.size() > 0){
gsBmScheduleMap = null;
gsBmScheduleMap = gsBmMaps;
}
}
public Collection<ScheduleRealInfo> findByGsbm(String gsbm){
return gsBmScheduleMap.get(gsbm);
}
/**
* 删除班次,删除内存中未清理掉的非当天的班次
* @createDate 2019.05.13
* @author zhangxianzhou
* @param sch
*/
public void deleteBC(ScheduleRealInfo sch) {
nbbmScheduleMap.remove(sch.getClZbh(), sch);
id2SchedulMap.remove(sch.getId());
lpScheduleMap.remove(sch.getXlBm() + "_" + sch.getLpName(), sch);
//如果正在执行该班次
if (carExecutePlanMap.get(sch.getClZbh()) == sch) {
//重新计算车辆当前执行班次
reCalcExecPlan(sch.getClZbh());
}
}
/**
** 用于重置维修上报计数,一般只应该在翻班的时候调用
*/
public void resetRepairReport(String incode) {
incode2report.remove(incode);
}
public RepairReport getLastestRepairReport(String incode) {
return incode2report.get(incode);
}
public void setLastestRepairReport(RepairReport rr) {
incode2report.put(rr.getIncode(), rr);
}
>>>>>>> 6cafe1acab4252c1fd171e0669654f317bf0fd4a
}