浏览代码

优化系统编码

李进 1 年之前
父节点
当前提交
e0e3c83732

+ 32 - 38
sys-api-service/src/main/java/com/xy/service/SysCodeConfigureServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.xy.annotate.Runners;
 import com.xy.collections.list.JArrayList;
 import com.xy.collections.list.JList;
+import com.xy.collections.map.JMap;
 import com.xy.dto.SysCodeConfigureDto;
 import com.xy.entity.SysCodeConfigure;
 import com.xy.entity.SysCodeConfigureRedis;
@@ -84,7 +85,7 @@ public class SysCodeConfigureServiceImpl extends ServiceImpl<SysCodeConfigureMap
         SysCodeConfigure saveInfo = copy(SysCodeConfigure.class, save)
                 .createUserTime(AuthorizeUtils.getLoginId(Long.class));
         save(saveInfo);
-        refuRedis(saveInfo);
+        refuRedis(saveInfo, 1);
         return R.ok();
     }
 
@@ -94,7 +95,7 @@ public class SysCodeConfigureServiceImpl extends ServiceImpl<SysCodeConfigureMap
         SysCodeConfigure updateInfo = copy(SysCodeConfigure.class, update)
                 .updateUserTime(AuthorizeUtils.getLoginId(Long.class));
         updateById(updateInfo);
-        refuRedis(updateInfo);
+        refuRedis(updateInfo, 1);
         return R.ok();
     }
 
@@ -105,21 +106,28 @@ public class SysCodeConfigureServiceImpl extends ServiceImpl<SysCodeConfigureMap
         //删除编码
         List<SysCodeConfigure> list = list(new LambdaQueryWrapper<SysCodeConfigure>().in(SysCodeConfigure::getCode, del.getCode()));
         removeBatchByIds(del.getCode());
-        //删除子编码
-        JList<SysCodeConfigure> paters = new JArrayList<>(list).filter().isNull(SysCodeConfigure::getPaterCode).list();
-        JList<SysCodeConfigure> paters2 = new JArrayList<>(list).filter().eq(SysCodeConfigure::getPaterCode, "").list();
-        if (Emptys.check(paters2)) {
-            paters.addAll(paters2);
-        }
-        if (Emptys.check(paters)) {
-            JList<String> codes = paters.getProperty(SysCodeConfigure::getCode);
-            remove(new LambdaQueryWrapper<SysCodeConfigure>().in(SysCodeConfigure::getPaterCode, codes));
-        }
+        //递归删除子编码
+        delSon(new JArrayList<>(list).getProperty(SysCodeConfigure::getCode));
         //刷新redis
-        list.forEach(sysDict -> refuRedis(sysDict));
+        list.forEach(sysCodeConfigure -> refuRedis(sysCodeConfigure, 2));
         return R.ok();
     }
 
