mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-11-04 09:01:40 +08:00 
			
		
		
		
	Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
		@@ -0,0 +1,161 @@
 | 
				
			|||||||
 | 
					package top.continew.starter.core.util;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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 cn.hutool.core.text.CharSequenceUtil;
 | 
				
			||||||
 | 
					import cn.hutool.core.util.ReflectUtil;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					import java.util.function.Function;
 | 
				
			||||||
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					import java.util.stream.Stream;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 扩展 hutool TreeUtil 封装树构建
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Lion Li
 | 
				
			||||||
 | 
					 * @author lishuyan
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class TreeBuildUtils extends TreeUtil {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private TreeBuildUtils() {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 构建树形结构
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param <T>        输入节点的类型
 | 
				
			||||||
 | 
					     * @param <K>        节点ID的类型
 | 
				
			||||||
 | 
					     * @param list       节点列表,其中包含了要构建树形结构的所有节点
 | 
				
			||||||
 | 
					     * @param nodeParser 解析器,用于将输入节点转换为树节点
 | 
				
			||||||
 | 
					     * @return 构建好的树形结构列表
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T, K> List<Tree<K>> build(List<T> list, NodeParser<T, K> nodeParser) {
 | 
				
			||||||
 | 
					        if (CollUtil.isEmpty(list)) {
 | 
				
			||||||
 | 
					            return CollUtil.newArrayList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        K k = ReflectUtil.invoke(list.get(0), CharSequenceUtil.genGetter("parentId"));
 | 
				
			||||||
 | 
					        return TreeUtil.build(list, k, TreeNodeConfig.DEFAULT_CONFIG, nodeParser);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 构建树形结构
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param <T>        输入节点的类型
 | 
				
			||||||
 | 
					     * @param <K>        节点ID的类型
 | 
				
			||||||
 | 
					     * @param parentId   顶级节点
 | 
				
			||||||
 | 
					     * @param list       节点列表,其中包含了要构建树形结构的所有节点
 | 
				
			||||||
 | 
					     * @param nodeParser 解析器,用于将输入节点转换为树节点
 | 
				
			||||||
 | 
					     * @return 构建好的树形结构列表
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T, K> List<Tree<K>> build(List<T> list, K parentId, NodeParser<T, K> nodeParser) {
 | 
				
			||||||
 | 
					        if (CollUtil.isEmpty(list)) {
 | 
				
			||||||
 | 
					            return CollUtil.newArrayList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return TreeUtil.build(list, parentId, TreeNodeConfig.DEFAULT_CONFIG, nodeParser);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 构建树形结构
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param <T>            输入节点的类型
 | 
				
			||||||
 | 
					     * @param <K>            节点ID的类型
 | 
				
			||||||
 | 
					     * @param list           节点列表,其中包含了要构建树形结构的所有节点
 | 
				
			||||||
 | 
					     * @param parentId       顶级节点
 | 
				
			||||||
 | 
					     * @param treeNodeConfig 树节点配置
 | 
				
			||||||
 | 
					     * @param nodeParser     解析器,用于将输入节点转换为树节点
 | 
				
			||||||
 | 
					     * @return 构建好的树形结构列表
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T, K> List<Tree<K>> build(List<T> list, K parentId, TreeNodeConfig treeNodeConfig, NodeParser<T, K> nodeParser) {
 | 
				
			||||||
 | 
					        if (CollUtil.isEmpty(list)) {
 | 
				
			||||||
 | 
					            return CollUtil.newArrayList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return TreeUtil.build(list, parentId, treeNodeConfig, nodeParser);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 构建多根节点的树结构(支持多个顶级节点)
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param list        原始数据列表
 | 
				
			||||||
 | 
					     * @param getId       获取节点 ID 的方法引用,例如:node -> node.getId()
 | 
				
			||||||
 | 
					     * @param getParentId 获取节点父级 ID 的方法引用,例如:node -> node.getParentId()
 | 
				
			||||||
 | 
					     * @param parser      树节点属性映射器,用于将原始节点 T 转为 Tree 节点
 | 
				
			||||||
 | 
					     * @param <T>         原始数据类型(如实体类、DTO 等)
 | 
				
			||||||
 | 
					     * @param <K>         节点 ID 类型(如 Long、String)
 | 
				
			||||||
 | 
					     * @return 构建完成的树形结构(可能包含多个顶级根节点)
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T, K> List<Tree<K>> buildMultiRoot(List<T> list, Function<T, K> getId, Function<T, K> getParentId, NodeParser<T, K> parser) {
 | 
				
			||||||
 | 
					        if (CollUtil.isEmpty(list)) {
 | 
				
			||||||
 | 
					            return CollUtil.newArrayList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Set<K> rootParentIds = CollUtils.mapToSet(list, getParentId);
 | 
				
			||||||
 | 
					        rootParentIds.removeAll(CollUtils.mapToSet(list, getId));
 | 
				
			||||||
 | 
					        // 构建每一个根 parentId 下的树,并合并成最终结果列表
 | 
				
			||||||
 | 
					        return rootParentIds.stream()
 | 
				
			||||||
 | 
					            .flatMap(rootParentId -> TreeUtil.build(list, rootParentId, parser).stream())
 | 
				
			||||||
 | 
					            .collect(Collectors.toList());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 构建多根节点的树结构(支持多个顶级节点)
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param <T>            原始数据类型(如实体类、DTO 等)
 | 
				
			||||||
 | 
					     * @param <K>            节点 ID 类型(如 Long、String)
 | 
				
			||||||
 | 
					     * @param list           原始数据列表
 | 
				
			||||||
 | 
					     * @param getId          获取节点 ID 的方法引用,例如:node -> node.getId()
 | 
				
			||||||
 | 
					     * @param getParentId    获取节点父级 ID 的方法引用,例如:node -> node.getParentId()
 | 
				
			||||||
 | 
					     * @param treeNodeConfig 树节点配置
 | 
				
			||||||
 | 
					     * @param parser         树节点属性映射器,用于将原始节点 T 转为 Tree 节点
 | 
				
			||||||
 | 
					     * @return 构建完成的树形结构(可能包含多个顶级根节点)
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T, K> List<Tree<K>> buildMultiRoot(List<T> list, Function<T, K> getId, Function<T, K> getParentId,
 | 
				
			||||||
 | 
					                                                      TreeNodeConfig treeNodeConfig, NodeParser<T, K> parser) {
 | 
				
			||||||
 | 
					        if (CollUtil.isEmpty(list)) {
 | 
				
			||||||
 | 
					            return CollUtil.newArrayList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Set<K> rootParentIds = CollUtils.mapToSet(list, getParentId);
 | 
				
			||||||
 | 
					        rootParentIds.removeAll(CollUtils.mapToSet(list, getId));
 | 
				
			||||||
 | 
					        // 构建每一个根 parentId 下的树,并合并成最终结果列表
 | 
				
			||||||
 | 
					        return rootParentIds.stream()
 | 
				
			||||||
 | 
					                .flatMap(rootParentId -> TreeUtil.build(list, rootParentId, treeNodeConfig, parser).stream())
 | 
				
			||||||
 | 
					                .collect(Collectors.toList());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 获取节点列表中所有节点的叶子节点
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param <K>   节点ID的类型
 | 
				
			||||||
 | 
					     * @param nodes 节点列表
 | 
				
			||||||
 | 
					     * @return 包含所有叶子节点的列表
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <K> List<Tree<K>> getLeafNodes(List<Tree<K>> nodes) {
 | 
				
			||||||
 | 
					        if (CollUtil.isEmpty(nodes)) {
 | 
				
			||||||
 | 
					            return CollUtil.newArrayList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return nodes.stream()
 | 
				
			||||||
 | 
					            .flatMap(TreeBuildUtils::extractLeafNodes)
 | 
				
			||||||
 | 
					            .collect(Collectors.toList());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 获取指定节点下的所有叶子节点
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param <K>  节点ID的类型
 | 
				
			||||||
 | 
					     * @param node 要查找叶子节点的根节点
 | 
				
			||||||
 | 
					     * @return 包含所有叶子节点的列表
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private static <K> Stream<Tree<K>> extractLeafNodes(Tree<K> node) {
 | 
				
			||||||
 | 
					        if (!node.hasChild()) {
 | 
				
			||||||
 | 
					            return Stream.of(node);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            // 递归调用,获取所有子节点的叶子节点
 | 
				
			||||||
 | 
					            return node.getChildren().stream()
 | 
				
			||||||
 | 
					                .flatMap(TreeBuildUtils::extractLeafNodes);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -61,10 +61,6 @@ public interface CrudService<L, D, Q, C> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 查询树列表
 | 
					     * 查询树列表
 | 
				
			||||||
     * <p>
 | 
					 | 
				
			||||||
     * 虽然提供了查询条件,但不建议使用,容易因缺失根节点导致树节点丢失。
 | 
					 | 
				
			||||||
     * 建议在前端进行查询过滤,如需使用建议重写方法。
 | 
					 | 
				
			||||||
     * </p>
 | 
					 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param query     查询条件
 | 
					     * @param query     查询条件
 | 
				
			||||||
     * @param sortQuery 排序查询条件
 | 
					     * @param sortQuery 排序查询条件
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,7 +22,6 @@ import cn.hutool.core.collection.CollUtil;
 | 
				
			|||||||
import cn.hutool.core.convert.Convert;
 | 
					import cn.hutool.core.convert.Convert;
 | 
				
			||||||
import cn.hutool.core.lang.tree.Tree;
 | 
					import cn.hutool.core.lang.tree.Tree;
 | 
				
			||||||
import cn.hutool.core.lang.tree.TreeNodeConfig;
 | 
					import cn.hutool.core.lang.tree.TreeNodeConfig;
 | 
				
			||||||
import cn.hutool.core.lang.tree.TreeUtil;
 | 
					 | 
				
			||||||
import cn.hutool.core.map.MapUtil;
 | 
					import cn.hutool.core.map.MapUtil;
 | 
				
			||||||
import cn.hutool.core.text.CharSequenceUtil;
 | 
					import cn.hutool.core.text.CharSequenceUtil;
 | 
				
			||||||
import cn.hutool.core.util.ReflectUtil;
 | 
					import cn.hutool.core.util.ReflectUtil;
 | 
				
			||||||
@@ -36,9 +35,13 @@ import org.springframework.transaction.annotation.Transactional;
 | 
				
			|||||||
import top.continew.starter.core.constant.StringConstants;
 | 
					import top.continew.starter.core.constant.StringConstants;
 | 
				
			||||||
import top.continew.starter.core.util.ClassUtils;
 | 
					import top.continew.starter.core.util.ClassUtils;
 | 
				
			||||||
import top.continew.starter.core.util.ReflectUtils;
 | 
					import top.continew.starter.core.util.ReflectUtils;
 | 
				
			||||||
 | 
					import top.continew.starter.core.util.TreeBuildUtils;
 | 
				
			||||||
 | 
					import top.continew.starter.core.util.validation.CheckUtils;
 | 
				
			||||||
 | 
					import top.continew.starter.core.util.validation.ValidationUtils;
 | 
				
			||||||
import top.continew.starter.data.mapper.BaseMapper;
 | 
					import top.continew.starter.data.mapper.BaseMapper;
 | 
				
			||||||
import top.continew.starter.data.service.impl.ServiceImpl;
 | 
					import top.continew.starter.data.service.impl.ServiceImpl;
 | 
				
			||||||
import top.continew.starter.data.util.QueryWrapperHelper;
 | 
					import top.continew.starter.data.util.QueryWrapperHelper;
 | 
				
			||||||
 | 
					import top.continew.starter.excel.util.ExcelUtils;
 | 
				
			||||||
import top.continew.starter.extension.crud.annotation.DictModel;
 | 
					import top.continew.starter.extension.crud.annotation.DictModel;
 | 
				
			||||||
import top.continew.starter.extension.crud.annotation.TreeField;
 | 
					import top.continew.starter.extension.crud.annotation.TreeField;
 | 
				
			||||||
import top.continew.starter.extension.crud.autoconfigure.CrudProperties;
 | 
					import top.continew.starter.extension.crud.autoconfigure.CrudProperties;
 | 
				
			||||||
@@ -48,12 +51,13 @@ import top.continew.starter.extension.crud.model.query.PageQuery;
 | 
				
			|||||||
import top.continew.starter.extension.crud.model.query.SortQuery;
 | 
					import top.continew.starter.extension.crud.model.query.SortQuery;
 | 
				
			||||||
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
 | 
					import top.continew.starter.extension.crud.model.resp.LabelValueResp;
 | 
				
			||||||
import top.continew.starter.extension.crud.model.resp.PageResp;
 | 
					import top.continew.starter.extension.crud.model.resp.PageResp;
 | 
				
			||||||
import top.continew.starter.excel.util.ExcelUtils;
 | 
					 | 
				
			||||||
import top.continew.starter.core.util.validation.CheckUtils;
 | 
					 | 
				
			||||||
import top.continew.starter.core.util.validation.ValidationUtils;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.lang.invoke.MethodHandleProxies;
 | 
				
			||||||
 | 
					import java.lang.invoke.MethodHandles;
 | 
				
			||||||
import java.lang.reflect.Field;
 | 
					import java.lang.reflect.Field;
 | 
				
			||||||
 | 
					import java.lang.reflect.Method;
 | 
				
			||||||
import java.util.*;
 | 
					import java.util.*;
 | 
				
			||||||
 | 
					import java.util.function.Function;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * CRUD 业务实现基类
 | 
					 * CRUD 业务实现基类
 | 
				
			||||||
@@ -100,20 +104,16 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseIdD
 | 
				
			|||||||
        CrudProperties crudProperties = SpringUtil.getBean(CrudProperties.class);
 | 
					        CrudProperties crudProperties = SpringUtil.getBean(CrudProperties.class);
 | 
				
			||||||
        CrudTreeProperties treeProperties = crudProperties.getTree();
 | 
					        CrudTreeProperties treeProperties = crudProperties.getTree();
 | 
				
			||||||
        TreeField treeField = listClass.getDeclaredAnnotation(TreeField.class);
 | 
					        TreeField treeField = listClass.getDeclaredAnnotation(TreeField.class);
 | 
				
			||||||
        TreeNodeConfig treeNodeConfig;
 | 
					 | 
				
			||||||
        Long rootId;
 | 
					 | 
				
			||||||
        // 简单树(下拉列表)使用全局配置结构,复杂树(表格)使用局部配置
 | 
					        // 简单树(下拉列表)使用全局配置结构,复杂树(表格)使用局部配置
 | 
				
			||||||
        if (isSimple) {
 | 
					        TreeNodeConfig treeNodeConfig = isSimple ? treeProperties.genTreeNodeConfig() : treeProperties.genTreeNodeConfig(treeField);
 | 
				
			||||||
            treeNodeConfig = treeProperties.genTreeNodeConfig();
 | 
					        String valueGetter = CharSequenceUtil.genGetter(treeField.value());
 | 
				
			||||||
            rootId = treeProperties.getRootId();
 | 
					        String parentIdKeyGetter = CharSequenceUtil.genGetter(treeField.parentIdKey());
 | 
				
			||||||
        } else {
 | 
					        Function<L, Long> getId = createMethodReference(listClass, valueGetter);
 | 
				
			||||||
            treeNodeConfig = treeProperties.genTreeNodeConfig(treeField);
 | 
					        Function<L, Long> getParentId = createMethodReference(listClass, parentIdKeyGetter);
 | 
				
			||||||
            rootId = treeField.rootId();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        // 构建树
 | 
					        // 构建树
 | 
				
			||||||
        return TreeUtil.build(list, rootId, treeNodeConfig, (node, tree) -> {
 | 
					        return TreeBuildUtils.buildMultiRoot(list, getId, getParentId, treeNodeConfig, (node, tree) -> {
 | 
				
			||||||
            tree.setId(ReflectUtil.invoke(node, CharSequenceUtil.genGetter(treeField.value())));
 | 
					            tree.setId(ReflectUtil.invoke(node, valueGetter));
 | 
				
			||||||
            tree.setParentId(ReflectUtil.invoke(node, CharSequenceUtil.genGetter(treeField.parentIdKey())));
 | 
					            tree.setParentId(ReflectUtil.invoke(node, parentIdKeyGetter));
 | 
				
			||||||
            tree.setName(ReflectUtil.invoke(node, CharSequenceUtil.genGetter(treeField.nameKey())));
 | 
					            tree.setName(ReflectUtil.invoke(node, CharSequenceUtil.genGetter(treeField.nameKey())));
 | 
				
			||||||
            tree.setWeight(ReflectUtil.invoke(node, CharSequenceUtil.genGetter(treeField.weightKey())));
 | 
					            tree.setWeight(ReflectUtil.invoke(node, CharSequenceUtil.genGetter(treeField.weightKey())));
 | 
				
			||||||
            // 如果构建简单树结构,则不包含扩展字段
 | 
					            // 如果构建简单树结构,则不包含扩展字段
 | 
				
			||||||
@@ -127,6 +127,26 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseIdD
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 通过反射创建方法引用
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param clazz      实体类类型
 | 
				
			||||||
 | 
					     * @param methodName 方法名
 | 
				
			||||||
 | 
					     * @param <T>        实体类类型
 | 
				
			||||||
 | 
					     * @param <K>        返回值类型
 | 
				
			||||||
 | 
					     * @return Function<T, K> 方法引用
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @SuppressWarnings("unchecked")
 | 
				
			||||||
 | 
					    public static <T, K> Function<T, K> createMethodReference(Class<T> clazz, String methodName) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            Method method = clazz.getDeclaredMethod(methodName);
 | 
				
			||||||
 | 
					            method.setAccessible(true);
 | 
				
			||||||
 | 
					            return MethodHandleProxies.asInterfaceInstance(Function.class, MethodHandles.lookup().unreflect(method));
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            throw new RuntimeException("Failed to create method reference for " + methodName, e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public D get(Long id) {
 | 
					    public D get(Long id) {
 | 
				
			||||||
        T entity = super.getById(id, false);
 | 
					        T entity = super.getById(id, false);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user