Sfoglia il codice sorgente

区域树结构显示

黄远 5 anni fa
parent
commit
fc761bc014

+ 4 - 4
lift-system-service/pom.xml

@@ -18,10 +18,10 @@
             <artifactId>xwy-spring-boot</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>cn.com.xwy</groupId>
-            <artifactId>xwy-cloud-dependencies</artifactId>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>cn.com.xwy</groupId>-->
+<!--            <artifactId>xwy-cloud-dependencies</artifactId>-->
+<!--        </dependency>-->
 
         <!-- 添加公共模块依赖 -->
         <dependency>

+ 1 - 1
lift-system-service/src/main/java/cn/com/ty/lift/system/user/service/impl/AreaCodeService.java

@@ -25,6 +25,6 @@ public class AreaCodeService extends ServiceImpl<AreaCodeMapper, AreaCode> imple
     @Override
     public List<AreaCode> areaTree() {
         List<AreaCode> allAreaCodeList = areaCodeMapper.getAll();
-        return TreeUtil.listToTree(allAreaCodeList, ApiConstants.AREA_PARENT_ATTR, ApiConstants.ROOT_AREA_PARENT_ID);
+        return TreeUtil.listToTree(allAreaCodeList, ApiConstants.AREA_PARENT_ATTR, ApiConstants.ROOT_AREA_PARENT_ID, "code");
     }
 }

+ 1 - 1
lift-system-service/src/main/java/cn/com/ty/lift/system/user/service/impl/MenuService.java

@@ -41,7 +41,7 @@ public class MenuService extends ServiceImpl<MenuMapper, Menu> implements IMenuS
     @Override
     public String menuToUrl(List<Menu> menuList) {
         //将菜单转化为菜单树
-        List<Menu> menuTree = TreeUtil.listToTree(menuList, ApiConstants.MENU_PARENT_ATTR, ApiConstants.ROOT_MENU_PARENT_ID);
+        List<Menu> menuTree = TreeUtil.listToTree(menuList, ApiConstants.MENU_PARENT_ATTR, ApiConstants.ROOT_MENU_PARENT_ID, "id");
         StringBuilder sb = new StringBuilder();
         //递归组合菜单路径
         menuTreeToUrl(menuTree, sb, "");

+ 19 - 4
lift-system-service/src/main/java/cn/com/ty/lift/system/user/tree/TreeUtil.java

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