|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+package com.smartSteward.common.utils;
|
|
|
2
|
+
|
|
|
3
|
+import com.smartSteward.common.core.redis.RedisCache;
|
|
|
4
|
+import com.smartSteward.common.utils.spring.SpringUtils;
|
|
|
5
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
6
|
+
|
|
|
7
|
+import java.util.Arrays;
|
|
|
8
|
+import java.util.Map;
|
|
|
9
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
10
|
+import java.util.stream.Collectors;
|
|
|
11
|
+
|
|
|
12
|
+/**
|
|
|
13
|
+ * 站点工具类
|
|
|
14
|
+ * @author smart_Steward
|
|
|
15
|
+ */
|
|
|
16
|
+@Slf4j
|
|
|
17
|
+public class StationUtil {
|
|
|
18
|
+ // 使用线程安全的缓存
|
|
|
19
|
+ private static final Map<String, String> STATION_CACHE = new ConcurrentHashMap<>();
|
|
|
20
|
+ private static final String EMPTY_STRING = "";
|
|
|
21
|
+ private static final String STATION_MAP_KEY = "stationMap";
|
|
|
22
|
+
|
|
|
23
|
+ private static final String DEFAULT_SEPARATOR = ",";
|
|
|
24
|
+ /**
|
|
|
25
|
+ * 获取单个站点名称
|
|
|
26
|
+ */
|
|
|
27
|
+ public static String getStationNameSafely(String stationCode) {
|
|
|
28
|
+ if (stationCode == null || stationCode.isEmpty()) {
|
|
|
29
|
+ return EMPTY_STRING;
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ // 优先从缓存获取
|
|
|
33
|
+ if (!STATION_CACHE.isEmpty()) {
|
|
|
34
|
+ return STATION_CACHE.getOrDefault(stationCode, EMPTY_STRING);
|
|
|
35
|
+ }
|
|
|
36
|
+
|
|
|
37
|
+ // 缓存为空时从Redis加载
|
|
|
38
|
+ try {
|
|
|
39
|
+ RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
|
|
|
40
|
+ if (redisCache.hasKey(STATION_MAP_KEY)) {
|
|
|
41
|
+ Map<String, String> redisStationMap = redisCache.getCacheMap(STATION_MAP_KEY);
|
|
|
42
|
+ STATION_CACHE.putAll(redisStationMap);
|
|
|
43
|
+ return STATION_CACHE.getOrDefault(stationCode, EMPTY_STRING);
|
|
|
44
|
+ }
|
|
|
45
|
+ } catch (Exception e) {
|
|
|
46
|
+ log.warn("Failed to load station map from Redis for code: {}", stationCode, e);
|
|
|
47
|
+ return EMPTY_STRING;
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ return EMPTY_STRING;
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ /**
|
|
|
54
|
+ * 获取多个站点名称,中间逗号分割
|
|
|
55
|
+ */
|
|
|
56
|
+ public static String getStationNamesSafely(String stationCodes) {
|
|
|
57
|
+ if (stationCodes == null || stationCodes.isEmpty()) {
|
|
|
58
|
+ return EMPTY_STRING;
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ // 优先从缓存获取
|
|
|
62
|
+ if (!STATION_CACHE.isEmpty()) {
|
|
|
63
|
+ return Arrays.stream(stationCodes.split(DEFAULT_SEPARATOR))
|
|
|
64
|
+ .map(code -> STATION_CACHE.getOrDefault(code, EMPTY_STRING))
|
|
|
65
|
+ .collect(Collectors.joining(DEFAULT_SEPARATOR));
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ // 缓存为空时从 Redis 加载
|
|
|
69
|
+ try {
|
|
|
70
|
+ RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
|
|
|
71
|
+ if (redisCache.hasKey(STATION_MAP_KEY)) {
|
|
|
72
|
+ Map<String, String> redisStationMap = redisCache.getCacheMap(STATION_MAP_KEY);
|
|
|
73
|
+ STATION_CACHE.putAll(redisStationMap);
|
|
|
74
|
+ return Arrays.stream(stationCodes.split(DEFAULT_SEPARATOR))
|
|
|
75
|
+ .map(code -> STATION_CACHE.getOrDefault(code, EMPTY_STRING))
|
|
|
76
|
+ .collect(Collectors.joining(DEFAULT_SEPARATOR));
|
|
|
77
|
+ }
|
|
|
78
|
+ } catch (Exception e) {
|
|
|
79
|
+ log.warn("Failed to load station map from Redis for codes: {}", stationCodes, e);
|
|
|
80
|
+ return EMPTY_STRING;
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ return EMPTY_STRING;
|
|
|
84
|
+ }
|
|
|
85
|
+}
|