DayOfSchedule.java
18.7 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
package com.bsth.data.schedule;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.Application;
import com.bsth.data.LineConfigData;
import com.bsth.data.directive.FirstScheduleCheckThread;
import com.bsth.data.gpsdata.GpsRealData;
import com.bsth.data.schedule.thread.ScheduleLateThread;
import com.bsth.data.schedule.thread.SchedulePstThread;
import com.bsth.data.schedule.thread.ScheduleRefreshThread;
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.BatchSaveUtils;
import com.bsth.util.DateUtils;
import com.bsth.websocket.handler.SendUtils;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.TreeMultimap;
/**
*
* @ClassName: DayOfSchedule
* @Description: TODO(当日实际排班)
* @author PanZhao
* @date 2016年8月15日 上午10:16:12
*
*/
@Component
public class DayOfSchedule implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(this.getClass());
// 按车辆分组的班次数据
private static ArrayListMultimap<String, ScheduleRealInfo> nbbmScheduleMap;
// 班次主键映射
private static Map<Long, ScheduleRealInfo> id2SchedulMap;
// 车辆和排班起终点站对照(包括进出的停车场,区间起终点)
private static TreeMultimap<String, String> nbbm2SEStationMap;
//车辆 ——> 当前执行班次
private static Map<String, ScheduleRealInfo> carExecutePlanMap;
// 持久化缓冲区
public static LinkedList<ScheduleRealInfo> pstBuffer;
// 排序器
private static ScheduleComparator.FCSJ schFCSJComparator;
@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();
id2SchedulMap = new HashMap<>();
pstBuffer = new LinkedList<>();
schFCSJComparator = new ScheduleComparator.FCSJ();
currSchDateMap = new HashMap<>();
nbbm2SEStationMap = TreeMultimap.create();
carExecutePlanMap = new HashMap<>();
}
@Autowired
ScheduleRefreshThread scheduleRefreshThread;
@Autowired
SchedulePstThread schedulePstThread;
@Autowired
FirstScheduleCheckThread firstScheduleCheckThread;
@Autowired
ScheduleLateThread scheduleLateThread;
@Override
public void run(String... arg0) throws Exception {
//翻班线程
Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, 20, 240, TimeUnit.SECONDS);
//入库
//Application.mainServices.scheduleWithFixedDelay(schedulePstThread, 60, 60, TimeUnit.SECONDS);
//首班出场指令补发器
//Application.mainServices.scheduleWithFixedDelay(firstScheduleCheckThread, 10, 120, TimeUnit.SECONDS);
//班次误点扫描
//Application.mainServices.scheduleWithFixedDelay(scheduleLateThread, 60, 60, TimeUnit.SECONDS);
}
public Map<String, String> getCurrSchDate() {
return currSchDateMap;
}
/**
*
* @Title: calcSchDateB
* @Description: TODO(计算线路当前应该使用的排班日期)
*/
public String calcSchDate(String lineCode) {
LineConfig conf = lineConfigData.get(lineCode);
long ct = System.currentTimeMillis();
SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
String schDate = sdfyyyyMMdd.format(new Date(ct));
// 小于当天起始运营时间,则取前一天的排班
if (ct < conf.getCurrStartTime())
schDate = DateUtils.subtractDay(schDate, 1);
return schDate;
}
/**
* @Title: reloadSch
* @Title: reloadSch
* @Description: TODO(重新载入排班)
* @param @param
* lineCode 线路编码
* @param @param
* schDate 班次日期 yyyy-MM-dd
* @param @param
* forcePlan 强制从计划调度重新抓取
*/
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);
Set<String> cars = searchAllCars(list);
//计算“起点站应到”时间
for(String nbbm : cars)
schAttrCalculator.calcQdzTimePlan(nbbmScheduleMap.get(nbbm));
//是否是出站即出场
LineConfig conf = lineConfigData.get(lineCode);
if(conf.getOutConfig() == 2){
for(String nbbm : cars)
schAttrCalculator.connectOutSchedule(nbbmScheduleMap.get(nbbm));
}
// 页面 翻班通知
sendUtils.shiftSchedule(lineCode);
} catch (Exception e) {
logger.error("", e);
return -1;
}
return 0;
}
/**
*
* @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;
}
private void putAll(List<ScheduleRealInfo> list) {
for (ScheduleRealInfo sch : list)
put(sch);
}
/**
* @Title: removeRealSch
* @Description: TODO(清除实际排班,包括数据库和内存数据)
* @param @param
* lineCode 线路编码
* @param @param
* schDate 班次日期 yyyy-MM-dd
*/
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> schs = nbbmScheduleMap.values();
for (ScheduleRealInfo sch : schs) {
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 ++;
}
}
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<>();
SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd")
,sdfHHmm = new SimpleDateFormat("HH:mm");
data.put("scheduleDate_eq", sdfyyyyMMdd.parse(schDate));
data.put("xlBm_eq", lineCode);
// 查询计划排班
List<SchedulePlanInfo> planItr = cleanSchPlanItr(schPlanService.list(data).iterator());
// 转换为实际排班
realList = JSONArray.parseArray(JSON.toJSONString(planItr), ScheduleRealInfo.class);
for (ScheduleRealInfo sch : realList) {
sch.setScheduleDateStr(sdfyyyyMMdd.format(sch.getScheduleDate()));
sch.setRealExecDate(sch.getScheduleDateStr());
// 计划终点时间
if (sch.getBcsj() != null) {
try{
sch.setZdsjT(sdfHHmm.parse(sch.getFcsj()).getTime() + (sch.getBcsj() * 60 * 1000));
sch.setZdsj(sdfHHmm.format(sch.getZdsjT()));
sch.setLate(false);
}catch(ParseException pe){
logger.error("loadPlanSch... 计算终点时间失败...");
}
}
//计划里程为0,直接清空
if(sch.getJhlc() != null && sch.getJhlc() == 0)
sch.setJhlc(null);
}
} catch (Exception e) {
logger.error("", e);
}
return realList;
}
/**
* @Title: batchSave
* @Description: TODO(批量入库)
*/
private void batchSave(List<ScheduleRealInfo> list) {
// 查询数据库最大ID
Long id = schRepository.getMaxId();
if (null == id)
id = 0L;
id++;
SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
for (ScheduleRealInfo item : list) {
item.setSpId(item.getId());// 保留原始的计划ID
item.setId(id++);// 设置ID
item.setScheduleDateStr(sdfyyyyMMdd.format(item.getScheduleDate()));
}
// 入库
new BatchSaveUtils<ScheduleRealInfo>().saveList(list, ScheduleRealInfo.class);
}
private List<SchedulePlanInfo> cleanSchPlanItr(Iterator<SchedulePlanInfo> itrab) {
List<SchedulePlanInfo> list = new ArrayList<>();
SchedulePlanInfo sp;
while (itrab.hasNext()) {
sp = itrab.next();
sp.setSchedulePlan(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: 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 rs;
}
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);
}
public Set<String> getSEStationList(String nbbm) {
return nbbm2SEStationMap.get(nbbm);
}
/**
*
* @Title: next
* @Description: TODO(下一个班次)
*/
public ScheduleRealInfo next(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
boolean flag = false;
ScheduleRealInfo next = null;
for(ScheduleRealInfo temp : list){
if(temp.getId() == sch.getId()){
flag = true;
continue;
}
//忽略烂班
if(temp.isDestroy())
continue;
if(flag){
next = temp;
break;
}
}
return next;
}
public void put(ScheduleRealInfo sch) {
schAttrCalculator
.calcRealDate(sch)
.calcAllTimeByFcsj(sch);
String nbbm = sch.getClZbh();
nbbmScheduleMap.put(nbbm, sch);
nbbm2SEStationMap.put(nbbm, sch.getQdzCode());
nbbm2SEStationMap.put(nbbm, sch.getZdzCode());
//主键索引
id2SchedulMap.put(sch.getId(), sch);
}
public void delete(ScheduleRealInfo sch) {
//ScheduleRealInfo sch = id2SchedulMap.get(id);
if(!sch.isSflj())
return;
nbbmScheduleMap.remove(sch.getClZbh(), sch);
id2SchedulMap.remove(sch.getId());
//return sch;
}
public void calcQdzTimePlan(String nbbm){
schAttrCalculator.calcQdzTimePlan(nbbmScheduleMap.get(nbbm));
}
public List<ScheduleRealInfo> updateQdzTimePlan(String nbbm){
return schAttrCalculator.updateQdzTimePlan(nbbmScheduleMap.get(nbbm));
}
/**
*
* @Title: nextAll
* @Description: TODO(之后的所有班次)
*/
public List<ScheduleRealInfo> nextAll(ScheduleRealInfo sch) {
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
// 排序
Collections.sort(list, schFCSJComparator);
List<ScheduleRealInfo> rs = new ArrayList<>();
ScheduleRealInfo temp;
for (int i = 0; i < list.size() - 1; i++) {
temp = list.get(i);
if(temp.getFcsjT() > sch.getFcsjT())
rs.add(temp);
}
return rs;
}
/**
*
* @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;
}
/**
*
* @Title: prveNotExecNum
* @Description: TODO(班次之前未执行班次数量)
*/
public int prveNotExecNum(ScheduleRealInfo sch){
int n = 0;
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
for(ScheduleRealInfo s : list){
if(s.getFcsjActual() == null && !s.isDestroy())
n ++;
if(s.getId().equals(sch.getId()))
break;
}
return n;
}
/**
*
* @Title: validEndTime
* @Description: TODO(是否是有效的到达时间)
*/
public boolean validEndTime(ScheduleRealInfo sch, Long ts) {
if(sch.getFcsjActualTime() != null && sch.getFcsjActualTime() > ts)
return false;
return validTime(sch, ts);
}
/**
*
* @Title: validStartTime
* @Description: TODO(是否是合适的发车时间)
*/
public boolean validStartTime(ScheduleRealInfo sch, Long ts) {
if(sch.getZdsjActualTime() != null && sch.getZdsjActualTime() < ts)
return false;
return validTime(sch, ts);
}
public boolean validTime(ScheduleRealInfo sch, Long ts){
List<ScheduleRealInfo> list = nbbmScheduleMap.get(sch.getClZbh());
int ci = list.indexOf(sch);
ScheduleRealInfo prve, next;
if(ci > 0){
//之前班次实际时间不能大于该时间
for(int i = ci - 1; i >= 0; i --){
prve = list.get(i);
if(prve.getZdsjActualTime() != null && prve.getZdsjActualTime() > ts )
return false;
if(prve.getFcsjActualTime() != null && prve.getFcsjActualTime() > ts)
return false;
}
}
if(ci < list.size() - 1){
//之后班次实际时间不能小于该时间
for(int i = ci + 1; i < list.size(); i ++){
next = list.get(i);
if(next.getFcsjActualTime() != null && next.getFcsjActualTime() < ts)
return false;
if(next.getZdsjActualTime() != null && next.getZdsjActualTime() < ts)
return false;
}
}
return true;
}
public void save(ScheduleRealInfo sch){
//schRepository.save(sch);
//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;
}
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 Set<String> allCar(){
return nbbmScheduleMap.keySet();
}
public Collection<ScheduleRealInfo> findAll(){
return nbbmScheduleMap.values();
}
public void addExecPlan(ScheduleRealInfo sch){
carExecutePlanMap.put(sch.getClZbh(), sch);
}
public void removeExecPlan(String clzbh){
carExecutePlanMap.remove(clzbh);
}
public Map<String, ScheduleRealInfo> execPlamMap(){
return carExecutePlanMap;
}
/**
* @Title: changeCar
* @Description: TODO(班次换车) 返回有更新的班次
* @param @param sch
* @param @param newClZbh 新的车辆自编号
*/
public List<ScheduleRealInfo> changeCar(ScheduleRealInfo sch , String newClZbh){
List<ScheduleRealInfo> ups = new ArrayList<>();
String oldClzbh = sch.getClZbh();
if(oldClzbh.equals(newClZbh))
return ups;
//变更相关映射信息
nbbmScheduleMap.remove(sch.getClZbh(), sch);
sch.setClZbh(newClZbh);
nbbmScheduleMap.put(newClZbh, sch);
nbbm2SEStationMap.put(newClZbh, sch.getQdzCode());
nbbm2SEStationMap.put(newClZbh, sch.getZdzCode());
//重新计算班次应到时间
ups.addAll(updateQdzTimePlan(oldClzbh));
ups.addAll(updateQdzTimePlan(newClZbh));
return ups;
}
/**
*
* @Title: linkToSchPlan
* @Description: TODO(车辆关联到班次)
*/
/* public void linkToSchPlan(String nbbm) {
//当前GPS状态
GpsEntity gps = gpsRealData.get(BasicData.deviceId2NbbmMap.inverse().get(nbbm));
if(null == gps)
return;
//班次集合
List<ScheduleRealInfo> schArr = nbbmScheduleMap.get(nbbm);
for(ScheduleRealInfo sch : schArr){
if(sch.getStatus() == 2)
continue;
if(sch.isDestroy())
continue;
if(!sch.getXlBm().equals(gps.getLineId())
|| Integer.parseInt(sch.getXlDir()) != gps.getUpDown().intValue())
continue;
addExecPlan(sch);
break;
}
}*/
}