Commit 037dc6eb0c5e90f1647171d0dd68e1f16cbcf6e7
1 parent
0d23bffc
1.调度接口中计算应到时间的口径与调度系统改为一致
Showing
1 changed file
with
342 additions
and
342 deletions
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 | + @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.getFcsjT() + (sch.getBcsj() * 60 * 1000)); | |
| 320 | + sch.setZdsj(fmtHHmm.print(sch.getZdsjT())); | |
| 321 | + } else | |
| 322 | + sch.setZdsjT(sch.getFcsjT() + (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 | +} | ... | ... |