ElecRedisService.java
4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.bsth.redis;
import com.bsth.entity.ElecInfo;
import com.bsth.redis.util.RedisUtils;
import com.bsth.repository.ElecInfoRepository;
import com.bsth.util.ConfigUtil;
import com.bsth.util.ConvertUtil;
import com.google.common.collect.ArrayListMultimap;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 油量数据Redis缓存
* Created by panzhao on 2017/3/19.
*/
@Service
@Order(3)
public class ElecRedisService implements CommandLineRunner {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
ElecInfoRepository elecInfoRepository;
@Autowired
RedisUtils redisUtils;
static Logger logger = LoggerFactory.getLogger(ElecRedisService.class);
private final static String REDIS_KEY_PREFIX = "elec:";
/**
* 将油量数据写入redis
*
* @param list
*/
public void wirte(List<ElecInfo> list) {
ArrayListMultimap<String, ElecInfo> multimap;
try {
if (list.size() == 0)
return;
//按日期和线路分组数据
Class clazz = ElecInfo.class;
multimap = new ConvertUtil().groupMultiList(list, ":", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("rq"));
//写入redis
Iterator<String> iterator = multimap.keySet().iterator();
String key;
while (iterator.hasNext()) {
key = iterator.next();
mergeData(key, multimap.get(key));
}
} catch (Exception e) {
logger.error("", e);
}
}
/**
* 根据车辆和日期获取油耗数据,以 车辆_驾驶员 为key
*
* @param nbbmArray
* @param rq
* @return
*/
public ArrayListMultimap findByNbbmGroup(Iterable<String> nbbmArray, String rq) {
ArrayListMultimap rs = ArrayListMultimap.create();
rq = rq.replaceAll("-", "");
try {
List<ElecInfo> list = new ArrayList<>();
for (String nbbm : nbbmArray) {
nbbm = nbbm.split("_")[1];
list.addAll(read(nbbm, rq));
}
Class clazz = ElecInfo.class;
rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"), clazz.getDeclaredField("jsy"));
} catch (Exception e) {
logger.error("", e);
}
return rs;
}
/**
* 根据车辆和日期获取油耗数据,以 车辆 为key
*
* @param nbbmArray
* @param rq
* @return
*/
public ArrayListMultimap findByNbbmGroup1(Iterable<String> nbbmArray, String rq) {
ArrayListMultimap rs = ArrayListMultimap.create();
DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(rq);
try {
List<ElecInfo> list = elecInfoRepository.findByDate(dateTime.toDate());
Class clazz = ElecInfo.class;
rs = new ConvertUtil().groupMultiList(list, "_", clazz.getDeclaredField("nbbm"));
} catch (Exception e) {
logger.error("", e);
}
return rs;
}
public List<ElecInfo> read(String nbbm, String rq) {
return redisTemplate.opsForList().range(REDIS_KEY_PREFIX + nbbm + ":" + rq, 0, -1);
}
/**
* 将 list 与redis里的数据合并
*
* @param key
* @param list
*/
public void mergeData(String key, List<ElecInfo> list) {
key = REDIS_KEY_PREFIX + key;
//更新 直接覆盖更新
redisTemplate.execute(redisUtils.getUpdateCallback(key, list));
}
@Override
public void run(String... strings) throws Exception {
}
/**
* 和数据库同步数据
*/
public void synchData(Integer days) {
int cacheDays = Integer.parseInt(ConfigUtil.get("cache.days"));
if (null != days && days < cacheDays)
cacheDays = days;
//设置key 序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
DateTime dt = new DateTime();
dt = dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).minusDays(cacheDays);
List<ElecInfo> list = elecInfoRepository.findByDateLT(dt.toDate());
//写入redis
wirte(list);
logger.info("刷新电耗数据, days: " + cacheDays + " -size: " + list.size() + " -LT: " + dt.toString("yyyy-MM-dd"));
}
}