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;
|
}
|
}
|