update 优化流程查询以及多根节点构建树结构

This commit is contained in:
AprilWind 2025-06-26 17:20:04 +08:00
parent eea96e87d9
commit 5a9728c868
3 changed files with 13 additions and 30 deletions

View File

@ -10,7 +10,6 @@ import lombok.NoArgsConstructor;
import org.dromara.common.core.utils.reflect.ReflectUtils; import org.dromara.common.core.utils.reflect.ReflectUtils;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -79,17 +78,10 @@ public class TreeBuildUtils extends TreeUtil {
return CollUtil.newArrayList(); return CollUtil.newArrayList();
} }
// 提取所有节点 ID用于后续判断哪些节点为根节点 parentId 不在其中 Set<K> rootParentIds = StreamUtils.toSet(list, getParentId);
Set<K> allIds = StreamUtils.toSet(list, getId); rootParentIds.removeAll(StreamUtils.toSet(list, getId));
// 筛选出所有 parentId 不在 allIds 中的节点这些节点的 parentId 可认为是根节点 // 构建每一个根 parentId 下的树并合并成最终结果列表
Set<K> rootParentIds = list.stream()
.map(getParentId)
.filter(Objects::nonNull)
.filter(pid -> !allIds.contains(pid))
.collect(Collectors.toSet());
// 使用流处理遍历每个顶级 parentId构建对应树并合并为一个列表返回
return rootParentIds.stream() return rootParentIds.stream()
.flatMap(rootParentId -> TreeUtil.build(list, rootParentId, parser).stream()) .flatMap(rootParentId -> TreeUtil.build(list, rootParentId, parser).stream())
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -4,14 +4,13 @@ import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
import cn.idev.excel.annotation.ExcelProperty; import cn.idev.excel.annotation.ExcelProperty;
import io.github.linpeilie.annotations.AutoMapper; import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data; import lombok.Data;
import org.dromara.common.translation.annotation.Translation;
import org.dromara.workflow.common.constant.FlowConstant;
import org.dromara.workflow.domain.FlowCategory; import org.dromara.workflow.domain.FlowCategory;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* 流程分类视图对象 wf_category * 流程分类视图对象 wf_category
@ -34,13 +33,14 @@ public class FlowCategoryVo implements Serializable {
private Long categoryId; private Long categoryId;
/** /**
* 父级id * 父级分类id
*/ */
private Long parentId; private Long parentId;
/** /**
* 名称 * 级分类名称
*/ */
@Translation(type = FlowConstant.CATEGORY_ID_TO_NAME, mapper = "parentId")
private String parentName; private String parentName;
/** /**
@ -66,9 +66,4 @@ public class FlowCategoryVo implements Serializable {
@ExcelProperty(value = "创建时间") @ExcelProperty(value = "创建时间")
private Date createTime; private Date createTime;
/**
* 子菜单
*/
private List<FlowCategoryVo> children = new ArrayList<>();
} }

View File

@ -8,7 +8,10 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.SystemConstants; import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.*; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.ObjectUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.TreeBuildUtils;
import org.dromara.common.mybatis.helper.DataBaseHelper; import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.warm.flow.core.service.DefService; import org.dromara.warm.flow.core.service.DefService;
@ -48,14 +51,7 @@ public class FlwCategoryServiceImpl implements IFlwCategoryService {
*/ */
@Override @Override
public FlowCategoryVo queryById(Long categoryId) { public FlowCategoryVo queryById(Long categoryId) {
FlowCategoryVo category = baseMapper.selectVoById(categoryId); return baseMapper.selectVoById(categoryId);
if (ObjectUtil.isNull(category)) {
return null;
}
FlowCategoryVo parentCategory = baseMapper.selectVoOne(new LambdaQueryWrapper<FlowCategory>()
.select(FlowCategory::getCategoryName).eq(FlowCategory::getCategoryId, category.getParentId()));
category.setParentName(ObjectUtils.notNullGetter(parentCategory, FlowCategoryVo::getCategoryName));
return category;
} }
/** /**