+    /**
+     * 递归删除子编码
+     *
+     * @param codes
+     */
+    private void delSon(List<String> codes) {
+        List<SysCodeConfigure> list = list(new LambdaQueryWrapper<SysCodeConfigure>().in(SysCodeConfigure::getPaterCode, codes));
+        if (!Emptys.check(list)) {
+            return;
+        }
+        delSon(new JArrayList<>(list).getProperty(SysCodeConfigure::getCode));
+        removeBatchByIds(list);
+        list.forEach(sysCodeConfigure -> refuRedis(sysCodeConfigure, 2));
+    }
+
     @ApiOperation("编码树")
     @PostMapping("tree")
     public R<List<SysCodeConfigureDto.TreeVo>> tree(@RequestBody SysCodeConfigureDto.TreeDto treeDto) {
@@ -146,20 +154,12 @@ public class SysCodeConfigureServiceImpl extends ServiceImpl<SysCodeConfigureMap
      *
      * @param sysCodeConfigure
      */
-    public void refuRedis(SysCodeConfigure sysCodeConfigure) {
-        List<SysCodeConfigure> list;
-        String key = sysCodeConfigure.getPaterCode();
-        if (!Emptys.check(key)) {
-            //父字典
-            list = list(new LambdaQueryWrapper<SysCodeConfigure>().eq(SysCodeConfigure::getPaterCode, sysCodeConfigure.getCode()));
-            key = sysCodeConfigure.getCode();
+    public void refuRedis(SysCodeConfigure sysCodeConfigure, int work) {
+        SysCodeConfigureRedis sysCodeConfigureRedis = copy(SysCodeConfigureRedis.class, sysCodeConfigure);
+        if (work == 1) {
+            redisService.setMap(SysCodeConfigureUtils.SYS_CODE_CONFIGURE_PREFIX, sysCodeConfigureRedis.getCode(), sysCodeConfigureRedis);
         } else {
-            //子字典
-            list = list(new LambdaQueryWrapper<SysCodeConfigure>().eq(SysCodeConfigure::getPaterCode, key));
-        }
-        redisService.removeMap(SysCodeConfigureUtils.getKey(key));
-        for (SysCodeConfigure sysCodeConfigureInfo : list) {
-            redisService.setMap(SysCodeConfigureUtils.getKey(key), sysCodeConfigureInfo.getCode(), copy(SysCodeConfigureRedis.class, sysCodeConfigureInfo));
+            redisService.removeMap(SysCodeConfigureUtils.SYS_CODE_CONFIGURE_PREFIX, sysCodeConfigureRedis.getCode());
         }
         log.info("系统编码刷新redis完成~");
     }
@@ -170,19 +170,13 @@ public class SysCodeConfigureServiceImpl extends ServiceImpl<SysCodeConfigureMap
     @Runners
     public void loadRedis() {
         List<SysCodeConfigure> sysCodeConfigures = list();
-        List<SysCodeConfigureRedis> sysCodeConfigureRedis = Beans.copy(SysCodeConfigureRedis.class, sysCodeConfigures);
-        JList<SysCodeConfigureRedis> list = new JArrayList<>(sysCodeConfigureRedis);
-        JList<SysCodeConfigureRedis> paters = list.filter().isNull(SysCodeConfigureRedis::getPaterCode).list();
-        JList<SysCodeConfigureRedis> paters2 = list.filter().eq(SysCodeConfigureRedis::getPaterCode, "").list();
-        if (Emptys.check(paters2)) {
-            paters.addAll(paters2);
+        if (!Emptys.check(sysCodeConfigures)) {
+            return;
         }
-        redisService.removeLikeMap(SysCodeConfigureUtils.SYS_CODE_CONFIGURE_PREFIX);
-        //[父code -> 子列表]数据结构写入redis
-        paters.forEach(sysDict -> {
-            JList<SysCodeConfigureRedis> sysCodeConfigureRedisJList = list.filter().eq(SysCodeConfigureRedis::getPaterCode, sysDict.getCode()).list();
-            sysCodeConfigureRedisJList.forEach(sysCodeConfigureRediss -> redisService.setMap(SysCodeConfigureUtils.getKey(sysDict.getCode()), sysCodeConfigureRediss.getCode(), sysCodeConfigureRediss));
-        });
+        List<SysCodeConfigureRedis> sysCodeConfigureRedis = Beans.copy(SysCodeConfigureRedis.class, sysCodeConfigures);
+        JMap<String, SysCodeConfigureRedis> sysCodeConfiguresJMaps = new JArrayList<>(sysCodeConfigureRedis).toMap(SysCodeConfigureRedis::getCode).cover();
+        redisService.remove(SysCodeConfigureUtils.SYS_CODE_CONFIGURE_PREFIX);
+        redisService.setMap(SysCodeConfigureUtils.SYS_CODE_CONFIGURE_PREFIX, sysCodeConfiguresJMaps);
         log.info("系统编码写入redis完成~");
     }
 }

+ 1 - 2
sys-sdk/src/main/java/com/xy/utils/LogUtils.java

@@ -75,8 +75,7 @@ public class LogUtils {
      * @param eventDescript 事件描述
      */
     public static void events(String code, String eventName, String eventDescript) {
-        String paterCode = code.substring(0, code.length() - 1);
-        SysCodeConfigureRedis sysCodeConfigureRedis = SysCodeConfigureUtils.get(paterCode, code);
+        SysCodeConfigureRedis sysCodeConfigureRedis = SysCodeConfigureUtils.get(code);
         Integer port = SpringBeanUtils.getBean("getPort", Integer.class);
         JSONObject jsonObject = new JSONObject()
                 .set("code", code)

+ 21 - 17
sys-sdk/src/main/java/com/xy/utils/SysCodeConfigureUtils.java

@@ -2,6 +2,7 @@ package com.xy.utils;
 
 import com.xy.entity.SysCodeConfigureRedis;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -12,33 +13,36 @@ public class SysCodeConfigureUtils {
 
     public static final String SYS_CODE_CONFIGURE_PREFIX = "sys_code_configure";
 
-    public static String getKey(String key) {
-        return String.format("%s:%s", SYS_CODE_CONFIGURE_PREFIX, key);
-    }
-
     private static RedisService<SysCodeConfigureRedis> getRedisService() {
         return SpringBeanUtils.getBean(RedisService.class);
     }
 
     /**
-     * code获取所有子级列表
+     * code获取
      *
-     * @param paterCode 父code
-     * @return k=code v=子对象
+     * @param code
+     * @return SysCodeConfigureRedis
      */
-    public static Map<String, SysCodeConfigureRedis> get(String paterCode) {
-        return getRedisService().getMap(getKey(paterCode));
+    public static SysCodeConfigureRedis get(String code) {
+        return getRedisService().getMap(SYS_CODE_CONFIGURE_PREFIX).get(code);
     }
 
     /**
-     * 父code和code获取对象
+     * code获取
      *
-     * @param paterCode 父code
-     * @param code      code
-     * @return 子对象
+     * @param codes
+     * @return SysCodeConfigureRedis
      */
-    public static SysCodeConfigureRedis get(String paterCode, String code) {
-        return getRedisService().getMap(getKey(paterCode), code);
+    public static Map<String, SysCodeConfigureRedis> get(List<String> codes) {
+        Map<String, SysCodeConfigureRedis> map = getRedisService().getMap(SYS_CODE_CONFIGURE_PREFIX);
+        Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap = new HashMap<>();
+        codes.forEach(code -> {
+            SysCodeConfigureRedis sysCodeConfigureRedis = map.get(code);
+            if (sysCodeConfigureRedis != null) {
+                stringSysCodeConfigureRedisMap.put(code, sysCodeConfigureRedis);
+            }
+        });
+        return stringSysCodeConfigureRedisMap;
     }
 
     /**
@@ -46,7 +50,7 @@ public class SysCodeConfigureUtils {
      *
      * @return
      */
-    public static List<Map<String, Map<String, SysCodeConfigureRedis>>> all() {
-        return getRedisService().likeMap(SYS_CODE_CONFIGURE_PREFIX);
+    public static Map<String, SysCodeConfigureRedis> all() {
+        return getRedisService().getMap(SYS_CODE_CONFIGURE_PREFIX);
     }
 }