Commit ab576aa9e124831fad494677356f8b278081a586
1 parent
faa4e685
1.计算排班接口中加入明天的数据
Showing
1 changed file
with
198 additions
and
199 deletions
src/main/java/com/bsth/redis/PlanScheduleRedisService.java
| 1 | -package com.bsth.redis; | |
| 2 | - | |
| 3 | -import com.bsth.Application; | |
| 4 | -import com.bsth.entity.SchedulePlanInfo; | |
| 5 | -import com.bsth.redis.util.DateUtils; | |
| 6 | -import com.bsth.redis.util.RedisUtils; | |
| 7 | -import com.bsth.repository.SchedulePlanInfoRepository; | |
| 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.joda.time.DateTime; | |
| 14 | -import org.slf4j.Logger; | |
| 15 | -import org.slf4j.LoggerFactory; | |
| 16 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 17 | -import org.springframework.boot.CommandLineRunner; | |
| 18 | -import org.springframework.core.annotation.Order; | |
| 19 | -import org.springframework.data.redis.core.ListOperations; | |
| 20 | -import org.springframework.data.redis.core.RedisTemplate; | |
| 21 | -import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 22 | -import org.springframework.stereotype.Component; | |
| 23 | -import org.springframework.stereotype.Service; | |
| 24 | - | |
| 25 | -import java.util.ArrayList; | |
| 26 | -import java.util.Date; | |
| 27 | -import java.util.Iterator; | |
| 28 | -import java.util.List; | |
| 29 | -import java.util.concurrent.TimeUnit; | |
| 30 | - | |
| 31 | -/** | |
| 32 | - * 计调的 计划排班redis缓存 | |
| 33 | - * Created by panzhao on 2017/3/27. | |
| 34 | - */ | |
| 35 | -@Service | |
| 36 | -@Order(6) | |
| 37 | -public class PlanScheduleRedisService implements CommandLineRunner { | |
| 38 | - | |
| 39 | - @Autowired | |
| 40 | - private RedisTemplate redisTemplate; | |
| 41 | - | |
| 42 | - @Autowired | |
| 43 | - SchedulePlanInfoRepository planInfoRepository; | |
| 44 | - | |
| 45 | - @Autowired | |
| 46 | - RedisUtils redisUtils; | |
| 47 | - | |
| 48 | - static Logger logger = LoggerFactory.getLogger(PlanScheduleRedisService.class); | |
| 49 | - | |
| 50 | - private final static String REDIS_KEY_PREFIX = "plan:"; | |
| 51 | - | |
| 52 | - /** | |
| 53 | - * 将一批计划写入redis | |
| 54 | - * | |
| 55 | - * @param list | |
| 56 | - */ | |
| 57 | - public void wirte(List<SchedulePlanInfo> list) { | |
| 58 | - ArrayListMultimap<String, SchedulePlanInfo> multimap; | |
| 59 | - try { | |
| 60 | - if (list.size() == 0) | |
| 61 | - return; | |
| 62 | - //按日期和线路分组数据 | |
| 63 | - Class clazz = SchedulePlanInfo.class; | |
| 64 | - multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDate")); | |
| 65 | - | |
| 66 | - //写入redis | |
| 67 | - Iterator<String> iterator = multimap.keySet().iterator(); | |
| 68 | - String key; | |
| 69 | - while (iterator.hasNext()) { | |
| 70 | - key = iterator.next(); | |
| 71 | - replace(key, multimap.get(key)); | |
| 72 | - } | |
| 73 | - } catch (Exception e) { | |
| 74 | - logger.error("", e); | |
| 75 | - } | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * 将 list 与redis里的数据合并 | |
| 80 | - * | |
| 81 | - * @param key | |
| 82 | - * @param list | |
| 83 | - */ | |
| 84 | - public void mergeData(String key, List<SchedulePlanInfo> list) { | |
| 85 | - key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 86 | - | |
| 87 | - ListOperations<String, SchedulePlanInfo> ops = redisTemplate.opsForList(); | |
| 88 | - List<SchedulePlanInfo> cacheList = ops.range(key, 0, -1); | |
| 89 | - | |
| 90 | - for (SchedulePlanInfo plan : cacheList) { | |
| 91 | - if (!list.contains(plan)) | |
| 92 | - list.add(plan); | |
| 93 | - } | |
| 94 | - | |
| 95 | - //更新 | |
| 96 | - redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 97 | - } | |
| 98 | - | |
| 99 | - /** | |
| 100 | - * 覆盖数据 | |
| 101 | - * | |
| 102 | - * @param key | |
| 103 | - * @param list | |
| 104 | - */ | |
| 105 | - public void replace(String key, List<SchedulePlanInfo> list) { | |
| 106 | - key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 107 | - redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 108 | - } | |
| 109 | - | |
| 110 | - /** | |
| 111 | - * 根据日期和线路编码从redis获取计划 | |
| 112 | - * | |
| 113 | - * @param dateStr | |
| 114 | - * @param lineCode | |
| 115 | - * @return | |
| 116 | - */ | |
| 117 | - public List<SchedulePlanInfo> read(String dateStr, String lineCode) { | |
| 118 | - return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1); | |
| 119 | - } | |
| 120 | - | |
| 121 | - | |
| 122 | - @Autowired | |
| 123 | - PlanClearThread planClearThread; | |
| 124 | - | |
| 125 | - @Override | |
| 126 | - public void run(String... strings) throws Exception { | |
| 127 | - Application.mainServices.schedule(new Runnable() { | |
| 128 | - @Override | |
| 129 | - public void run() { | |
| 130 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 131 | - //设置key 序列化器 | |
| 132 | - redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 133 | - | |
| 134 | - DateTime dt = new DateTime(); | |
| 135 | - dt = dt.minusDays(cacheDays); | |
| 136 | - dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays); | |
| 137 | - List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(dt.toDate(), new Date()); | |
| 138 | - //写入redis | |
| 139 | - wirte(list); | |
| 140 | - } | |
| 141 | - }, 60 * 5, TimeUnit.SECONDS); | |
| 142 | - | |
| 143 | - | |
| 144 | - //定时 00:05 分清理计划,并加载当天的计划 | |
| 145 | - long diff = (DateUtils.getTimestamp() + 1000 * 60 * 5) - System.currentTimeMillis(); | |
| 146 | - if (diff < 0) | |
| 147 | - diff += (1000 * 60 * 60 * 24); | |
| 148 | - Application.mainServices.scheduleAtFixedRate(planClearThread, diff / 1000, 60 * 60 * 24, TimeUnit.SECONDS); | |
| 149 | - } | |
| 150 | - | |
| 151 | - public List<SchedulePlanInfo> findByMultiLine(List<String> lineArray, String rq) { | |
| 152 | - rq = rq.replaceAll("-", ""); | |
| 153 | - List<SchedulePlanInfo> rs = new ArrayList<>(); | |
| 154 | - for (String lineCode : lineArray) { | |
| 155 | - rs.addAll(read(rq, lineCode)); | |
| 156 | - } | |
| 157 | - return rs; | |
| 158 | - } | |
| 159 | - | |
| 160 | - @Component | |
| 161 | - public static class PlanClearThread extends Thread { | |
| 162 | - | |
| 163 | - @Autowired | |
| 164 | - PlanScheduleRedisService planRedisService; | |
| 165 | - @Autowired | |
| 166 | - SchedulePlanInfoRepository planInfoRepository; | |
| 167 | - | |
| 168 | - @Override | |
| 169 | - public void run() { | |
| 170 | - try { | |
| 171 | - logger.info("redis -清理计划排班"); | |
| 172 | - | |
| 173 | - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 174 | - DateTime dt = new DateTime(); | |
| 175 | - dt = dt.minusDays(cacheDays); | |
| 176 | - String rq = dt.toString("yyyy-MM-dd"); | |
| 177 | - | |
| 178 | - List<Line> lines = LineBufferData.findAll(); | |
| 179 | - for (Line line : lines) { | |
| 180 | - planRedisService.delete(line.getLineCode(), rq); | |
| 181 | - } | |
| 182 | - | |
| 183 | - //加载当天的计划 | |
| 184 | - Date d = new Date(); | |
| 185 | - Date s = new Date(d.getTime() - 1000 * 60 * 60 * 24); | |
| 186 | - List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(s, d); | |
| 187 | - //写入redis | |
| 188 | - planRedisService.wirte(list); | |
| 189 | - } catch (Exception e) { | |
| 190 | - logger.error("", e); | |
| 191 | - } | |
| 192 | - } | |
| 193 | - } | |
| 194 | - | |
| 195 | - private void delete(String lineCode, String rq) { | |
| 196 | - String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", ""); | |
| 197 | - redisTemplate.delete(key); | |
| 198 | - } | |
| 199 | -} | |
| 1 | +package com.bsth.redis; | |
| 2 | + | |
| 3 | +import com.bsth.Application; | |
| 4 | +import com.bsth.entity.SchedulePlanInfo; | |
| 5 | +import com.bsth.redis.util.DateUtils; | |
| 6 | +import com.bsth.redis.util.RedisUtils; | |
| 7 | +import com.bsth.repository.SchedulePlanInfoRepository; | |
| 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.joda.time.DateTime; | |
| 14 | +import org.slf4j.Logger; | |
| 15 | +import org.slf4j.LoggerFactory; | |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 17 | +import org.springframework.boot.CommandLineRunner; | |
| 18 | +import org.springframework.core.annotation.Order; | |
| 19 | +import org.springframework.data.redis.core.ListOperations; | |
| 20 | +import org.springframework.data.redis.core.RedisTemplate; | |
| 21 | +import org.springframework.data.redis.serializer.StringRedisSerializer; | |
| 22 | +import org.springframework.stereotype.Component; | |
| 23 | +import org.springframework.stereotype.Service; | |
| 24 | + | |
| 25 | +import java.util.ArrayList; | |
| 26 | +import java.util.Date; | |
| 27 | +import java.util.Iterator; | |
| 28 | +import java.util.List; | |
| 29 | +import java.util.concurrent.TimeUnit; | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * 计调的 计划排班redis缓存 | |
| 33 | + * Created by panzhao on 2017/3/27. | |
| 34 | + */ | |
| 35 | +@Service | |
| 36 | +@Order(6) | |
| 37 | +public class PlanScheduleRedisService implements CommandLineRunner { | |
| 38 | + | |
| 39 | + @Autowired | |
| 40 | + private RedisTemplate redisTemplate; | |
| 41 | + | |
| 42 | + @Autowired | |
| 43 | + SchedulePlanInfoRepository planInfoRepository; | |
| 44 | + | |
| 45 | + @Autowired | |
| 46 | + RedisUtils redisUtils; | |
| 47 | + | |
| 48 | + static Logger logger = LoggerFactory.getLogger(PlanScheduleRedisService.class); | |
| 49 | + | |
| 50 | + private final static String REDIS_KEY_PREFIX = "plan:"; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * 将一批计划写入redis | |
| 54 | + * | |
| 55 | + * @param list | |
| 56 | + */ | |
| 57 | + public void wirte(List<SchedulePlanInfo> list) { | |
| 58 | + ArrayListMultimap<String, SchedulePlanInfo> multimap; | |
| 59 | + try { | |
| 60 | + if (list.size() == 0) | |
| 61 | + return; | |
| 62 | + //按日期和线路分组数据 | |
| 63 | + Class clazz = SchedulePlanInfo.class; | |
| 64 | + multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("xlBm"), clazz.getDeclaredField("scheduleDate")); | |
| 65 | + | |
| 66 | + //写入redis | |
| 67 | + Iterator<String> iterator = multimap.keySet().iterator(); | |
| 68 | + String key; | |
| 69 | + while (iterator.hasNext()) { | |
| 70 | + key = iterator.next(); | |
| 71 | + replace(key, multimap.get(key)); | |
| 72 | + } | |
| 73 | + } catch (Exception e) { | |
| 74 | + logger.error("", e); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * 将 list 与redis里的数据合并 | |
| 80 | + * | |
| 81 | + * @param key | |
| 82 | + * @param list | |
| 83 | + */ | |
| 84 | + public void mergeData(String key, List<SchedulePlanInfo> list) { | |
| 85 | + key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 86 | + | |
| 87 | + ListOperations<String, SchedulePlanInfo> ops = redisTemplate.opsForList(); | |
| 88 | + List<SchedulePlanInfo> cacheList = ops.range(key, 0, -1); | |
| 89 | + | |
| 90 | + for (SchedulePlanInfo plan : cacheList) { | |
| 91 | + if (!list.contains(plan)) | |
| 92 | + list.add(plan); | |
| 93 | + } | |
| 94 | + | |
| 95 | + //更新 | |
| 96 | + redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * 覆盖数据 | |
| 101 | + * | |
| 102 | + * @param key | |
| 103 | + * @param list | |
| 104 | + */ | |
| 105 | + public void replace(String key, List<SchedulePlanInfo> list) { | |
| 106 | + key = REDIS_KEY_PREFIX + key.replaceAll("-", ""); | |
| 107 | + redisTemplate.execute(redisUtils.getUpdateCallback(key, list)); | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * 根据日期和线路编码从redis获取计划 | |
| 112 | + * | |
| 113 | + * @param dateStr | |
| 114 | + * @param lineCode | |
| 115 | + * @return | |
| 116 | + */ | |
| 117 | + public List<SchedulePlanInfo> read(String dateStr, String lineCode) { | |
| 118 | + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + lineCode + ":" + dateStr, 0, -1); | |
| 119 | + } | |
| 120 | + | |
| 121 | + | |
| 122 | + @Autowired | |
| 123 | + PlanClearThread planClearThread; | |
| 124 | + | |
| 125 | + @Override | |
| 126 | + public void run(String... strings) throws Exception { | |
| 127 | + Application.mainServices.schedule(new Runnable() { | |
| 128 | + @Override | |
| 129 | + public void run() { | |
| 130 | + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 131 | + //设置key 序列化器 | |
| 132 | + redisTemplate.setKeySerializer(new StringRedisSerializer()); | |
| 133 | + | |
| 134 | + DateTime dt = new DateTime(); | |
| 135 | + dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0); | |
| 136 | + List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(dt.minusDays(cacheDays).toDate(), dt.plusDays(1).toDate()); | |
| 137 | + //写入redis | |
| 138 | + wirte(list); | |
| 139 | + } | |
| 140 | + }, 20, TimeUnit.SECONDS); | |
| 141 | + | |
| 142 | + | |
| 143 | + //定时 00:05 分清理计划,并加载当天的计划 | |
| 144 | + long diff = (DateUtils.getTimestamp() + 1000 * 60 * 5) - System.currentTimeMillis(); | |
| 145 | + if (diff < 0) | |
| 146 | + diff += (1000 * 60 * 60 * 24); | |
| 147 | + Application.mainServices.scheduleAtFixedRate(planClearThread, diff / 1000, 60 * 60 * 24, TimeUnit.SECONDS); | |
| 148 | + } | |
| 149 | + | |
| 150 | + public List<SchedulePlanInfo> findByMultiLine(List<String> lineArray, String rq) { | |
| 151 | + rq = rq.replaceAll("-", ""); | |
| 152 | + List<SchedulePlanInfo> rs = new ArrayList<>(); | |
| 153 | + for (String lineCode : lineArray) { | |
| 154 | + rs.addAll(read(rq, lineCode)); | |
| 155 | + } | |
| 156 | + return rs; | |
| 157 | + } | |
| 158 | + | |
| 159 | + @Component | |
| 160 | + public static class PlanClearThread extends Thread { | |
| 161 | + | |
| 162 | + @Autowired | |
| 163 | + PlanScheduleRedisService planRedisService; | |
| 164 | + @Autowired | |
| 165 | + SchedulePlanInfoRepository planInfoRepository; | |
| 166 | + | |
| 167 | + @Override | |
| 168 | + public void run() { | |
| 169 | + try { | |
| 170 | + logger.info("redis -清理计划排班"); | |
| 171 | + | |
| 172 | + int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days")); | |
| 173 | + DateTime dt = new DateTime(); | |
| 174 | + dt = dt.minusDays(cacheDays); | |
| 175 | + String rq = dt.toString("yyyy-MM-dd"); | |
| 176 | + | |
| 177 | + List<Line> lines = LineBufferData.findAll(); | |
| 178 | + for (Line line : lines) { | |
| 179 | + planRedisService.delete(line.getLineCode(), rq); | |
| 180 | + } | |
| 181 | + | |
| 182 | + //加载明天的计划 | |
| 183 | + DateTime d = new DateTime(); | |
| 184 | + d.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0); | |
| 185 | + List<SchedulePlanInfo> list = planInfoRepository.findByDateRange(d.toDate(), d.plusDays(1).toDate()); | |
| 186 | + //写入redis | |
| 187 | + planRedisService.wirte(list); | |
| 188 | + } catch (Exception e) { | |
| 189 | + logger.error("", e); | |
| 190 | + } | |
| 191 | + } | |
| 192 | + } | |
| 193 | + | |
| 194 | + private void delete(String lineCode, String rq) { | |
| 195 | + String key = REDIS_KEY_PREFIX + (lineCode + ":" + rq).replaceAll("-", ""); | |
| 196 | + redisTemplate.delete(key); | |
| 197 | + } | |
| 198 | +} | ... | ... |