liujie
2026-02-11 a6d2a0ebcb6d9cc8c3c2cbd274e4b942830ee767
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.estate.expert.admin.utils.tree;
 
import com.estate.expert.admin.enums.BizResultCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import javax.persistence.Transient;
import java.util.*;
 
/**
 * 树根节点
 */
@Getter
@Setter
@Slf4j
public abstract class TreeRoot<T> {
 
    // 保存所有的节点, 用于快速查找
    @JsonIgnore
    @Transient
    private HashMap<String, TreeNode<T>> nodeMap = new HashMap<>();
 
    @JsonIgnore
    @Transient
    private HashMap<String, TreeNode<T>> namePathMap = new HashMap<>();
 
    /*
    * 获取树的根节点
    * 数据放到具体的实现类中
    * 例如DepartmentStore
    * 以便于可读性和字段控制
     */
    public abstract TreeNode<T> root() ;
 
    public TreeNode<T> getByNamePath(String namePath) {
        return namePathMap.get(namePath);
    }
 
    public TreeNode<T> get(String path) {
        return get(path, true);
    }
 
    public TreeNode<T> get(String path, boolean recursive) {
        var node = nodeMap.get(path);
        if (node == null) {
            return null;
        }
        if (recursive) {
            return node;
        }
 
        return node.ShadowNode();
    }
 
    public void edit(String path, String name, T data) {
        var node = get(path);
        BizResultCode.HIERARCHY_NODE_NOT_FOUND.check(node == null);
 
        if(node.getParent()!= null) {
            for (TreeNode<T> child : node.getParent().getChildren().values()) {
                BizResultCode.HIERARCHY_NODE_NAME_ALREADY_EXISTS.check(child.getName().equals(name) && !child.getId().equals(node.getId()));
            }
        }
        node.setName(name);
        node.setData(data);
    }
 
    public void remove(String path) {
        var node = get(path);
        if (node == null) {
            return;
        }
        nodeMap.remove(path);
        namePathMap.remove(node.getNamePath());
 
        var parent = node.getParent();
        if (parent == null) {
            return;
        }
        parent.getChildren().remove(node.getId());
    }
 
    public TreeNode<T> addChild(String parentPath, String name, T data) {
        var parent = get(parentPath);
        BizResultCode.HIERARCHY_PARENT_NOT_FOUND.check(parent == null);
        var child = parent.addChild(name, data);
        nodeMap.put(child.getPath(), child);
        namePathMap.put(child.getNamePath(), child);
        return child;
    }
 
    // copy节点
//    public TreeNode<T> copy(String path) {
//        var node = get(path);
//        BizResultCode.HIERARCHY_NODE_NOT_FOUND.check(node == null);
//        var parent = node.getParent();
//        BizResultCode.HIERARCHY_PARENT_NOT_FOUND.check(parent == null);
//
////        TreeHelper.convertTree(node, )
//    }
 
    /*
    * 设置树的父子关系
    * 同时将所有的节点放入nodeMap中
    * 便于快速查找
     */
    public TreeRoot<T> buildTree() {
        nodeMap.clear();
 
        if(root() == null)
            return this;
        root().buildPath();
        nodeMap.put(root().getPath(), root());
        namePathMap.clear();
        namePathMap.put(root().getNamePath(), root());
 
        setParent(root());
        return this;
    }
 
    // 设置所有的节点的父节点引用, 并将节点放入nodeMap中
    private void setParent(TreeNode<T> node) {
        for (TreeNode<T> child : node.getChildren().values()) {
            child.setParent(node);
            child.buildPath();
            nodeMap.put(child.getPath(), child);
            namePathMap.put(child.getNamePath(), child);
            setParent(child);
        }
    }
 
    public List<TreeNode<T>> children(String path){
        return children(path, null);
    }
 
    public List<TreeNode<T>> children(String path, String keyword) {
        var result = new ArrayList<TreeNode<T>>();
        var node = get(path);
 
        BizResultCode.HIERARCHY_NODE_NOT_FOUND.check(node == null);
        // 不包含子节点
        for (var child : node.getChildren().values()) {
            if(StringUtils.isBlank(keyword) || child.getName().contains(keyword))
                result.add(child.ShadowNode());
        }
        return result;
    }
 
    public List<String> findAllPaths(List<String> hierarchyPaths) {
        var nodes = hierarchyPaths.stream()
                .map(h -> get(h, true))
                .filter(Objects::nonNull)
                .toList();
 
        // 归总并过滤掉相同的path
        var pathMap = new TreeSet<String>();
        nodes.forEach(n -> pathMap.addAll( n.allPaths()));
        return new ArrayList<>(pathMap);
    }
 
}