Commit 4b443325b2a73d519ad5f6983c07c58d4206e102

Authored by 王通
1 parent 97bd87ce

1.

src/main/java/com/bsth/redis/HRedisService.java
1   -package com.bsth.redis;
2   -
3   -import com.bsth.Application;
4   -import com.bsth.entity.HInfo;
5   -import com.bsth.redis.util.RedisUtils;
6   -import com.bsth.repository.HInfoRepository;
7   -import com.bsth.util.ConfigUtil;
8   -import com.bsth.util.ConvertUtil;
9   -import com.google.common.collect.ArrayListMultimap;
10   -import org.joda.time.DateTime;
11   -import org.slf4j.Logger;
12   -import org.slf4j.LoggerFactory;
13   -import org.springframework.beans.factory.annotation.Autowired;
14   -import org.springframework.boot.CommandLineRunner;
15   -import org.springframework.core.annotation.Order;
16   -import org.springframework.data.redis.core.RedisTemplate;
17   -import org.springframework.data.redis.serializer.StringRedisSerializer;
18   -import org.springframework.stereotype.Component;
19   -import org.springframework.stereotype.Service;
20   -
21   -import java.util.*;
22   -import java.util.concurrent.TimeUnit;
23   -
24   -/**
25   - * 油量数据Redis缓存
26   - * Created by panzhao on 2017/3/19.
27   - */
28   -@Service
29   -@Order(3)
30   -public class HRedisService implements CommandLineRunner {
31   -
32   - @Autowired
33   - private RedisTemplate redisTemplate;
34   -
35   - @Autowired
36   - HInfoRepository hInfoRepository;
37   -
38   - @Autowired
39   - RedisUtils redisUtils;
40   -
41   - static Logger logger = LoggerFactory.getLogger(HRedisService.class);
42   -
43   - private final static String REDIS_KEY_PREFIX = "hydrogen:";
44   -
45   - /**
46   - * 将油量数据写入redis
47   - *
48   - * @param list
49   - */
50   - public void wirte(List<HInfo> list) {
51   - ArrayListMultimap<String, HInfo> multimap;
52   - try {
53   - if (list.size() == 0)
54   - return;
55   - //按日期和线路分组数据
56   - Class clazz = HInfo.class;
57   - multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq"));
58   -
59   - //写入redis
60   - Iterator<String> iterator = multimap.keySet().iterator();
61   - String key;
62   - while (iterator.hasNext()) {
63   - key = iterator.next();
64   - mergeData(key, multimap.get(key));
65   - }
66   - } catch (Exception e) {
67   - logger.error("", e);
68   - }
69   - }
70   -
71   - /**
72   - * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key
73   - *
74   - * @param nbbmArray
75   - * @param rq
76   - * @return
77   - */
78   - public ArrayListMultimap findByNbbmGroup(Iterable<String> nbbmArray, String rq) {
79   - ArrayListMultimap rs = ArrayListMultimap.create();
80   -
81   - rq = rq.replaceAll("-", "");
82   - try {
83   - List<HInfo> list = new ArrayList<>();
84   - for (String nbbm : nbbmArray) {
85   - nbbm = nbbm.split("_")[1];
86   - list.addAll(read(nbbm, rq));
87   - }
88   - Class clazz = HInfo.class;
89   - rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy"));
90   - } catch (Exception e) {
91   - logger.error("", e);
92   - }
93   - return rs;
94   - }
95   -
96   - /**
97   - * 根据车辆和日期获取油耗数据,以 车辆 为key
98   - *
99   - * @param nbbmArray
100   - * @param rq
101   - * @return
102   - */
103   - public ArrayListMultimap findByNbbmGroup1(Iterable<String> nbbmArray, String rq) {
104   - ArrayListMultimap rs = ArrayListMultimap.create();
105   -
106   - rq = rq.replaceAll("-", "");
107   - try {
108   - List<HInfo> list = new ArrayList<>();
109   - Set<String> nbbms = new HashSet<>();
110   - for (String nbbm : nbbmArray) {
111   - if (!nbbms.contains(nbbm)) {
112   - nbbms.add(nbbm);
113   - list.addAll(read(nbbm, rq));
114   - }
115   - }
116   - Class clazz = HInfo.class;
117   - rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"));
118   - } catch (Exception e) {
119   - logger.error("", e);
120   - }
121   - return rs;
122   - }
123   -
124   - public List<HInfo> read(String nbbm, String rq) {
125   - return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1);
126   - }
127   -
128   - /**
129   - * 将 list 与redis里的数据合并
130   - *
131   - * @param key
132   - * @param list
133   - */
134   - public void mergeData(String key, List<HInfo> list) {
135   - key = REDIS_KEY_PREFIX + key;
136   -
137   - //更新 直接覆盖更新
138   - redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
139   - }
140   -
141   - @Autowired
142   - HRefreshThread hRefreshThread;
143   -
144   - @Override
145   - public void run(String... strings) throws Exception {
146   - Application.mainServices.schedule(new Runnable() {
147   - @Override
148   - public void run() {
149   - //启动加载油耗缓存
150   - synchData(null);
151   - }
152   - }, 30, TimeUnit.SECONDS);
153   -
154   -
155   - //定时刷新油耗信息
156   - Application.mainServices.scheduleWithFixedDelay(hRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS);
157   - }
158   -
159   - /**
160   - * 和数据库同步数据
161   - */
162   - public void synchData(Integer days) {
163   - int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
164   - if (null != days && days < cacheDays)
165   - cacheDays = days;
166   - //设置key 序列化器
167   - redisTemplate.setKeySerializer(new StringRedisSerializer());
168   -
169   - DateTime dt = new DateTime();
170   - dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
171   - List<HInfo> list = hInfoRepository.findByDateLT(dt.toDate());
172   - //写入redis
173   - wirte(list);
174   - logger.info("刷新氢耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd"));
175   - }
176   -
177   - @Component
178   - public static class HRefreshThread extends Thread {
179   -
180   - @Autowired
181   - HRedisService hRedisService;
182   -
183   - @Override
184   - public void run() {
185   - try {
186   - hRedisService.synchData(5);
187   - } catch (Exception e) {
188   - logger.error("", e);
189   - }
190   - }
191   - }
192   -}
  1 +package com.bsth.redis;
  2 +
  3 +import com.bsth.Application;
  4 +import com.bsth.entity.HInfo;
  5 +import com.bsth.redis.util.RedisUtils;
  6 +import com.bsth.repository.HInfoRepository;
  7 +import com.bsth.util.AppProperties;
  8 +import com.bsth.util.ConvertUtil;
  9 +import com.google.common.collect.ArrayListMultimap;
  10 +import org.joda.time.DateTime;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.boot.CommandLineRunner;
  15 +import org.springframework.core.annotation.Order;
  16 +import org.springframework.data.redis.core.RedisTemplate;
  17 +import org.springframework.data.redis.serializer.StringRedisSerializer;
  18 +import org.springframework.stereotype.Component;
  19 +import org.springframework.stereotype.Service;
  20 +
  21 +import java.util.*;
  22 +import java.util.concurrent.TimeUnit;
  23 +
  24 +/**
  25 + * 油量数据Redis缓存
  26 + * Created by panzhao on 2017/3/19.
  27 + */
  28 +@Service
  29 +@Order(3)
  30 +public class HRedisService implements CommandLineRunner {
  31 +
  32 + @Autowired
  33 + private RedisTemplate redisTemplate;
  34 +
  35 + @Autowired
  36 + HInfoRepository hInfoRepository;
  37 +
  38 + @Autowired
  39 + RedisUtils redisUtils;
  40 +
  41 + static Logger logger = LoggerFactory.getLogger(HRedisService.class);
  42 +
  43 + private final static String REDIS_KEY_PREFIX = "hydrogen:";
  44 +
  45 + /**
  46 + * 将油量数据写入redis
  47 + *
  48 + * @param list
  49 + */
  50 + public void wirte(List<HInfo> list) {
  51 + ArrayListMultimap<String, HInfo> multimap;
  52 + try {
  53 + if (list.size() == 0)
  54 + return;
  55 + //按日期和线路分组数据
  56 + Class clazz = HInfo.class;
  57 + multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq"));
  58 +
  59 + //写入redis
  60 + Iterator<String> iterator = multimap.keySet().iterator();
  61 + String key;
  62 + while (iterator.hasNext()) {
  63 + key = iterator.next();
  64 + mergeData(key, multimap.get(key));
  65 + }
  66 + } catch (Exception e) {
  67 + logger.error("", e);
  68 + }
  69 + }
  70 +
  71 + /**
  72 + * 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key
  73 + *
  74 + * @param nbbmArray
  75 + * @param rq
  76 + * @return
  77 + */
  78 + public ArrayListMultimap findByNbbmGroup(Iterable<String> nbbmArray, String rq) {
  79 + ArrayListMultimap rs = ArrayListMultimap.create();
  80 +
  81 + rq = rq.replaceAll("-", "");
  82 + try {
  83 + List<HInfo> list = new ArrayList<>();
  84 + for (String nbbm : nbbmArray) {
  85 + nbbm = nbbm.split("_")[1];
  86 + list.addAll(read(nbbm, rq));
  87 + }
  88 + Class clazz = HInfo.class;
  89 + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy"));
  90 + } catch (Exception e) {
  91 + logger.error("", e);
  92 + }
  93 + return rs;
  94 + }
  95 +
  96 + /**
  97 + * 根据车辆和日期获取油耗数据,以 车辆 为key
  98 + *
  99 + * @param nbbmArray
  100 + * @param rq
  101 + * @return
  102 + */
  103 + public ArrayListMultimap findByNbbmGroup1(Iterable<String> nbbmArray, String rq) {
  104 + ArrayListMultimap rs = ArrayListMultimap.create();
  105 +
  106 + rq = rq.replaceAll("-", "");
  107 + try {
  108 + List<HInfo> list = new ArrayList<>();
  109 + Set<String> nbbms = new HashSet<>();
  110 + for (String nbbm : nbbmArray) {
  111 + if (!nbbms.contains(nbbm)) {
  112 + nbbms.add(nbbm);
  113 + list.addAll(read(nbbm, rq));
  114 + }
  115 + }
  116 + Class clazz = HInfo.class;
  117 + rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"));
  118 + } catch (Exception e) {
  119 + logger.error("", e);
  120 + }
  121 + return rs;
  122 + }
  123 +
  124 + public List<HInfo> read(String nbbm, String rq) {
  125 + return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1);
  126 + }
  127 +
  128 + /**
  129 + * 将 list 与redis里的数据合并
  130 + *
  131 + * @param key
  132 + * @param list
  133 + */
  134 + public void mergeData(String key, List<HInfo> list) {
  135 + key = REDIS_KEY_PREFIX + key;
  136 +
  137 + //更新 直接覆盖更新
  138 + redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
  139 + }
  140 +
  141 + @Autowired
  142 + HRefreshThread hRefreshThread;
  143 +
  144 + @Override
  145 + public void run(String... strings) throws Exception {
  146 + Application.mainServices.schedule(new Runnable() {
  147 + @Override
  148 + public void run() {
  149 + //启动加载油耗缓存
  150 + synchData(null);
  151 + }
  152 + }, 30, TimeUnit.SECONDS);
  153 +
  154 +
  155 + //定时刷新油耗信息
  156 + Application.mainServices.scheduleWithFixedDelay(hRefreshThread, 60 * 40, 60 * 40, TimeUnit.SECONDS);
  157 + }
  158 +
  159 + /**
  160 + * 和数据库同步数据
  161 + */
  162 + public void synchData(Integer days) {
  163 + int cacheDays = AppProperties.getCacheDays();
  164 + if (null != days && days < cacheDays)
  165 + cacheDays = days;
  166 + //设置key 序列化器
  167 + redisTemplate.setKeySerializer(new StringRedisSerializer());
  168 +
  169 + DateTime dt = new DateTime();
  170 + dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
  171 + List<HInfo> list = hInfoRepository.findByDateLT(dt.toDate());
  172 + //写入redis
  173 + wirte(list);
  174 + logger.info("刷新氢耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd"));
  175 + }
  176 +
  177 + @Component
  178 + public static class HRefreshThread extends Thread {
  179 +
  180 + @Autowired
  181 + HRedisService hRedisService;
  182 +
  183 + @Override
  184 + public void run() {
  185 + try {
  186 + hRedisService.synchData(5);
  187 + } catch (Exception e) {
  188 + logger.error("", e);
  189 + }
  190 + }
  191 + }
  192 +}
... ...
src/main/java/com/bsth/repository/JdlReceptionRepository.java
... ... @@ -11,7 +11,7 @@ import com.bsth.entity.JdlReception;
11 11 @Repository
12 12 public interface JdlReceptionRepository extends PagingAndSortingRepository<JdlReception, Integer>{
13 13  
14   - @Query("SELECT inside_code from bsth_c_cars where REPLACE(car_plate, '-', '') = ?1 and scrap_state = 0 order by create_date desc ")
  14 + @Query(value = "SELECT inside_code from bsth_c_cars where REPLACE(car_plate, '-', '') = ?1 and scrap_state = 0 order by create_date desc ", nativeQuery = true)
15 15 List<Object[]> selectNbbmByPlate(String carPlate);
16 16  
17 17 }
... ...