ScheduleBuffer.java
5.36 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
package com.bsth.service.realcontrol.buffer;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.LinkedListMultimap;
/**
*
* @ClassName: ScheduleRealInfoBuffer
* @Description: TODO(当天实际排班缓存)
* @author PanZhao
* @date 2016年6月14日 上午9:40:38
*
*/
public class ScheduleBuffer {
static Logger logger = LoggerFactory.getLogger(ScheduleBuffer.class);
/**
* 当天实际排班
* K 线路编码 V 按时间升序的班次
*/
public static ArrayListMultimap<String, ScheduleRealInfo> schedulListMap;
/**
* K: 车辆自编号 V: 未完成班次链表
*/
public static Map<String, LinkedList<ScheduleRealInfo>> vehLinkedMap;
/**
* K: 车辆自编号 V: 已执行班次链表
*/
public static LinkedListMultimap<String, ScheduleRealInfo> finishLinkedMap;
/**
* 主键 和 排班映射
*/
public static Map<Long, ScheduleRealInfo> pkSchedulMap;
/**
* 需要持久化的排班
*/
public static LinkedList<ScheduleRealInfo> persistentList;
/**
* 线路是否托管
* K:线路编码 V:0 托管 1 非托管
*/
public static Map<Integer, Integer> trustMap;
static ScheduleComparator scheduleComparator = new ScheduleComparator();
static{
schedulListMap = ArrayListMultimap.create();
pkSchedulMap = new HashMap<>();
persistentList = new LinkedList<>();
vehLinkedMap = new HashMap<>();
finishLinkedMap = LinkedListMultimap.create();
trustMap = new HashMap<>();
}
public static int init(List<ScheduleRealInfo> list){
try{
//计算时间戳
for(ScheduleRealInfo schedul : list){
schedul.syncTime();
}
//发车时间排序
Collections.sort(list, scheduleComparator);
String zbh;
for(ScheduleRealInfo schedul : list){
schedulListMap.put(schedul.getXlBm(), schedul);
pkSchedulMap.put(schedul.getId(), schedul);
//初始化车辆和班次列表对照
zbh = schedul.getClZbh();
if(!vehLinkedMap.containsKey(zbh))
vehLinkedMap.put(zbh, new LinkedList<ScheduleRealInfo>());
vehLinkedMap.get(zbh).add(schedul);
}
//计算起点应到时间
Set<String> codes = schedulListMap.keySet();
for(String code : codes)
calcArrDateQd(code);
}catch(Exception e){
logger.error("缓存排班数据失败...", e);
return -1;
}
return 0;
}
public static int put(ScheduleRealInfo sch){
schedulListMap.put(sch.getXlBm(), sch);
pkSchedulMap.put(sch.getId(), sch);
String zbh = sch.getClZbh();
if(!vehLinkedMap.containsKey(zbh))
vehLinkedMap.put(zbh, new LinkedList<ScheduleRealInfo>());
vehLinkedMap.get(zbh).add(sch);
//重新排序
Collections.sort(vehLinkedMap.get(zbh), scheduleComparator);
return 0;
}
public static class ScheduleComparator implements Comparator<ScheduleRealInfo>{
@Override
public int compare(ScheduleRealInfo o1, ScheduleRealInfo o2) {
return (int) (o1.getFcsjT() - o2.getFcsjT());
}
}
/**
*
* @Title: finishSch
* @Description: TODO(完成一个班次)
* @param @param sch
* @return ScheduleRealInfo 返回 下一个未执行的班次
* @throws
*/
public static ScheduleRealInfo finishSch(ScheduleRealInfo sch){
LinkedList<ScheduleRealInfo> list = vehLinkedMap.get(sch.getClZbh());
//状态修改为已执行
sch.setStatus(2);
persistentList.add(sch);
ScheduleRealInfo temp;
int len = list.size();
for(int i = 0; i < len; i ++){
temp = list.poll();
if(temp.equals(sch)){
//加入已执行
finishLinkedMap.put(sch.getClZbh(), sch);
break;
}
}
ScheduleRealInfo next = list.getFirst();
next.setQdzArrDateSJ(sch.getZdsjActual());
return next;
}
/**
*
* @Title: getFinishSchNo
* @Description: TODO(获取车辆已完成的班次数)
* @param @param nbbm
* @throws
*/
public static int getFinishSchNo(String nbbm){
return finishLinkedMap.get(nbbm) == null ? 0 : finishLinkedMap.get(nbbm).size();
}
/**
*
* @Title: calcArrDateQd
* @Description: TODO(计算计划起点时间)
* @throws
*/
public static void calcArrDateQd(String lineCode){
List<ScheduleRealInfo> list = schedulListMap.get(lineCode);
//按车辆分组
ArrayListMultimap<String, ScheduleRealInfo> map = ArrayListMultimap.create();
for(ScheduleRealInfo sch : list){
map.put(sch.getClZbh(), sch);
}
//链接班次
Set<String> set = map.keySet();
List<ScheduleRealInfo> subList;
int len;
ScheduleRealInfo prve, curr;
for(String k : set){
subList = map.get(k);
//排序
Collections.sort(subList, scheduleComparator);
len = subList.size();
if(len == 0)
continue;
prve = subList.get(0);
for(int i = 1; i < len; i ++){
curr = subList.get(i);
if(prve.getZdzName().equals(curr.getQdzName()))
curr.setQdzArrDateJH(prve.getZdsj());
prve = curr;
}
}
}
public static ScheduleRealInfo findOne(Long id){
return pkSchedulMap.get(id);
}
}