mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 优化 TreeBuildUtils 工具 使用反射自动获取顶级父id
This commit is contained in:
		@@ -1,9 +1,11 @@
 | 
			
		||||
package com.ruoyi.common.utils;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import cn.hutool.core.lang.tree.Tree;
 | 
			
		||||
import cn.hutool.core.lang.tree.TreeNodeConfig;
 | 
			
		||||
import cn.hutool.core.lang.tree.TreeUtil;
 | 
			
		||||
import cn.hutool.core.lang.tree.parser.NodeParser;
 | 
			
		||||
import com.ruoyi.common.utils.reflect.ReflectUtils;
 | 
			
		||||
import lombok.AccessLevel;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
 | 
			
		||||
@@ -22,8 +24,12 @@ public class TreeBuildUtils extends TreeUtil {
 | 
			
		||||
     */
 | 
			
		||||
    public static final TreeNodeConfig DEFAULT_CONFIG = TreeNodeConfig.DEFAULT_CONFIG.setNameKey("label");
 | 
			
		||||
 | 
			
		||||
    public static <T> List<Tree<Long>> build(List<T> list, Long parentId, NodeParser<T, Long> nodeParser) {
 | 
			
		||||
        return TreeUtil.build(list, parentId, DEFAULT_CONFIG, nodeParser);
 | 
			
		||||
    public static <T, K> List<Tree<K>> build(List<T> list, NodeParser<T, K> nodeParser) {
 | 
			
		||||
        if (CollUtil.isEmpty(list)) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        K k = ReflectUtils.invokeGetter(list.get(0), "parentId");
 | 
			
		||||
        return TreeUtil.build(list, k, DEFAULT_CONFIG, nodeParser);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -60,8 +60,7 @@ public class SysDeptServiceImpl implements ISysDeptService {
 | 
			
		||||
        if (CollUtil.isEmpty(depts)) {
 | 
			
		||||
            return CollUtil.newArrayList();
 | 
			
		||||
        }
 | 
			
		||||
        Long parentId = depts.get(0).getParentId();
 | 
			
		||||
        return TreeBuildUtils.build(depts, parentId, (dept, tree) ->
 | 
			
		||||
        return TreeBuildUtils.build(depts, (dept, tree) ->
 | 
			
		||||
            tree.setId(dept.getDeptId())
 | 
			
		||||
                .setParentId(dept.getParentId())
 | 
			
		||||
                .setName(dept.getDeptName())
 | 
			
		||||
 
 | 
			
		||||
@@ -181,8 +181,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
 | 
			
		||||
        if (CollUtil.isEmpty(menus)) {
 | 
			
		||||
            return CollUtil.newArrayList();
 | 
			
		||||
        }
 | 
			
		||||
        Long parentId = menus.get(0).getParentId();
 | 
			
		||||
        return TreeBuildUtils.build(menus, parentId, (menu, tree) ->
 | 
			
		||||
        return TreeBuildUtils.build(menus, (menu, tree) ->
 | 
			
		||||
            tree.setId(menu.getMenuId())
 | 
			
		||||
                .setParentId(menu.getParentId())
 | 
			
		||||
                .setName(menu.getMenuName())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user