Commit a5d3dbb5cc00c9139f9a56e354e651c1e950a5da

Authored by 王通
1 parent 21ac55bd

1./external 路单信息开放

src/main/java/com/bsth/redis/ScheduleRedisService.java
1 -package com.bsth.redis;  
2 -  
3 -import com.bsth.Application;  
4 -import com.bsth.common.BasicData;  
5 -import com.bsth.entity.ScheduleRealInfo;  
6 -import com.bsth.redis.util.RedisUtils;  
7 -import com.bsth.repository.ScheduleRealInfoRepository;  
8 -import com.bsth.server_rs.base_info.line.Line;  
9 -import com.bsth.server_rs.base_info.line.buffer.LineBufferData;  
10 -import com.bsth.util.ConfigUtil;  
11 -import com.bsth.util.ConvertUtil;  
12 -import com.google.common.collect.ArrayListMultimap;  
13 -import org.apache.commons.lang3.StringUtils;  
14 -import org.joda.time.DateTime;  
15 -import org.joda.time.format.DateTimeFormat;  
16 -import org.joda.time.format.DateTimeFormatter;  
17 -import org.slf4j.Logger;  
18 -import org.slf4j.LoggerFactory;  
19 -import org.springframework.beans.factory.annotation.Autowired;  
20 -import org.springframework.boot.CommandLineRunner;  
21 -import org.springframework.core.annotation.Order;  
22 -import org.springframework.data.redis.core.ListOperations;  
23 -import org.springframework.data.redis.core.RedisTemplate;  
24 -import org.springframework.data.redis.serializer.StringRedisSerializer;  
25 -import org.springframework.stereotype.Component;  
26 -import org.springframework.stereotype.Service;  
27 -  
28 -import java.util.ArrayList;  
29 -import java.util.Iterator;  
30 -import java.util.List;  
31 -import java.util.concurrent.TimeUnit;  
32 -  
33 -/**  
34 - * 班次 redis 缓存管理  
35 - * Created by panzhao on 2017/3/13.  
36 - */  
37 -@Service  
38 -@Order(2)  
39 -public class ScheduleRedisService implements CommandLineRunner {  
40 -  
41 - @Autowired  
42 - ScheduleRealInfoRepository scheduleRealInfoRepository;  
43 -  
44 - private final static String REDIS_KEY_PREFIX = "schedule:";  
45 -  
46 - @Autowired  
47 - private RedisTemplate redisTemplate;  
48 -  
49 - @Autowired  
50 - RedisUtils redisUtils;  
51 -  
52 - static Logger logger = LoggerFactory.getLogger(ScheduleRedisService.class);  
53 -  
54 - private final static long DAY_TIME = 1000 * 60 * 60 * 24L;  
55 -  
56 - /**  
57 - * 将一批班次写入redis  
58 - *  
59 - * @param list  
60 - */  
61 - public void wirte(List<ScheduleRealInfo> list) {  
62 - ArrayListMultimap<String, ScheduleRealInfo> multimap;  
63 - try {  
64 - if (list.size() == 0)  
65 - return;  
66 - //按日期和线路分组数据  
67 - Class clazz = ScheduleRealInfo.class;  
68 - multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDateStr"));  
69 -  
70 - //写入redis  
71 - Iterator<String> iterator = multimap.keySet().iterator();  
72 - String key;  
73 - while (iterator.hasNext()) {  
74 - key = iterator.next();  
75 - replace(key, multimap.get(key));  
76 - }  
77 - } catch (Exception e) {  
78 - logger.error("", e);  
79 - }  
80 - }  
81 -  
82 - /**  
83 - * 将 list 与redis里的数据合并  
84 - *  
85 - * @param key  
86 - * @param list  
87 - */  
88 - public void mergeData(String key, List<ScheduleRealInfo> list) {  
89 - key = REDIS_KEY_PREFIX + key.replaceAll("-", "");  
90 -  
91 - ListOperations<String, ScheduleRealInfo> ops = redisTemplate.opsForList();  
92 - List<ScheduleRealInfo> cacheList = ops.range(key, 0, -1);  
93 -  
94 - for (ScheduleRealInfo sch : cacheList) {  
95 - if (!list.contains(sch))  
96 - list.add(sch);  
97 - }  
98 -  
99 - //更新  
100 - redisTemplate.execute(redisUtils.getUpdateCallback(key, list));  
101 - }  
102 -  
103 - /**  
104 - * 覆盖数据  
105 - *  
106 - * @param key  
107 - * @param list  
108 - */  
109 - public void replace(String key, List<ScheduleRealInfo> list) {  
110 - key = REDIS_KEY_PREFIX + key.replaceAll("-", "");  
111 - redisTemplate.execute(redisUtils.getUpdateCallback(key, list));  
112 - }  
113 -  
114 - /**  
115 - * 删除数据  
116 - *  
117 - * @param key  
118 - */  
119 - public void delete(String lineCode, String rq) {  
120 - String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", "");  
121 - redisTemplate.delete(key);  
122 - }  
123 -  
124 - /**  
125 - * 根据日期和线路编码从redis获取班次  
126 - *  
127 - * @param dateStr  
128 - * @param lineCode  
129 - * @return  
130 - */  
131 - public List<ScheduleRealInfo> read(String dateStr, String lineCode) {  
132 - return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1);  
133 - }  
134 -  
135 - /**  
136 - * 返回指定日期,公司的实际排班,并按线路_车辆分组  
137 - *  
138 - * @param rq  
139 - * @param companyId  
140 - * @return  
141 - */  
142 - public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm(String rq, String companyId) {  
143 - List<String> lineArray = LineBufferData.findCodesByCompany(companyId);  
144 - ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();  
145 -  
146 - rq = rq.replaceAll("-", "");  
147 - List<ScheduleRealInfo> list;  
148 - for (String lineCode : lineArray) {  
149 -  
150 - list = read(rq, lineCode);  
151 -  
152 - for (ScheduleRealInfo sch : list) {  
153 - rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch);  
154 - }  
155 - }  
156 - return rs;  
157 - }  
158 -  
159 - /**  
160 - * 返回指定日期,公司的实际排班,并按线路分组  
161 - *  
162 - * @param rq  
163 - * @param companyId  
164 - * @return  
165 - */  
166 - public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByLine(String rq, String companyId) {  
167 -  
168 - List<String> lineArray = LineBufferData.findCodesByCompany(companyId);  
169 - ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();  
170 -  
171 - rq = rq.replaceAll("-", "");  
172 - List<ScheduleRealInfo> list;  
173 - for (String lineCode : lineArray) {  
174 -  
175 - list = read(rq, lineCode);  
176 -  
177 - for (ScheduleRealInfo sch : list) {  
178 - rs.put(sch.getXlBm(), sch);  
179 - }  
180 - }  
181 - return rs;  
182 - }  
183 -  
184 - @Autowired  
185 - ScheduleRefreshThread scheduleRefreshThread;  
186 - @Autowired  
187 - ScheduleClearThread scheduleClearThread;  
188 -  
189 - @Override  
190 - public void run(String... strings) throws Exception {  
191 - //用子线程去加载,,不要阻塞  
192 - Application.mainServices.schedule(new Runnable() {  
193 - @Override  
194 - public void run() {  
195 - try {  
196 - logger.info("redis 实际排班 start...");  
197 - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));  
198 - //设置key 序列化器  
199 - redisTemplate.setKeySerializer(new StringRedisSerializer());  
200 -  
201 - DateTime dt = new DateTime();  
202 - dt = dt.minusDays(cacheDays);  
203 - List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd"));  
204 - calcTime(list);  
205 - //写入redis  
206 - wirte(list);  
207 - logger.info("redis 实际排班 over...");  
208 - } catch (Exception e) {  
209 - logger.info("redis 实际排班 异常", e);  
210 - }  
211 - }  
212 - }, 5, TimeUnit.SECONDS);  
213 -  
214 - //定时刷新一次当日实际排班  
215 - int minute = 10;  
216 - Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute + 4, minute, TimeUnit.MINUTES);  
217 - //24小时清理一次实际排班数据  
218 - Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS);  
219 - }  
220 -  
221 - public List<ScheduleRealInfo> findByMultiLine(List<String> lineArray, String rq) {  
222 - rq = rq.replaceAll("-", "");  
223 - List<ScheduleRealInfo> rs = new ArrayList<>();  
224 - for (String lineCode : lineArray) {  
225 - rs.addAll(read(rq, lineCode));  
226 - }  
227 - return rs;  
228 - }  
229 -  
230 - @Component  
231 - public static class ScheduleRefreshThread extends Thread {  
232 -  
233 - @Autowired  
234 - ScheduleRealInfoRepository realInfoRepository;  
235 -  
236 - @Autowired  
237 - ScheduleRedisService scheduleRedisService;  
238 -  
239 - @Override  
240 - public void run() {  
241 - try {  
242 - DateTime dt = new DateTime();  
243 - DateTime yesterday = dt.minusDays(2);  
244 -  
245 - String rq = yesterday.toString("yyyy-MM-dd");  
246 - logger.info("refresh lt yesterday ..." + rq + " -start");  
247 - List<ScheduleRealInfo> list = realInfoRepository.findByDateLT(rq);  
248 -  
249 - //计算时间戳  
250 - scheduleRedisService.calcTime(list);  
251 - scheduleRedisService.wirte(list);  
252 -  
253 - logger.info("refresh lt yesterday ..." + rq + " -end### size: " + list.size());  
254 - } catch (Exception e) {  
255 - logger.error("", e);  
256 - }  
257 - }  
258 - }  
259 -  
260 - @Component  
261 - public static class ScheduleClearThread extends Thread {  
262 -  
263 - @Autowired  
264 - ScheduleRedisService scheduleRedisService;  
265 -  
266 - @Override  
267 - public void run() {  
268 - try {  
269 - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));  
270 - DateTime dt = new DateTime();  
271 - dt = dt.minusDays(cacheDays);  
272 - String rq = dt.toString("yyyy-MM-dd");  
273 -  
274 - List<Line> lines = LineBufferData.findAll();  
275 - for (Line line : lines) {  
276 - scheduleRedisService.delete(line.getLineCode(), rq);  
277 - }  
278 - } catch (Exception e) {  
279 - logger.error("", e);  
280 - }  
281 - }  
282 - }  
283 -  
284 - /**  
285 - * ############ 时间戳计算 ##########  
286 - */  
287 - private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"),  
288 - fmtHHmm = DateTimeFormat.forPattern("HH:mm");  
289 -  
290 - private void calcTime(List<ScheduleRealInfo> list) {  
291 - if (list.size() == 0)  
292 - return;  
293 -  
294 - //计算真实执行日期 和 时间戳  
295 - for (ScheduleRealInfo sch : list) {  
296 - calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch);  
297 - }  
298 - }  
299 -  
300 - /**  
301 - * @Title: calcRealDate  
302 - * @Description: TODO(计算班次的真实执行日期)  
303 - */  
304 - public void calcRealDate(String startTime, ScheduleRealInfo sch) {  
305 - try {  
306 - if (null == startTime)  
307 - return;  
308 -  
309 - if (null == sch.getBcsj())  
310 - sch.setBcsj(0);  
311 -  
312 - String rq = sch.getScheduleDateStr();  
313 - //计发时间  
314 - sch.setFcsjT(parseTime(rq, sch.getFcsj(), startTime));  
315 - //待发时间  
316 - sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime));  
317 - //计划终点时间  
318 - if (StringUtils.isEmpty(sch.getZdsj())) {  
319 - sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));  
320 - sch.setZdsj(fmtHHmm.print(sch.getZdsjT()));  
321 - } else  
322 - sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));  
323 - //实发时间  
324 - if (StringUtils.isNotEmpty(sch.getFcsjActual()))  
325 - sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime));  
326 -  
327 - //实达时间  
328 - if (StringUtils.isNotEmpty(sch.getZdsjActual()))  
329 - sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime));  
330 - } catch (Exception e) {  
331 - logger.error("", e);  
332 - }  
333 - }  
334 -  
335 - private long parseTime(String rq, String sj, String startTime) {  
336 - long t = fmtyyyyMMddHHmm.parseMillis(rq + sj);  
337 - if (sj.compareTo(startTime) < 0) {  
338 - t += DAY_TIME;  
339 - }  
340 - return t;  
341 - }  
342 -} 1 +package com.bsth.redis;
  2 +
  3 +import com.bsth.Application;
  4 +import com.bsth.common.BasicData;
  5 +import com.bsth.entity.ScheduleRealInfo;
  6 +import com.bsth.redis.util.RedisUtils;
  7 +import com.bsth.repository.ScheduleRealInfoRepository;
  8 +import com.bsth.server_rs.base_info.line.Line;
  9 +import com.bsth.server_rs.base_info.line.buffer.LineBufferData;
  10 +import com.bsth.util.ConfigUtil;
  11 +import com.bsth.util.ConvertUtil;
  12 +import com.google.common.collect.ArrayListMultimap;
  13 +import org.apache.commons.lang3.StringUtils;
  14 +import org.joda.time.DateTime;
  15 +import org.joda.time.format.DateTimeFormat;
  16 +import org.joda.time.format.DateTimeFormatter;
  17 +import org.slf4j.Logger;
  18 +import org.slf4j.LoggerFactory;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.boot.CommandLineRunner;
  21 +import org.springframework.core.annotation.Order;
  22 +import org.springframework.data.redis.core.ListOperations;
  23 +import org.springframework.data.redis.core.RedisTemplate;
  24 +import org.springframework.data.redis.serializer.StringRedisSerializer;
  25 +import org.springframework.stereotype.Component;
  26 +import org.springframework.stereotype.Service;
  27 +
  28 +import java.util.ArrayList;
  29 +import java.util.Iterator;
  30 +import java.util.List;
  31 +import java.util.concurrent.TimeUnit;
  32 +
  33 +/**
  34 + * 班次 redis 缓存管理
  35 + * Created by panzhao on 2017/3/13.
  36 + */
  37 +@Service
  38 +@Order(2)
  39 +public class ScheduleRedisService implements CommandLineRunner {
  40 +
  41 + @Autowired
  42 + ScheduleRealInfoRepository scheduleRealInfoRepository;
  43 +
  44 + private final static String REDIS_KEY_PREFIX = "schedule:";
  45 +
  46 + @Autowired
  47 + private RedisTemplate redisTemplate;
  48 +
  49 + @Autowired
  50 + RedisUtils redisUtils;
  51 +
  52 + static Logger logger = LoggerFactory.getLogger(ScheduleRedisService.class);
  53 +
  54 + private final static long DAY_TIME = 1000 * 60 * 60 * 24L;
  55 +
  56 + /**
  57 + * 将一批班次写入redis
  58 + *
  59 + * @param list
  60 + */
  61 + public void wirte(List<ScheduleRealInfo> list) {
  62 + ArrayListMultimap<String, ScheduleRealInfo> multimap;
  63 + try {
  64 + if (list.size() == 0)
  65 + return;
  66 + //按日期和线路分组数据
  67 + Class clazz = ScheduleRealInfo.class;
  68 + multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDateStr"));
  69 +
  70 + //写入redis
  71 + Iterator<String> iterator = multimap.keySet().iterator();
  72 + String key;
  73 + while (iterator.hasNext()) {
  74 + key = iterator.next();
  75 + replace(key, multimap.get(key));
  76 + }
  77 + } catch (Exception e) {
  78 + logger.error("", e);
  79 + }
  80 + }
  81 +
  82 + /**
  83 + * 将 list 与redis里的数据合并
  84 + *
  85 + * @param key
  86 + * @param list
  87 + */
  88 + public void mergeData(String key, List<ScheduleRealInfo> list) {
  89 + key = REDIS_KEY_PREFIX + key.replaceAll("-", "");
  90 +
  91 + ListOperations<String, ScheduleRealInfo> ops = redisTemplate.opsForList();
  92 + List<ScheduleRealInfo> cacheList = ops.range(key, 0, -1);
  93 +
  94 + for (ScheduleRealInfo sch : cacheList) {
  95 + if (!list.contains(sch))
  96 + list.add(sch);
  97 + }
  98 +
  99 + //更新
  100 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  101 + }
  102 +
  103 + /**
  104 + * 覆盖数据
  105 + *
  106 + * @param key
  107 + * @param list
  108 + */
  109 + public void replace(String key, List<ScheduleRealInfo> list) {
  110 + key = REDIS_KEY_PREFIX + key.replaceAll("-", "");
  111 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  112 + }
  113 +
  114 + /**
  115 + * 删除数据
  116 + *
  117 + * @param key
  118 + */
  119 + public void delete(String lineCode, String rq) {
  120 + String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", "");
  121 + redisTemplate.delete(key);
  122 + }
  123 +
  124 + /**
  125 + * 根据日期和线路编码从redis获取班次
  126 + *
  127 + * @param dateStr
  128 + * @param lineCode
  129 + * @return
  130 + */
  131 + public List<ScheduleRealInfo> read(String dateStr, String lineCode) {
  132 + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1);
  133 + }
  134 +
  135 + /**
  136 + * 返回指定日期,公司的实际排班,并按线路_车辆分组
  137 + *
  138 + * @param rq
  139 + * @param companyId
  140 + * @return
  141 + */
  142 + public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByNbbm(String rq, String companyId) {
  143 + List<String> lineArray = LineBufferData.findCodesByCompany(companyId);
  144 + ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
  145 +
  146 + rq = rq.replaceAll("-", "");
  147 + List<ScheduleRealInfo> list;
  148 + for (String lineCode : lineArray) {
  149 +
  150 + list = read(rq, lineCode);
  151 +
  152 + for (ScheduleRealInfo sch : list) {
  153 + rs.put(sch.getXlBm() + "_" + sch.getClZbh(), sch);
  154 + }
  155 + }
  156 + return rs;
  157 + }
  158 +
  159 + /**
  160 + * 返回指定日期,公司的实际排班,并按线路分组
  161 + *
  162 + * @param rq
  163 + * @param companyId
  164 + * @return
  165 + */
  166 + public ArrayListMultimap<String, ScheduleRealInfo> findByDateAndGroupByLine(String rq, String companyId) {
  167 +
  168 + List<String> lineArray = LineBufferData.findCodesByCompany(companyId);
  169 + ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
  170 +
  171 + rq = rq.replaceAll("-", "");
  172 + List<ScheduleRealInfo> list;
  173 + for (String lineCode : lineArray) {
  174 +
  175 + list = read(rq, lineCode);
  176 +
  177 + for (ScheduleRealInfo sch : list) {
  178 + rs.put(sch.getXlBm(), sch);
  179 + }
  180 + }
  181 + return rs;
  182 + }
  183 +
  184 + /**
  185 + * 返回指定日期,所有实际排班,并按自编号分组
  186 + *
  187 + * @param rq
  188 + * @return
  189 + */
  190 + public ArrayListMultimap<String, ScheduleRealInfo> findByDate(String rq) {
  191 +
  192 + ArrayListMultimap<String, ScheduleRealInfo> rs = ArrayListMultimap.create();
  193 +
  194 + List<ScheduleRealInfo> list = scheduleRealInfoRepository.findAll(rq);
  195 + for (ScheduleRealInfo sch : list) {
  196 + rs.put(sch.getClZbh(), sch);
  197 + }
  198 + return rs;
  199 + }
  200 +
  201 + @Autowired
  202 + ScheduleRefreshThread scheduleRefreshThread;
  203 + @Autowired
  204 + ScheduleClearThread scheduleClearThread;
  205 +
  206 + @Override
  207 + public void run(String... strings) throws Exception {
  208 + //用子线程去加载,,不要阻塞
  209 + Application.mainServices.schedule(new Runnable() {
  210 + @Override
  211 + public void run() {
  212 + try {
  213 + logger.info("redis 实际排班 start...");
  214 + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
  215 + //设置key 序列化器
  216 + redisTemplate.setKeySerializer(new StringRedisSerializer());
  217 +
  218 + DateTime dt = new DateTime();
  219 + dt = dt.minusDays(cacheDays);
  220 + List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd"));
  221 + calcTime(list);
  222 + //写入redis
  223 + wirte(list);
  224 + logger.info("redis 实际排班 over...");
  225 + } catch (Exception e) {
  226 + logger.info("redis 实际排班 异常", e);
  227 + }
  228 + }
  229 + }, 5, TimeUnit.SECONDS);
  230 +
  231 + //定时刷新一次当日实际排班
  232 + int minute = 10;
  233 + Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute + 4, minute, TimeUnit.MINUTES);
  234 + //24小时清理一次实际排班数据
  235 + Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS);
  236 + }
  237 +
  238 + public List<ScheduleRealInfo> findByMultiLine(List<String> lineArray, String rq) {
  239 + rq = rq.replaceAll("-", "");
  240 + List<ScheduleRealInfo> rs = new ArrayList<>();
  241 + for (String lineCode : lineArray) {
  242 + rs.addAll(read(rq, lineCode));
  243 + }
  244 + return rs;
  245 + }
  246 +
  247 + @Component
  248 + public static class ScheduleRefreshThread extends Thread {
  249 +
  250 + @Autowired
  251 + ScheduleRealInfoRepository realInfoRepository;
  252 +
  253 + @Autowired
  254 + ScheduleRedisService scheduleRedisService;
  255 +
  256 + @Override
  257 + public void run() {
  258 + try {
  259 + DateTime dt = new DateTime();
  260 + DateTime yesterday = dt.minusDays(2);
  261 +
  262 + String rq = yesterday.toString("yyyy-MM-dd");
  263 + logger.info("refresh lt yesterday ..." + rq + " -start");
  264 + List<ScheduleRealInfo> list = realInfoRepository.findByDateLT(rq);
  265 +
  266 + //计算时间戳
  267 + scheduleRedisService.calcTime(list);
  268 + scheduleRedisService.wirte(list);
  269 +
  270 + logger.info("refresh lt yesterday ..." + rq + " -end### size: " + list.size());
  271 + } catch (Exception e) {
  272 + logger.error("", e);
  273 + }
  274 + }
  275 + }
  276 +
  277 + @Component
  278 + public static class ScheduleClearThread extends Thread {
  279 +
  280 + @Autowired
  281 + ScheduleRedisService scheduleRedisService;
  282 +
  283 + @Override
  284 + public void run() {
  285 + try {
  286 + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
  287 + DateTime dt = new DateTime();
  288 + dt = dt.minusDays(cacheDays);
  289 + String rq = dt.toString("yyyy-MM-dd");
  290 +
  291 + List<Line> lines = LineBufferData.findAll();
  292 + for (Line line : lines) {
  293 + scheduleRedisService.delete(line.getLineCode(), rq);
  294 + }
  295 + } catch (Exception e) {
  296 + logger.error("", e);
  297 + }
  298 + }
  299 + }
  300 +
  301 + /**
  302 + * ############ 时间戳计算 ##########
  303 + */
  304 + private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"),
  305 + fmtHHmm = DateTimeFormat.forPattern("HH:mm");
  306 +
  307 + public void calcTime(List<ScheduleRealInfo> list) {
  308 + if (list.size() == 0)
  309 + return;
  310 +
  311 + //计算真实执行日期 和 时间戳
  312 + for (ScheduleRealInfo sch : list) {
  313 + calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch);
  314 + }
  315 + }
  316 +
  317 + /**
  318 + * @Title: calcRealDate
  319 + * @Description: TODO(计算班次的真实执行日期)
  320 + */
  321 + public void calcRealDate(String startTime, ScheduleRealInfo sch) {
  322 + try {
  323 + if (null == startTime)
  324 + return;
  325 +
  326 + if (null == sch.getBcsj())
  327 + sch.setBcsj(0);
  328 +
  329 + String rq = sch.getScheduleDateStr();
  330 + //计发时间
  331 + sch.setFcsjT(parseTime(rq, sch.getFcsj(), startTime));
  332 + //待发时间
  333 + sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime));
  334 + //计划终点时间
  335 + if (StringUtils.isEmpty(sch.getZdsj())) {
  336 + sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
  337 + sch.setZdsj(fmtHHmm.print(sch.getZdsjT()));
  338 + } else
  339 + sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
  340 + //实发时间
  341 + if (StringUtils.isNotEmpty(sch.getFcsjActual()))
  342 + sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime));
  343 +
  344 + //实达时间
  345 + if (StringUtils.isNotEmpty(sch.getZdsjActual()))
  346 + sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime));
  347 + } catch (Exception e) {
  348 + logger.error("", e);
  349 + }
  350 + }
  351 +
  352 + private long parseTime(String rq, String sj, String startTime) {
  353 + long t = fmtyyyyMMddHHmm.parseMillis(rq + sj);
  354 + if (sj.compareTo(startTime) < 0) {
  355 + t += DAY_TIME;
  356 + }
  357 + return t;
  358 + }
  359 +}
