cuijian
2023-08-19 bdd0875d4b13a3f1ef472f64d4b6a95e0ef64b22
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
package org.jeecg.common.api.vo;
 
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringUtils;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
@SuppressWarnings("rawtypes")
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class CommonGenericTree<T> {
 
    private String key;// 要显示的节点的ID
 
    private String title;// 要显示的子节点的名称
 
    private String icon; // 节点的图标
 
    private String parentId;// 父节点的ID
 
    private boolean isLeaf;// 是否为叶子节点
 
    private boolean selectable = true;// 是否可选
 
    private boolean disabled = false;// 是否可选
 
    private List<CommonGenericTree> children;// 子节点的List
    
    private String rField1;// 绑定扩展属性1
 
    private String rField2;// 绑定扩展属性2
 
    private String rField3;// 绑定扩展属性3
 
    private ScopedSlots scopedSlots = new ScopedSlots();
 
    private Integer type;
 
    private T entity;
 
    private String value;
 
    /**
     * @Description: 添加子节点
     */
    @SuppressWarnings("unchecked")
    public CommonGenericTree<T> addChildren(CommonGenericTree node) {
        if (this.children == null) {
            this.children = new ArrayList<CommonGenericTree>();
        }
        this.children.add(node);
        return node;
    }
 
    public boolean hasChildren() {
        boolean bu = true;
        if (getChildren() == null || getChildren().isEmpty()) {
            bu = false;
            return bu;
        }
        return bu;
    }
 
    /**
     * @Description: 获取树的所有叶子节点
     */
    @SuppressWarnings("unchecked")
    public static Map<String, CommonGenericTree> getLeafNode(Map<String, CommonGenericTree> map,
            CommonGenericTree tree) {
        if (tree == null)
            return map;
        if (tree.hasChildren()) {
            List<CommonGenericTree> childrenNodes = tree.getChildren();
            for (CommonGenericTree child : childrenNodes) {
                map = getLeafNode(map, child);
            }
            map.put(tree.getKey(), tree);
        } else {
            map.put(tree.getKey(), tree);
        }
        return map;
    }
 
    public static Map<String, CommonGenericTree> getAllParentNode(Map<String, CommonGenericTree> map,
            Map<String, CommonGenericTree> allNode, CommonGenericTree childNode) {
        if (childNode == null || allNode == null || allNode.isEmpty())
            return map;
        String parentId = childNode.getParentId();
        if (StringUtils.isNotBlank(parentId)) {
            CommonGenericTree parent;
            if (allNode.containsKey(parentId)) {
                parent = allNode.get(childNode.getParentId());
                map.put(childNode.getKey(), childNode);
                map = getAllParentNode(map, allNode, parent);
            } else {
                map.put(childNode.getKey(), childNode);
            }
 
        } else {
            map.put(childNode.getKey(), childNode);
        }
        return map;
    }
 
    @SuppressWarnings("unchecked")
    public static CommonGenericTree copyToNewTree(CommonGenericTree oldNode, CommonGenericTree newNode,
            Map<String, CommonGenericTree> map) {
        if (oldNode == null || map == null || map.isEmpty())
            return newNode;
        if (oldNode.hasChildren()) {
            List<CommonGenericTree> childrenNodes = oldNode.getChildren();
            CommonGenericTree newChild;
            for (CommonGenericTree child : childrenNodes) {
                if (map.containsKey(child.getKey())) {
                    newChild = new CommonGenericTree();
                    newChild.copyNoneChild(child);
                    newNode.addChildren(newChild);
                    copyToNewTree(child, newChild, map);
                }
            }
        }
        return newNode;
 
    }
 
    public static List<CommonGenericTree> convertMapToList(Map<String, CommonGenericTree> map) {
        if (map == null || map.isEmpty())
            return Collections.emptyList();
        List<CommonGenericTree> tree = new ArrayList<>();
        CommonGenericTree node;
        CommonGenericTree parent;
        for (Map.Entry<String, CommonGenericTree> en : map.entrySet()) {
            node = en.getValue();
            if (node != null) {
                if (node.getParentId() == null) {
                    tree.add(en.getValue());
                } else {
                    if (map.containsKey(node.getParentId())) {
                        parent = map.get(node.getParentId());
                        parent.addChildren(node);
                    }
                }
            }
        }
        return tree;
    }
 
    @SuppressWarnings("unchecked")
    public CommonGenericTree copyNoneChild(CommonGenericTree oldNode) {
        this.setKey(oldNode.getKey()).setTitle(oldNode.getTitle()).setType(oldNode.getType()).setIcon(oldNode.getIcon())
                .setEntity((T) oldNode.getEntity()).setParentId(oldNode.getParentId()).setRField1(oldNode.getRField1())
                .setRField2(oldNode.getRField2());
        return this;
    }
 
}