|
@@ -1,7 +1,9 @@
|
|
|
package cn.com.ty.lift.system.user.tree;
|
|
|
|
|
|
+import cn.com.ty.lift.system.utils.PojoUtils;
|
|
|
import cn.com.ty.lift.system.utils.ProjectUtils;
|
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -21,10 +23,10 @@ public class TreeUtil {
|
|
|
* @throws
|
|
|
* @author huangy
|
|
|
*/
|
|
|
- public static <T> List listToTree(List treeList, String parentAttr, T rootParentId) {
|
|
|
+ public static <T> List listToTree(List treeList, String parentAttr, T rootParentId, String attrName) {
|
|
|
BaseTree rootBaseTree = new BaseTree();
|
|
|
Map parentIdToChild = ProjectUtils.attrToListMap(treeList, parentAttr, null);
|
|
|
- getChildren(rootBaseTree, parentIdToChild, rootParentId);
|
|
|
+ getChildren(rootBaseTree, parentIdToChild, rootParentId, attrName);
|
|
|
return rootBaseTree.getChildren();
|
|
|
}
|
|
|
|
|
@@ -37,15 +39,28 @@ public class TreeUtil {
|
|
|
* @throws
|
|
|
* @author huangy
|
|
|
*/
|
|
|
- private static <T> void getChildren(BaseTree baseTree, Map<? extends Object, List<? extends BaseTree>> parentIdToChild, T rootParentId) {
|
|
|
+ private static <T> void getChildren(BaseTree baseTree, Map<? extends Object, List<? extends BaseTree>> parentIdToChild,
|
|
|
+ T rootParentId, String attrName){
|
|
|
List<? extends BaseTree> children = parentIdToChild.get(rootParentId);
|
|
|
if (children != null && children.size() > 0) {
|
|
|
for (BaseTree baseTreeInfo : children) {
|
|
|
//info:递归设置子集合
|
|
|
- getChildren(baseTreeInfo, parentIdToChild, baseTreeInfo.getId());
|
|
|
+ getChildren(baseTreeInfo, parentIdToChild, getAttrValue(baseTreeInfo, attrName), attrName);
|
|
|
}
|
|
|
}
|
|
|
//info:设置子集合
|
|
|
baseTree.setChildren(children);
|
|
|
}
|
|
|
+
|
|
|
+ public static Object getAttrValue(Object t,String attrName){
|
|
|
+ try{
|
|
|
+ Class<?> classObj = t.getClass();
|
|
|
+ String attrGetMethod = PojoUtils.attrGetMethodName(attrName);
|
|
|
+ Method method = classObj.getMethod(attrGetMethod, null);
|
|
|
+ return method.invoke(t, null);
|
|
|
+ } catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|