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 { 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 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 addChildren(CommonGenericTree node) { if (this.children == null) { this.children = new ArrayList(); } 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 getLeafNode(Map map, CommonGenericTree tree) { if (tree == null) return map; if (tree.hasChildren()) { List 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 getAllParentNode(Map map, Map 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 map) { if (oldNode == null || map == null || map.isEmpty()) return newNode; if (oldNode.hasChildren()) { List 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 convertMapToList(Map map) { if (map == null || map.isEmpty()) return Collections.emptyList(); List tree = new ArrayList<>(); CommonGenericTree node; CommonGenericTree parent; for (Map.Entry 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; } }