GetSchedulePlanThread.java
3.61 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
package com.bsth.service.realcontrol.buffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.repository.schedule.SchedulePlanInfoRepository;
import com.bsth.util.BatchSaveUtils;
import com.bsth.vehicle.directive.buffer.DirectiveBuffer;
import com.bsth.vehicle.gpsdata.buffer.ArrivalDataBuffer;
import com.bsth.websocket.handler.RealControlSocketHandler;
/**
*
* @ClassName: GetSchedulePlanThread
* @Description: TODO(从计划排班表抓取当天实际排班)
* @author PanZhao
* @date 2016年6月6日 下午7:42:43
*
*/
@Component
public class GetSchedulePlanThread extends Thread{
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
SchedulePlanInfoRepository schedulePlanInfoRepository;
@Autowired
ScheduleRealInfoRepository scheduleRealInfoRepository;
SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
@Autowired
DirectiveBuffer directiveBuffer;
@Autowired
RealControlSocketHandler realControlSocket;
@Override
public void run() {
try{
logger.info("从计划调度抓取排班数据...");
//清除缓存
ScheduleBuffer.clear();
ArrivalDataBuffer.clear();
//所有指令入库
directiveBuffer.saveAll();
DirectiveBuffer.clear();
//加载新的排班
loaSchedule();
//通知所有线调页面刷新
Map<String, Object> data = new HashMap<>();
data.put("fn", "refresh");
data.put("dateStr", sdfyyyyMMdd.format(new Date()));
realControlSocket.sendMessageToUsers(new TextMessage(JSON.toJSONString(data)));
}catch(Exception e){
logger.error("",e);
}
}
public void loaSchedule() throws ParseException{
List<ScheduleRealInfo> realList = null;
String dateStr = sdfyyyyMMdd.format(new Date())/*"2016-07-28"*/;
//Date cDate = sdfyyyyMMdd.parse(dateStr);
//查询数据库是否有今日排班
int size = scheduleRealInfoRepository.countByDate(dateStr);
if(size > 0){
//从数据库恢复当日排班
realList = scheduleRealInfoRepository.findByDate(dateStr);
logger.info("从数据库恢复当日排班 " + realList.size());
//写入缓存
ScheduleBuffer.init(realList);
}
else{
List<SchedulePlanInfo> list = schedulePlanInfoRepository.findByDate(sdfyyyyMMdd.parse(dateStr));
for(SchedulePlanInfo sp : list){
sp.setSchedulePlan(null);
}
//实际排班计划
realList = JSONArray.parseArray(JSON.toJSONString(list), ScheduleRealInfo.class);
//查询数据库最大ID
Long id = scheduleRealInfoRepository.getMaxId();
if(null == id)
id = 0L;
id ++;
for(ScheduleRealInfo item : realList){
item.setSpId(item.getId());
item.setId(id ++);//设置ID
item.setScheduleDateStr(sdfyyyyMMdd.format(item.getScheduleDate()));
}
//写入缓存
ScheduleBuffer.init(realList);
//入库
new BatchSaveUtils<ScheduleRealInfo>().saveList(realList, ScheduleRealInfo.class);
}
logger.info("获取当天实际排班计划数量:" + realList.size());
}
}