Commit 253fa772246555100a69489d12ac95e69bb93134
1 parent
f2f80edc
1.票务数据接口逻辑层级变更
Showing
2 changed files
with
371 additions
and
351 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 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 196 | - //设置key 序列化器 | ||
| 197 | - redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
| 198 | - | ||
| 199 | - DateTime dt = new DateTime(); | ||
| 200 | - dt = dt.minusDays(cacheDays); | ||
| 201 | - List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd")); | ||
| 202 | - calcTime(list); | ||
| 203 | - //写入redis | ||
| 204 | - wirte(list); | ||
| 205 | - logger.info("redis 实际排班 over..."); | ||
| 206 | - } | ||
| 207 | - }, 5, TimeUnit.SECONDS); | ||
| 208 | - | ||
| 209 | - //定时刷新一次当日实际排班 | ||
| 210 | - int minute = 10; | ||
| 211 | - Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute + 4, minute, TimeUnit.MINUTES); | ||
| 212 | - //24小时清理一次实际排班数据 | ||
| 213 | - Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS); | ||
| 214 | - } | ||
| 215 | - | ||
| 216 | - public List<ScheduleRealInfo> findByMultiLine(List<String> lineArray, String rq) { | ||
| 217 | - rq = rq.replaceAll("-", ""); | ||
| 218 | - List<ScheduleRealInfo> rs = new ArrayList<>(); | ||
| 219 | - for (String lineCode : lineArray) { | ||
| 220 | - rs.addAll(read(rq, lineCode)); | ||
| 221 | - } | ||
| 222 | - return rs; | ||
| 223 | - } | ||
| 224 | - | ||
| 225 | - @Component | ||
| 226 | - public static class ScheduleRefreshThread extends Thread { | ||
| 227 | - | ||
| 228 | - @Autowired | ||
| 229 | - ScheduleRealInfoRepository realInfoRepository; | ||
| 230 | - | ||
| 231 | - @Autowired | ||
| 232 | - ScheduleRedisService scheduleRedisService; | ||
| 233 | - | ||
| 234 | - @Override | ||
| 235 | - public void run() { | ||
| 236 | - try { | ||
| 237 | - DateTime dt = new DateTime(); | ||
| 238 | - DateTime yesterday = dt.minusDays(2); | ||
| 239 | - | ||
| 240 | - String rq = yesterday.toString("yyyy-MM-dd"); | ||
| 241 | - logger.info("refresh lt yesterday ..." + rq + " -start"); | ||
| 242 | - List<ScheduleRealInfo> list = realInfoRepository.findByDateLT(rq); | ||
| 243 | - | ||
| 244 | - //计算时间戳 | ||
| 245 | - scheduleRedisService.calcTime(list); | ||
| 246 | - scheduleRedisService.wirte(list); | ||
| 247 | - | ||
| 248 | - logger.info("refresh lt yesterday ..." + rq + " -end### size: " + list.size()); | ||
| 249 | - } catch (Exception e) { | ||
| 250 | - logger.error("", e); | ||
| 251 | - } | ||
| 252 | - } | ||
| 253 | - } | ||
| 254 | - | ||
| 255 | - @Component | ||
| 256 | - public static class ScheduleClearThread extends Thread { | ||
| 257 | - | ||
| 258 | - @Autowired | ||
| 259 | - ScheduleRedisService scheduleRedisService; | ||
| 260 | - | ||
| 261 | - @Override | ||
| 262 | - public void run() { | ||
| 263 | - try { | ||
| 264 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 265 | - DateTime dt = new DateTime(); | ||
| 266 | - dt = dt.minusDays(cacheDays); | ||
| 267 | - String rq = dt.toString("yyyy-MM-dd"); | ||
| 268 | - | ||
| 269 | - List<Line> lines = LineBufferData.findAll(); | ||
| 270 | - for (Line line : lines) { | ||
| 271 | - scheduleRedisService.delete(line.getLineCode(), rq); | ||
| 272 | - } | ||
| 273 | - } catch (Exception e) { | ||
| 274 | - logger.error("", e); | ||
| 275 | - } | ||
| 276 | - } | ||
| 277 | - } | ||
| 278 | - | ||
| 279 | - /** | ||
| 280 | - * ############ 时间戳计算 ########## | ||
| 281 | - */ | ||
| 282 | - private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"), | ||
| 283 | - fmtHHmm = DateTimeFormat.forPattern("HH:mm"); | ||
| 284 | - | ||
| 285 | - private void calcTime(List<ScheduleRealInfo> list) { | ||
| 286 | - if (list.size() == 0) | ||
| 287 | - return; | ||
| 288 | - | ||
| 289 | - //计算真实执行日期 和 时间戳 | ||
| 290 | - for (ScheduleRealInfo sch : list) { | ||
| 291 | - calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch); | ||
| 292 | - } | ||
| 293 | - } | ||
| 294 | - | ||
| 295 | - /** | ||
| 296 | - * @Title: calcRealDate | ||
| 297 | - * @Description: TODO(计算班次的真实执行日期) | ||
| 298 | - */ | ||
| 299 | - public void calcRealDate(String startTime, ScheduleRealInfo sch) { | ||
| 300 | - try { | ||
| 301 | - if (null == startTime) | ||
| 302 | - return; | ||
| 303 | - | ||
| 304 | - if (null == sch.getBcsj()) | ||
| 305 | - sch.setBcsj(0); | ||
| 306 | - | ||
| 307 | - String rq = sch.getScheduleDateStr(); | ||
| 308 | - //计发时间 | ||
| 309 | - sch.setFcsjT(parseTime(rq, sch.getFcsj(), startTime)); | ||
| 310 | - //待发时间 | ||
| 311 | - sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime)); | ||
| 312 | - //计划终点时间 | ||
| 313 | - if (StringUtils.isEmpty(sch.getZdsj())) { | ||
| 314 | - sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); | ||
| 315 | - sch.setZdsj(fmtHHmm.print(sch.getZdsjT())); | ||
| 316 | - } else | ||
| 317 | - sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); | ||
| 318 | - //实发时间 | ||
| 319 | - if (StringUtils.isNotEmpty(sch.getFcsjActual())) | ||
| 320 | - sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime)); | ||
| 321 | - | ||
| 322 | - //实达时间 | ||
| 323 | - if (StringUtils.isNotEmpty(sch.getZdsjActual())) | ||
| 324 | - sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime)); | ||
| 325 | - } catch (Exception e) { | ||
| 326 | - logger.error("", e); | ||
| 327 | - } | ||
| 328 | - } | ||
| 329 | - | ||
| 330 | - private long parseTime(String rq, String sj, String startTime) { | ||
| 331 | - long t = fmtyyyyMMddHHmm.parseMillis(rq + sj); | ||
| 332 | - if (sj.compareTo(startTime) < 0) { | ||
| 333 | - t += DAY_TIME; | ||
| 334 | - } | ||
| 335 | - return t; | ||
| 336 | - } | ||
| 337 | -} | 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 | + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 196 | + //设置key 序列化器 | ||
| 197 | + redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
| 198 | + | ||
| 199 | + DateTime dt = new DateTime(); | ||
| 200 | + dt = dt.minusDays(cacheDays); | ||
| 201 | + List<ScheduleRealInfo> list = scheduleRealInfoRepository.findByDateLT(dt.toString("yyyy-MM-dd")); | ||
| 202 | + calcTime(list); | ||
| 203 | + //写入redis | ||
| 204 | + wirte(list); | ||
| 205 | + logger.info("redis 实际排班 over..."); | ||
| 206 | + } | ||
| 207 | + }, 5, TimeUnit.SECONDS); | ||
| 208 | + | ||
| 209 | + //定时刷新一次当日实际排班 | ||
| 210 | + int minute = 10; | ||
| 211 | + Application.mainServices.scheduleWithFixedDelay(scheduleRefreshThread, minute + 4, minute, TimeUnit.MINUTES); | ||
| 212 | + //24小时清理一次实际排班数据 | ||
| 213 | + Application.mainServices.scheduleWithFixedDelay(scheduleClearThread, 24, 24, TimeUnit.HOURS); | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + public List<ScheduleRealInfo> findByMultiLine(List<String> lineArray, String rq) { | ||
| 217 | + rq = rq.replaceAll("-", ""); | ||
| 218 | + List<ScheduleRealInfo> rs = new ArrayList<>(); | ||
| 219 | + for (String lineCode : lineArray) { | ||
| 220 | + rs.addAll(read(rq, lineCode)); | ||
| 221 | + } | ||
| 222 | + return rs; | ||
| 223 | + } | ||
| 224 | + | ||
| 225 | + @Component | ||
| 226 | + public static class ScheduleRefreshThread extends Thread { | ||
| 227 | + | ||
| 228 | + @Autowired | ||
| 229 | + ScheduleRealInfoRepository realInfoRepository; | ||
| 230 | + | ||
| 231 | + @Autowired | ||
| 232 | + ScheduleRedisService scheduleRedisService; | ||
| 233 | + | ||
| 234 | + @Override | ||
| 235 | + public void run() { | ||
| 236 | + try { | ||
| 237 | + DateTime dt = new DateTime(); | ||
| 238 | + DateTime yesterday = dt.minusDays(2); | ||
| 239 | + | ||
| 240 | + String rq = yesterday.toString("yyyy-MM-dd"); | ||
| 241 | + logger.info("refresh lt yesterday ..." + rq + " -start"); | ||
| 242 | + List<ScheduleRealInfo> list = realInfoRepository.findByDateLT(rq); | ||
| 243 | + | ||
| 244 | + //计算时间戳 | ||
| 245 | + scheduleRedisService.calcTime(list); | ||
| 246 | + scheduleRedisService.wirte(list); | ||
| 247 | + | ||
| 248 | + logger.info("refresh lt yesterday ..." + rq + " -end### size: " + list.size()); | ||
| 249 | + } catch (Exception e) { | ||
| 250 | + logger.error("", e); | ||
| 251 | + } | ||
| 252 | + } | ||
| 253 | + } | ||
| 254 | + | ||
| 255 | + @Component | ||
| 256 | + public static class ScheduleClearThread extends Thread { | ||
| 257 | + | ||
| 258 | + @Autowired | ||
| 259 | + ScheduleRedisService scheduleRedisService; | ||
| 260 | + | ||
| 261 | + @Override | ||
| 262 | + public void run() { | ||
| 263 | + try { | ||
| 264 | + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | ||
| 265 | + DateTime dt = new DateTime(); | ||
| 266 | + dt = dt.minusDays(cacheDays); | ||
| 267 | + String rq = dt.toString("yyyy-MM-dd"); | ||
| 268 | + | ||
| 269 | + List<Line> lines = LineBufferData.findAll(); | ||
| 270 | + for (Line line : lines) { | ||
| 271 | + scheduleRedisService.delete(line.getLineCode(), rq); | ||
| 272 | + } | ||
| 273 | + } catch (Exception e) { | ||
| 274 | + logger.error("", e); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + } | ||
| 278 | + | ||
| 279 | + /** | ||
| 280 | + * ############ 时间戳计算 ########## | ||
| 281 | + */ | ||
| 282 | + private static DateTimeFormatter fmtyyyyMMddHHmm = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm"), | ||
| 283 | + fmtHHmm = DateTimeFormat.forPattern("HH:mm"); | ||
| 284 | + | ||
| 285 | + private void calcTime(List<ScheduleRealInfo> list) { | ||
| 286 | + if (list.size() == 0) | ||
| 287 | + return; | ||
| 288 | + | ||
| 289 | + //计算真实执行日期 和 时间戳 | ||
| 290 | + for (ScheduleRealInfo sch : list) { | ||
| 291 | + calcRealDate(BasicData.lineStartTimeMap.get(sch.getXlBm()), sch); | ||
| 292 | + } | ||
| 293 | + } | ||
| 294 | + | ||
| 295 | + /** | ||
| 296 | + * @Title: calcRealDate | ||
| 297 | + * @Description: TODO(计算班次的真实执行日期) | ||
| 298 | + */ | ||
| 299 | + public void calcRealDate(String startTime, ScheduleRealInfo sch) { | ||
| 300 | + try { | ||
| 301 | + if (null == startTime) | ||
| 302 | + return; | ||
| 303 | + | ||
| 304 | + if (null == sch.getBcsj()) | ||
| 305 | + sch.setBcsj(0); | ||
| 306 | + | ||
| 307 | + String rq = sch.getScheduleDateStr(); | ||
| 308 | + //计发时间 | ||
| 309 | + sch.setFcsjT(parseTime(rq, sch.getFcsj(), startTime)); | ||
| 310 | + //待发时间 | ||
| 311 | + sch.setDfsjT(parseTime(rq, sch.getDfsj(), startTime)); | ||
| 312 | + //计划终点时间 | ||
| 313 | + if (StringUtils.isEmpty(sch.getZdsj())) { | ||
| 314 | + sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); | ||
| 315 | + sch.setZdsj(fmtHHmm.print(sch.getZdsjT())); | ||
| 316 | + } else | ||
| 317 | + sch.setZdsjT(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000)); | ||
| 318 | + //实发时间 | ||
| 319 | + if (StringUtils.isNotEmpty(sch.getFcsjActual())) | ||
| 320 | + sch.setFcsjActualAll(parseTime(rq, sch.getFcsjActual(), startTime)); | ||
| 321 | + | ||
| 322 | + //实达时间 | ||
| 323 | + if (StringUtils.isNotEmpty(sch.getZdsjActual())) | ||
| 324 | + sch.setZdsjActualAll(parseTime(rq, sch.getZdsjActual(), startTime)); | ||
| 325 | + } catch (Exception e) { | ||
| 326 | + logger.error("", e); | ||
| 327 | + } | ||
| 328 | + } | ||
| 329 | + | ||
| 330 | + private long parseTime(String rq, String sj, String startTime) { | ||
| 331 | + long t = fmtyyyyMMddHHmm.parseMillis(rq + sj); | ||
| 332 | + if (sj.compareTo(startTime) < 0) { | ||
| 333 | + t += DAY_TIME; | ||
| 334 | + } | ||
| 335 | + return t; | ||
| 336 | + } | ||
| 337 | +} |
src/main/java/com/bsth/server_rs/waybill/WaybillRestService.java
| @@ -29,6 +29,9 @@ import com.bsth.server_ws.util.ScheduleCalculator; | @@ -29,6 +29,9 @@ import com.bsth.server_ws.util.ScheduleCalculator; | ||
| 29 | import com.bsth.util.ConvertUtil; | 29 | import com.bsth.util.ConvertUtil; |
| 30 | import com.google.common.collect.ArrayListMultimap; | 30 | import com.google.common.collect.ArrayListMultimap; |
| 31 | 31 | ||
| 32 | +import ch.qos.logback.classic.Level; | ||
| 33 | +import ch.qos.logback.classic.LoggerContext; | ||
| 34 | + | ||
| 32 | /** | 35 | /** |
| 33 | * @author Hill. | 36 | * @author Hill. |
| 34 | */ | 37 | */ |
| @@ -74,6 +77,24 @@ public class WaybillRestService { | @@ -74,6 +77,24 @@ public class WaybillRestService { | ||
| 74 | return map; | 77 | return map; |
| 75 | } | 78 | } |
| 76 | 79 | ||
| 80 | + @GET | ||
| 81 | + @Path("/level/{level}") | ||
| 82 | + public Map<String, Object> waybill(@PathParam("level") String level) { | ||
| 83 | + Map<String, Object> map = new HashMap<>(); | ||
| 84 | + try { | ||
| 85 | + LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory(); | ||
| 86 | + context.getLogger("root").setLevel(Level.toLevel(level)); | ||
| 87 | + map.put("errCode", 0); | ||
| 88 | + map.put("errMsg", "成功"); | ||
| 89 | + } catch (Exception e) { | ||
| 90 | + logger.error("", e); | ||
| 91 | + map.put("errCode", 1); | ||
| 92 | + map.put("errMsg", "服务器出现异常!"); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + return map; | ||
| 96 | + } | ||
| 97 | + | ||
| 77 | private List<Waybill> toNhWaybill(ArrayListMultimap<String, ScheduleRealInfo> listMap, List<DutyEmployee> des) { | 98 | private List<Waybill> toNhWaybill(ArrayListMultimap<String, ScheduleRealInfo> listMap, List<DutyEmployee> des) { |
| 78 | Set<String> nbbms = listMap.keySet(); | 99 | Set<String> nbbms = listMap.keySet(); |
| 79 | List<Waybill> result = new ArrayList<Waybill>(nbbms.size()); | 100 | List<Waybill> result = new ArrayList<Waybill>(nbbms.size()); |
| @@ -92,24 +113,23 @@ public class WaybillRestService { | @@ -92,24 +113,23 @@ public class WaybillRestService { | ||
| 92 | list = listMap.get(nbbm); | 113 | list = listMap.get(nbbm); |
| 93 | if (list.size() == 0) | 114 | if (list.size() == 0) |
| 94 | continue; | 115 | continue; |
| 95 | - //班次信息 | ||
| 96 | - Waybill wb = new Waybill(); | ||
| 97 | - sch = list.get(0); | ||
| 98 | - //日期 | ||
| 99 | - wb.setDate(sch.getScheduleDateStr()); | ||
| 100 | - //车辆自编号 | ||
| 101 | - wb.setInsideCode(sch.getClZbh()); | ||
| 102 | - //线路编码 | ||
| 103 | - wb.setLineCode(sch.getXlBm()); | ||
| 104 | - wb.setLineName(sch.getXlName()); | ||
| 105 | - wb.setCompanyCode(sch.getGsBm()); | ||
| 106 | - wb.setBranchCompanyCode(sch.getFgsBm()); | ||
| 107 | - System.out.println(sch.getFgsBm() + "_" + sch.getFgsName()); | ||
| 108 | 116 | ||
| 109 | //按 驾驶员 分组班次,构造路单子项 | 117 | //按 驾驶员 分组班次,构造路单子项 |
| 110 | jGhListMap = new ConvertUtil<ScheduleRealInfo>().groupMultiList(list, "_", jGhField); | 118 | jGhListMap = new ConvertUtil<ScheduleRealInfo>().groupMultiList(list, "_", jGhField); |
| 111 | for (String jGh : jGhListMap.keySet()) { | 119 | for (String jGh : jGhListMap.keySet()) { |
| 112 | list = jGhListMap.get(jGh); | 120 | list = jGhListMap.get(jGh); |
| 121 | + //班次信息 | ||
| 122 | + Waybill wb = new Waybill(); | ||
| 123 | + sch = list.get(0); | ||
| 124 | + //日期 | ||
| 125 | + wb.setDate(sch.getScheduleDateStr()); | ||
| 126 | + //车辆自编号 | ||
| 127 | + wb.setInsideCode(sch.getClZbh()); | ||
| 128 | + //线路编码 | ||
| 129 | + wb.setLineCode(sch.getXlBm()); | ||
| 130 | + wb.setLineName(sch.getXlName()); | ||
| 131 | + wb.setCompanyCode(sch.getGsBm()); | ||
| 132 | + wb.setBranchCompanyCode(sch.getFgsBm()); | ||
| 113 | //营业公里 | 133 | //营业公里 |
| 114 | wb.setMiles(ScheduleCalculator.calcYYLC(list)); | 134 | wb.setMiles(ScheduleCalculator.calcYYLC(list)); |
| 115 | //驾驶员工号 | 135 | //驾驶员工号 |
| @@ -126,8 +146,8 @@ public class WaybillRestService { | @@ -126,8 +146,8 @@ public class WaybillRestService { | ||
| 126 | break; | 146 | break; |
| 127 | } | 147 | } |
| 128 | } | 148 | } |
| 149 | + result.add(wb); | ||
| 129 | } | 150 | } |
| 130 | - result.add(wb); | ||
| 131 | } | 151 | } |
| 132 | 152 | ||
| 133 | return result; | 153 | return result; |