src/main/java/com/bsth/repository/ScheduleRealInfoRepository.java
1 -package com.bsth.repository;  
2 -  
3 -import com.bsth.entity.ScheduleRealInfo;  
4 -import org.springframework.data.jpa.repository.EntityGraph;  
5 -import org.springframework.data.jpa.repository.Query;  
6 -import org.springframework.data.repository.PagingAndSortingRepository;  
7 -import org.springframework.stereotype.Repository;  
8 -  
9 -import java.util.List;  
10 -  
11 -@Repository  
12 -public interface ScheduleRealInfoRepository extends PagingAndSortingRepository<ScheduleRealInfo, Long> {  
13 -  
14 - /**  
15 - * 根据日期获取班次信息  
16 - * @param schDate  
17 - * @return  
18 - */  
19 - @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)  
20 - @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr=?1")  
21 - List<ScheduleRealInfo> findAll(String schDate);  
22 -  
23 - /**  
24 - * 根据日期和线路编码获取班次信息  
25 - * @param schDate  
26 - * @param lineCode  
27 - * @return  
28 - */  
29 - @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)  
30 - @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr=?1 and s.xlBm=?2")  
31 - List<ScheduleRealInfo> findAll(String schDate, String lineCode);  
32 -  
33 - /**  
34 - * 获取大于指定日期的班次信息  
35 - * @param schDate  
36 - * @return  
37 - */  
38 - @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)  
39 - @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr>?1")  
40 - List<ScheduleRealInfo> findByDateLT(String schDate);  
41 -} 1 +package com.bsth.repository;
  2 +
  3 +import com.bsth.entity.ScheduleRealInfo;
  4 +import org.springframework.data.jpa.repository.EntityGraph;
  5 +import org.springframework.data.jpa.repository.Query;
  6 +import org.springframework.data.repository.PagingAndSortingRepository;
  7 +import org.springframework.stereotype.Repository;
  8 +
  9 +import java.util.Date;
  10 +import java.util.List;
  11 +
  12 +@Repository
  13 +public interface ScheduleRealInfoRepository extends PagingAndSortingRepository<ScheduleRealInfo, Long> {
  14 +
  15 + /**
  16 + * 根据日期获取班次信息
  17 + * @param schDate
  18 + * @return
  19 + */
  20 + @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)
  21 + @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr=?1")
  22 + List<ScheduleRealInfo> findAll(String schDate);
  23 +
  24 + /**
  25 + * 根据日期和线路编码获取班次信息
  26 + * @param schDate
  27 + * @param lineCode
  28 + * @return
  29 + */
  30 + @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)
  31 + @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr=?1 and s.xlBm=?2")
  32 + List<ScheduleRealInfo> findAll(String schDate, String lineCode);
  33 +
  34 + /**
  35 + * 获取大于指定日期的班次信息
  36 + * @param schDate
  37 + * @return
  38 + */
  39 + @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)
  40 + @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr>?1")
  41 + List<ScheduleRealInfo> findByDateLT(String schDate);
  42 +
  43 + /**
  44 + * 根据日期和公司编码获取班次信息
  45 + * @param schDate
  46 + * @param companyCode
  47 + * @return
  48 + */
  49 + @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)
  50 + @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr=?1 and s.gsBm=?2")
  51 + List<ScheduleRealInfo> findByDateAndCompany(String schDate, String companyCode);
  52 +
  53 + /**
  54 + * 根据日期和公司编码获取班次信息
  55 + * @param schDate
  56 + * @param companyCode
  57 + * @return
  58 + */
  59 + @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)
  60 + @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr=?1 and s.gsBm=?2 and s.updateDate>=?3")
  61 + List<ScheduleRealInfo> findByDateAndCompany(String schDate, String companyCode, Date timestamp);
  62 +
  63 + /**
  64 + * 根据日期和公司编码获取班次信息
  65 + * @param schDates
  66 + * @param timestamp
  67 + * @return
  68 + */
  69 + @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)
  70 + @Query("select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr in (?1) and s.updateDate>=?2")
  71 + List<ScheduleRealInfo> findByDates(List<String> schDates, Date timestamp);
  72 +
  73 + /**
  74 + * 根据日期区间获取班次信息
  75 + * @param startDate
  76 + * @param endDate
  77 + * @return
  78 + */
  79 + @EntityGraph(value = "scheduleRealInfo_cTasks", type = EntityGraph.EntityGraphType.FETCH)
  80 + @Query(value="select DISTINCT s from ScheduleRealInfo s where s.scheduleDateStr between ?1 and ?2 order by s.scheduleDateStr, s.gsBm, s.fgsBm, s.xlBm")
  81 + List<ScheduleRealInfo> findScheduleByDates(String startDate, String endDate);
  82 +
  83 + @Query(value = " select bc_type from bsth_c_s_sp_info where id = ?1 ", nativeQuery = true)
  84 + List<String> findoOriginalType(Long spId);
  85 +}
