Explorar o código

fix:
修改树结构的构造

18339543638 %!s(int64=2) %!d(string=hai) anos
pai
achega
f3df60b709
Modificáronse 1 ficheiros con 30 adicións e 33 borrados
  1. 30 33
      tr-framework/src/main/java/cn/tr/core/utils/TreeUtil.java

+ 30 - 33
tr-framework/src/main/java/cn/tr/core/utils/TreeUtil.java

@@ -2,7 +2,6 @@ package cn.tr.core.utils;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.ObjectUtil;
-import cn.hutool.core.util.StrUtil;
 import cn.tr.core.tree.TreeNode;
 import lombok.experimental.UtilityClass;
 
@@ -63,44 +62,42 @@ public class TreeUtil {
                     .filter(source->!excludeNodeIds.contains(source.getId()))
                     .collect(Collectors.toList());
         }
-        //找到顶级节点
-        sources.stream()
-                .peek(node->node.setParentId(ObjectUtil.isEmpty(node.getParentId())?"0":node.getParentId()))
-                .filter(source-> ObjectUtil.isNull(source.getSort())).forEach(source->source.setSort(999));
-        List<Node> result = sources.stream()
-                .filter(node -> StrUtil.equals("0",String.valueOf(node.getParentId())))
-                .collect(Collectors.toList());
-        if(CollectionUtil.isEmpty(result)){
-            return result;
-        }
-        Map<Object, List<TreeNode>> groupByParentId = sources.stream()
-                .filter(node ->!StrUtil.equals("0",String.valueOf(node.getParentId())))
+
+        Map<Object, Node> nodeIdMap = sources.stream()
+                .collect(Collectors.groupingBy(TreeNode::getId, Collectors.collectingAndThen(Collectors.toList(), CollectionUtil::getFirst)));
+
+        Map<Object, List<Node>> groupByParentId = sources.stream()
+                .peek(node->{
+                    if(ObjectUtil.isNull(node.getParentId())){
+                        node.setParentId("-1");
+                    }
+                    if(ObjectUtil.isNull(node.getSort())){
+                        node.setSort(999);
+                    }
+                })
                 .collect(Collectors.groupingBy(TreeNode::getParentId));
-        result.forEach(topNode->{
-            topNode.setNodePath(topNode.getName());
-            fillChildNode(topNode,groupByParentId);
+        Set<Node> topNode = new HashSet<>();
+        groupByParentId.forEach((parentId,children)->{
+            CollectionUtil.sort(children,Comparator.comparing(TreeNode::getSort));
+            Node node = nodeIdMap.get(parentId);
+            if(ObjectUtil.isNull(node)){
+                children.forEach(child->{
+                    child.setNodePath(child.getName());
+                });
+                topNode.addAll(children);
+            }else {
+                children.forEach(child->{
+                    child.setNodePath(Optional.ofNullable(node.getName()).orElse("")+"/"+child.getName());
+                });
+                node.setChildren(children);
+                topNode.add(node);
+            }
         });
+        ArrayList<Node> result = new ArrayList<>(topNode);
         CollectionUtil.sort(result, Comparator.comparing(TreeNode::getSort));
         return result;
     }
 
-    /**
-     * 填充叶子子节点
-     */
-    private static void fillChildNode(TreeNode source, Map<Object, List<TreeNode>> groupByParentIdMap){
-        Object parentId = source.getId();
-        List<TreeNode> children = groupByParentIdMap.get(parentId);
-        if(CollectionUtil.isEmpty(children)){
-            return;
-        }
-        source.setChildren(children);
-        children.forEach(child->{
-            child.setNodePath(Optional.ofNullable(source.getName()).orElse("")+"/"+child.getName());
-            fillChildNode(child,groupByParentIdMap);
-        });
-        CollectionUtil.sort(children,Comparator.comparing(TreeNode::getSort));
-    }
-
     /**
      * 校验节点配置是否合理
      */