|
@@ -2,6 +2,7 @@ package cn.tr.core.utils;
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.tr.core.tree.TreeNode;
|
|
import cn.tr.core.tree.TreeNode;
|
|
|
import lombok.experimental.UtilityClass;
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
|
|
|
@@ -62,42 +63,44 @@ public class TreeUtil {
|
|
|
.filter(source->!excludeNodeIds.contains(source.getId()))
|
|
.filter(source->!excludeNodeIds.contains(source.getId()))
|
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ //找到顶级节点
|
|
|
|
|
+ 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())))
|
|
|
.collect(Collectors.groupingBy(TreeNode::getParentId));
|
|
.collect(Collectors.groupingBy(TreeNode::getParentId));
|
|
|
- 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);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ result.forEach(topNode->{
|
|
|
|
|
+ topNode.setNodePath(topNode.getName());
|
|
|
|
|
+ fillChildNode(topNode,groupByParentId);
|
|
|
});
|
|
});
|
|
|
- ArrayList<Node> result = new ArrayList<>(topNode);
|
|
|
|
|
CollectionUtil.sort(result, Comparator.comparing(TreeNode::getSort));
|
|
CollectionUtil.sort(result, Comparator.comparing(TreeNode::getSort));
|
|
|
return result;
|
|
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));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 校验节点配置是否合理
|
|
* 校验节点配置是否合理
|
|
|
*/
|
|
*/
|