src/main/java/com/bsth/server_rs/external/ExternalService.java
1 package com.bsth.server_rs.external; 1 package com.bsth.server_rs.external;
2 2
  3 +import com.bsth.entity.ScheduleRealInfo;
3 import com.bsth.redis.ScheduleRedisService; 4 import com.bsth.redis.ScheduleRedisService;
4 import com.bsth.repository.ScheduleRealInfoRepository; 5 import com.bsth.repository.ScheduleRealInfoRepository;
5 import com.bsth.server_rs.AuthorizeInterceptor_IN; 6 import com.bsth.server_rs.AuthorizeInterceptor_IN;
@@ -15,7 +16,11 @@ import com.bsth.server_rs.gps.buffer.BasicDataBuffer; @@ -15,7 +16,11 @@ import com.bsth.server_rs.gps.buffer.BasicDataBuffer;
15 import com.bsth.server_rs.gps.buffer.GpsRealDataBuffer; 16 import com.bsth.server_rs.gps.buffer.GpsRealDataBuffer;
16 import com.bsth.server_rs.gps.dao.HistoryGpsDao; 17 import com.bsth.server_rs.gps.dao.HistoryGpsDao;
17 import com.bsth.server_rs.gps.entity.GpsEntity; 18 import com.bsth.server_rs.gps.entity.GpsEntity;
  19 +import com.bsth.server_rs.schedule.dto.ScheduleRealInfoDTO_JK;
