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
package org.jeecg.modules.dnc.response;
 
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
@Data
@NoArgsConstructor
public class CommonGenericTree<T> {
    private String id; // 要显示的节点的ID
    private String label; // 要显示的子节点的名称
    private String iconClass; // 节点的图标
    private String parentId; // 父节点的ID
    private List<CommonGenericTree> children; // 孩子节点的List
    private String rField; //绑定扩展鼠性
    /**
     * 节点分类
     * 产品树型 1 产品 2 部件 3 零件
     * 设备树形 1 设备分组 2 设备
     */
    private Integer type;
    private T entity;
    /**
     * 是否为子节点
     */
    private Boolean leaf = false;
 
    // 添加子节点的方法
    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;
    }
 
    /**
     * 获取树的所有叶子节点
     * @param tree
     * @return
     */
    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.getId(), tree);
        }else {
            map.put(tree.getId(), 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(StrUtil.isEmpty(parentId)) {
            CommonGenericTree parent;
            if(allNode.containsKey(parentId)) {
                parent = allNode.get(childNode.getParentId());
                map.put(childNode.getId(), childNode);
                map = getAllParentNode(map, allNode, parent);
            }else {
                map.put(childNode.getId(), childNode);
            }
 
        }else {
            map.put(childNode.getId(), childNode);
        }
        return map;
    }
 
    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.getId())) {
                    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;
    }
 
    public CommonGenericTree copyNoneChild(CommonGenericTree oldNode) {
        this.id = oldNode.getId();
        this.label = oldNode.getLabel();
        this.type = oldNode.getType();
        this.iconClass = oldNode.getIconClass();
        this.entity = (T)oldNode.getEntity();
        this.rField = oldNode.getRField();
        this.parentId = oldNode.getParentId();
        return this;
    }
}