diff --git a/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java b/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java index 77311b2..8312a03 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java +++ b/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java @@ -343,13 +343,26 @@ public class BeanUtils { * @return */ public static List buildTree(List allNodes, Object rootNodeId){ + return buildTree(allNodes, rootNodeId, Cons.FieldName.parentId.name(), Cons.FieldName.children.name()); + } + + /*** + * 构建指定根节点的上下级关联的树形结构(上级parentId、子节点children) + * @param allNodes 所有节点对象 + * @param rootNodeId 跟节点ID + * @param parentIdFieldName 父节点属性名 + * @param childrenFieldName 子节点集合属性名 + * @param + * @return + */ + public static List buildTree(List allNodes, Object rootNodeId, String parentIdFieldName, String childrenFieldName){ if(V.isEmpty(allNodes)){ return null; } // 提取所有的top level对象 List topLevelModels = new ArrayList(); for(T node : allNodes){ - Object parentId = getProperty(node, Cons.FieldName.parentId.name()); + Object parentId = getProperty(node, parentIdFieldName); if(parentId == null || V.fuzzyEqual(parentId, rootNodeId)){ topLevelModels.add(node); } @@ -364,8 +377,8 @@ public class BeanUtils { // 遍历第一级节点,并挂载 children 子节点 for(T node : allNodes) { Object nodeId = getProperty(node, Cons.FieldName.id.name()); - List children = buildTreeChildren(nodeId, allNodes); - setProperty(node, Cons.FieldName.children.name(), children); + List children = buildTreeChildren(nodeId, allNodes, parentIdFieldName, childrenFieldName); + setProperty(node, childrenFieldName, children); } return topLevelModels; } @@ -374,12 +387,14 @@ public class BeanUtils { * 递归构建树节点的子节点 * @param parentId * @param nodeList + * @param parentIdFieldName 父节点属性名 + * @param childrenFieldName 子节点集合属性名 * @return */ - private static List buildTreeChildren(Object parentId, List nodeList) { + private static List buildTreeChildren(Object parentId, List nodeList, String parentIdFieldName, String childrenFieldName) { List children = null; for(T node : nodeList) { - Object nodeParentId = getProperty(node, Cons.FieldName.parentId.name()); + Object nodeParentId = getProperty(node, parentIdFieldName); if(nodeParentId != null && V.equals(nodeParentId, parentId)) { if(children == null){ children = new ArrayList<>(); @@ -390,11 +405,11 @@ public class BeanUtils { if(children != null){ for(T child : children) { Object nodeId = getProperty(child, Cons.FieldName.id.name()); - List childNodeChildren = buildTreeChildren(nodeId, nodeList); + List childNodeChildren = buildTreeChildren(nodeId, nodeList, parentIdFieldName, childrenFieldName); if(childNodeChildren == null) { childNodeChildren = new ArrayList<>(); } - setProperty(child, Cons.FieldName.children.name(), childNodeChildren); + setProperty(child, childrenFieldName, childNodeChildren); } } return children; diff --git a/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java b/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java index f92e139..ca4f6da 100644 --- a/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java +++ b/diboot-core/src/test/java/diboot/core/test/util/BeanUtilsTest.java @@ -174,6 +174,11 @@ public class BeanUtilsTest { Assert.assertEquals(list.size(), 1); Assert.assertEquals(list.get(0).getChildren().size(), 5); + list = BeanUtils.convertList(dictionaryList, DictionaryVO.class); + list = BeanUtils.buildTree(list, 0, "parentId", "children"); + Assert.assertEquals(list.size(), 1); + Assert.assertEquals(list.get(0).getChildren().size(), 5); + // 异常数据告警 Dictionary dict2 = new Dictionary(); dict2.setId(1L); @@ -182,6 +187,7 @@ public class BeanUtilsTest { list = BeanUtils.convertList(dictionaryList, DictionaryVO.class); try{ list = BeanUtils.buildTree(list); + Assert.assertTrue(false); } catch (Exception e){ Assert.assertTrue(e.getMessage().contains("请检查"));