18 import org.apache.commons.lang3.StringUtils; 20 import org.apache.commons.lang3.StringUtils;
  21 +import org.joda.time.DateTime;
  22 +import org.joda.time.format.DateTimeFormat;
  23 +import org.joda.time.format.DateTimeFormatter;
19 import org.slf4j.Logger; 24 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory; 25 import org.slf4j.LoggerFactory;
21 import org.springframework.beans.factory.annotation.Autowired; 26 import org.springframework.beans.factory.annotation.Autowired;
@@ -330,25 +335,33 @@ public class ExternalService { @@ -330,25 +335,33 @@ public class ExternalService {
330 // return result; 335 // return result;
331 // } 336 // }
332 337
333 -// @GET  
334 -// @Path("/waybill/{company}/{rq}")  
335 -// public List<ScheduleRealInfoDTO_JK> sch_jk(@PathParam("company") String company, @PathParam("rq") String rq) {  
336 -// DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");  
337 -// DateTime dateTime = fmt.parseDateTime(rq);  
338 -// if (!dateTime.isBefore(DateTime.now().withTimeAtStartOfDay())) {  
339 -// return new ArrayList<>();  
340 -// }  
341 -// List<ScheduleRealInfo> scheduleRealInfos = scheduleRealInfoRepository.findByDateAndCompany(dateTime.toString("yyyy-MM-dd"), company);  
342 -// scheduleRedisService.calcTime(scheduleRealInfos);  
343 -// List<ScheduleRealInfoDTO_JK> result = ScheduleRealInfoDTO_JK.getMultiInstance(scheduleRealInfos);  
344 -// for (ScheduleRealInfoDTO_JK jk : result) {  
345 -// if (jk.getZdsjActualTime() == null) {  
346 -// jk.setZdsjActualTime(jk.getZdsjT());  
347 -// }  
348 -// }  
349 -//  
350 -// return result;  
351 -// } 338 + @GET
  339 + @Path("/waybill/{company}/{rq}")
  340 + public List<ScheduleRealInfoDTO_JK> sch_jk(@PathParam("company") String company, @PathParam("rq") String rq, @Context HttpServletRequest request) {
  341 + DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");
  342 + DateTime dateTime = fmt.parseDateTime(rq);
  343 + if (!dateTime.isBefore(DateTime.now().withTimeAtStartOfDay())) {
  344 + return new ArrayList<>();
  345 + }
  346 + String password = request.getParameter("password");
  347 + Set<String> limitLines = authorizeInterceptorIn.getLimitLines(password);
  348 + StringBuilder sb = new StringBuilder();
  349 + if (limitLines == null || limitLines.isEmpty()) {
  350 + return new ArrayList<>();
  351 + }
  352 + List<ScheduleRealInfo> scheduleRealInfos = scheduleRealInfoRepository.findByDateAndCompany(dateTime.toString("yyyy-MM-dd"), company);
  353 + scheduleRedisService.calcTime(scheduleRealInfos);
  354 + List<ScheduleRealInfoDTO_JK> jks = ScheduleRealInfoDTO_JK.getMultiInstance(scheduleRealInfos), result = new ArrayList<>();
  355 +
  356 + for (ScheduleRealInfoDTO_JK jk : jks) {
  357 + String lineCode = jk.getLineCode();
  358 + if (limitLines.contains(lineCode) || limitLines.contains("ALL")) {
  359 + result.add(jk);
  360 + }
  361 + }
  362 +
  363 + return result;
  364 + }
352 365
353 @GET 366 @GET
354 @Path("/limits/reload") 367 @Path("/limits/reload")