WXF il y a 3 ans
Parent
commit
1bedd0ac26

+ 4 - 0
api-common/src/main/java/api/common/pojo/vo/system/DictVO.java

@@ -2,6 +2,8 @@ package api.common.pojo.vo.system;
 
 import lombok.Data;
 
+import java.util.List;
+
 @Data
 public class DictVO {
 
@@ -17,4 +19,6 @@ public class DictVO {
     private String dictCode;
     //序号
     private String dictOrder;
+    //子对象
+    private List<DictVO> children;
 }

+ 4 - 11
api-common/src/main/java/api/common/util/JsonUtil.java

@@ -1,11 +1,11 @@
 package api.common.util;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 
-import java.lang.reflect.Type;
+
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -51,15 +51,8 @@ public class JsonUtil {
     }
 
     public static <T> List<T> jsonToList(String json, Class<T> tClass) throws JsonProcessingException {
-        TypeFactory t = TypeFactory.defaultInstance();
-        //List<T> list = new ObjectMapper().readValue(json,t.constructReferenceType(ArrayList.class,tClass));
-        List<T> list = new ObjectMapper().readValue(json, new TypeReference<List<T>>() {
-            @Override
-            public Type getType() {
-                return super.getType();
-            }
-
-        });
+        JavaType javaType = TypeFactory.defaultInstance().constructParametricType(List.class, tClass);
+        List<T> list = new ObjectMapper().readValue(json,javaType);
         return list;
     }
 

+ 21 - 2
api-common/src/main/java/api/common/util/ObjectUtil.java

@@ -1,5 +1,6 @@
 package api.common.util;
 
+import api.common.pojo.vo.system.DictVO;
 import com.fasterxml.jackson.databind.JsonNode;
 
 import java.util.*;
@@ -80,8 +81,26 @@ public class ObjectUtil {
         return jsonNode.isNull() || jsonNode.isEmpty();
     }
 
-    public static String uuid() {
-        return UUID.randomUUID().toString().replace("-", "");
+    public static List<DictVO> DictListToTree(List<DictVO> list) {
+        List<DictVO> treeList = new ArrayList<DictVO>();
+        for (DictVO dictVO: list) {
+            if("0".equals(dictVO.getPid())){
+                treeList.add(findChildren(dictVO,list));
+            }
+        }
+        return treeList;
+    }
+
+    private static DictVO findChildren(DictVO dictVO, List<DictVO> list) {
+        for (DictVO node: list) {
+            if(dictVO.getId().equals(node.getPid())){
+                if(dictVO.getChildren() == null || dictVO.getChildren().size() == 0){
+                    dictVO.setChildren(new ArrayList<DictVO>());
+                }
+                dictVO.getChildren().add(findChildren(node,list));
+            }
+        }
+        return dictVO;
     }
 
 }

+ 14 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/system/ctrl/DictCtrl.java

@@ -53,6 +53,20 @@ public class DictCtrl {
         return response;
     }
 
+    /**
+     * 通过字典类型dictTypes从缓存中查询字典数据trees
+     * @param param
+     * @return
+     * @throws JsonProcessingException
+     */
+    @RequestMapping("/getDictTreesByTypes")
+    @ResponseBody
+    public ResponseBodyVO getDictTreesByTypes(@RequestBody @Validated DictParam param) throws JsonProcessingException {
+        ResponseBodyVO<Map<String, List<DictVO>>> response = new ResponseBodyVO<Map<String,List<DictVO>>>(ResponseBodyVO.Response.SUCCESS);
+        response.setInfo(dictService.getDictTreesByTypes(param));
+        return response;
+    }
+
     /**
      * 通过字典类型dictTypes从缓存中查询字典数据maps
      * @param param

+ 21 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/system/service/DictService.java

@@ -5,6 +5,7 @@ import api.common.pojo.param.RedisParameter;
 import api.common.pojo.param.system.DictParam;
 import api.common.pojo.vo.system.DictVO;
 import api.common.util.JsonUtil;
+import api.common.util.ObjectUtil;
 import com.css.simulation.resource.feign.RedisService;
 import com.css.simulation.resource.system.mapper.DictMapper;
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -89,6 +90,26 @@ public class DictService {
         return dictLists;
     }
 
+    public Map<String, List<DictVO>> getDictTreesByTypes(DictParam param) throws JsonProcessingException {
+        //解析参数进行查询
+        String dictTypes = param.getDictTypes();
+        List<String> dictTypeList = Arrays.asList(dictTypes.split(","));
+        ResponseBodyVO<Map<String, String>> responseBodyVO = redisService.getDictLists(dictTypeList);
+        //查询结果转换至java对象
+        Map<String, String> map = responseBodyVO.getInfo();
+        Map<String, List<DictVO>> dictTrees = new HashMap<>();
+        Set<String> keySet = map.keySet();
+        for (String key: keySet) {
+            String val = map.get(key);
+            //json转javaBean
+            List<DictVO> dictVOS = JsonUtil.jsonToList(val, DictVO.class);
+            //list转tree
+            List<DictVO> tree = ObjectUtil.DictListToTree(dictVOS);
+            dictTrees.put(key,tree);
+        }
+        return dictTrees;
+    }
+
     public Map<String, Map<String,String>> getDictMapsByTypes(DictParam param) {
         //解析参数进行查询
         String dictTypes = param.getDictTypes();