BasicCacheData.java
2.93 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
package com.bsth.data;
import com.bsth.entity.Line;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 基础数据缓存 by panzhao
*/
@Component
public class BasicCacheData {
/**
* K: lineCode
*/
public static Map<String, Line> code2LineMap;
public static BiMap<String, String> device2nbbmMap;
public static Map<String, String> device2plateMap;
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
JdbcTemplate jdbcTemplate;
public void reLoad() {
loadCode2LineMap();
loadDevice2nbbmMap();
}
private void loadDevice2nbbmMap() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select INSIDE_CODE,EQUIPMENT_CODE,CAR_PLATE from bsth_c_cars where equipment_code is not null");
BiMap<String, String> biMap = HashBiMap.create();
Map<String, String> pateMap = new HashMap<>();
for (Map<String, Object> map : list) {
try {
biMap.put(map.get("EQUIPMENT_CODE").toString(), map.get("INSIDE_CODE").toString());
pateMap.put(map.get("EQUIPMENT_CODE").toString(), map.get("CAR_PLATE").toString());
} catch (Exception e) {
logger.error("", e);
}
}
device2nbbmMap = biMap;
device2plateMap = pateMap;
}
private void loadCode2LineMap() {
String sql = "select line_code,`name`,start_station_name,end_station_name,start_station_first_time,start_station_end_time,end_station_first_time,end_station_end_time from bsth_c_line WHERE destroy=0 and `remove`!=1";
List<Line> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Line.class));
Map<String, Line> map = new HashMap<>();
for (Line line : list) {
try {
line.setFullChars(PinyinHelper.convertToPinyinString(line.getName(), "", PinyinFormat.WITHOUT_TONE));
line.setCamelChars(PinyinHelper.getShortPinyin(line.getName()));
} catch (PinyinException e) {
logger.error("", e);
}
map.put(line.getLineCode(), line);
}
if (map.size() > 0)
code2LineMap = map;
}
@Component
public class RefreshDataThead extends Thread {
@Override
public void run() {
reLoad();
}
}
}