瀏覽代碼

Merge branch 'master' into dev

skyline 1 年之前
父節點
當前提交
7b635d9962

+ 47 - 27
common/src/main/java/com/kym/common/cache/PlatformCache.java

@@ -1,10 +1,16 @@
 package com.kym.common.cache;
 
+import cn.hutool.extra.spring.SpringUtil;
+import com.alibaba.fastjson2.JSONObject;
 import com.kym.common.utils.PlatformConvertUtil;
 import com.kym.entity.admin.Platform;
+import com.kym.entity.common.RedisKeys;
+import org.springframework.boot.context.event.ApplicationStartedEvent;
+import org.springframework.context.ApplicationListener;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
 
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * 平台配置缓存
@@ -14,20 +20,15 @@ import java.util.concurrent.ConcurrentHashMap;
 public enum PlatformCache {
     INSTANCE;
 
-    /**
-     * 充电口ID-平台映射
-     */
-    private static ConcurrentHashMap<String, String> CONNECTOR_ID_PLATFORM_NAME_MAPPING = new ConcurrentHashMap<>();
 
     /**
-     * 站点ID-平台映射
-     */
-    private static ConcurrentHashMap<String, String> STATION_ID_PLATFORM_NAME_MAPPING = new ConcurrentHashMap<>();
-
-    /**
-     * 平台名称-平台配置映射
+     * 保存站点id-平台映射
+     *
+     * @param map
      */
-    private static ConcurrentHashMap<String, Platform> PLATFORM_CONFIG_MAPPING = new ConcurrentHashMap<>();
+    public void putStationId2PlatformName(Map<String, String> map) {
+        map.forEach((k, v) -> PlatformCacheInjector.redisTemplate.opsForValue().set(RedisKeys.STATION_ID_PLATFORM_NAME + k, v));
+    }
 
     /**
      * 站点ID获取平台名称
@@ -36,37 +37,46 @@ public enum PlatformCache {
      * @return
      */
     public String getPlatformNameByStationId(String stationId) {
-        return STATION_ID_PLATFORM_NAME_MAPPING.get(stationId);
+        return PlatformCacheInjector.redisTemplate.opsForValue().get(RedisKeys.STATION_ID_PLATFORM_NAME + stationId);
     }
 
-    public void putConnectorId2PlatformName(Map<String, String> data) {
-        CONNECTOR_ID_PLATFORM_NAME_MAPPING.putAll(data);
+    /**
+     * 枪号-平台名称映射
+     *
+     * @param map
+     */
+    public void putConnectorId2PlatformName(Map<String, String> map) {
+        map.forEach((k, v) -> PlatformCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_PLATFORM_NAME + k, v));
     }
 
     /**
-     * 保存站点id-平台映射
+     * 根据枪号获取平台名称
      *
-     * @param data
+     * @param connectorId
+     * @return
      */
-    public void putStationId2PlatformName(Map<String, String> data) {
-        STATION_ID_PLATFORM_NAME_MAPPING.putAll(data);
+    public String getPlatformNameByConnectorId(String connectorId) {
+        connectorId = PlatformConvertUtil.parse2LocalConnectorId(connectorId);
+        return PlatformCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_PLATFORM_NAME + connectorId);
     }
 
-    public void putPlatformName2Config(Map<String, Platform> data) {
-        PLATFORM_CONFIG_MAPPING.putAll(data);
-    }
 
-    public String getPlatformNameByConnectorId(String connectorId) {
-        connectorId = PlatformConvertUtil.parse2LocalConnectorId(connectorId);
-        return CONNECTOR_ID_PLATFORM_NAME_MAPPING.get(connectorId);
+    /**
+     * 保存平台名称-配置
+     *
+     * @param map
+     */
+    public void putPlatformName2Config(Map<String, Platform> map) {
+        map.forEach((k, v) -> PlatformCacheInjector.redisTemplate.opsForValue().set(RedisKeys.PLATFORM_NAME_CONFIG + k, JSONObject.toJSONString(v)));
     }
 
+
     public Platform getPlatformByName(String... platformName) {
         var name = "EN_PLUS";
         if (platformName.length > 0 && platformName[0] != null) {
             name = platformName[0];
         }
-        return PLATFORM_CONFIG_MAPPING.get(name);
+        return JSONObject.parseObject(PlatformCacheInjector.redisTemplate.opsForValue().get(RedisKeys.PLATFORM_NAME_CONFIG + name), Platform.class);
     }
 
     /**
@@ -78,7 +88,17 @@ public enum PlatformCache {
     public Platform getPlatformByConnectorId(String connectorId) {
         connectorId = PlatformConvertUtil.parse2LocalConnectorId(connectorId);
         var platformName = getPlatformNameByConnectorId(connectorId);
-        return PLATFORM_CONFIG_MAPPING.get(platformName);
+        return getPlatformByName(platformName);
+    }
+
+    @Component
+    public static class PlatformCacheInjector implements ApplicationListener<ApplicationStartedEvent> {
+        private static final StringRedisTemplate redisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
+
+        @Override
+        public void onApplicationEvent(ApplicationStartedEvent event) {
+
+        }
     }
 
 }

+ 3 - 0
entity/src/main/java/com/kym/entity/common/RedisKeys.java

@@ -23,4 +23,7 @@ public interface RedisKeys {
     String ADMIN_USER_STATION_IDS = "ADMIN_USER_STATION_IDS:";
     String COUPON_ID_TO_USERS = "COUPON_ID_TO_USER_ID:";
     String CHARGE_ORDER_EQUIP_CHARGE_STATUS = "CHARGE_ORDER_EQUIP_CHARGE_STATUS:";
+    String CONNECTOR_ID_PLATFORM_NAME = "CONNECTOR_ID_PLATFORM_NAME:";
+    String STATION_ID_PLATFORM_NAME = "STATION_ID_PLATFORM_NAME:";
+    String PLATFORM_NAME_CONFIG = "PLATFORM_NAME_CONFIG:";
 }

+ 5 - 0
entity/src/main/java/com/kym/entity/platform/response/PlatformBusinessPolicy.java

@@ -57,6 +57,11 @@ public class PlatformBusinessPolicy {
 
     @JSONCreator
     PlatformBusinessPolicy(@JSONField(name = "PolicyInfos") JSONArray array) {
+        // 如果策略信息为空,不做转换(朗新新站点切换会出现此问题)
+        if (array == null || array.isEmpty()) {
+            policyInfos = null;
+            return;
+        }
         this.policyInfos = array.toJavaList(PlatformPolicyInfo.class);
         // 将尖峰平谷信息填入EnPolicyInfo中
         var elecPriceSet = policyInfos.stream().map(PlatformPolicyInfo::getElecPrice).collect(Collectors.toSet()).stream().sorted().toList();