feat:后端树形接口

This commit is contained in:
2026-01-20 16:23:54 +08:00
parent 53a3b96fd3
commit 4a0ef377bb
2 changed files with 84 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ package org.nl.pmm.modular.stagedetail.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -32,6 +33,8 @@ import org.nl.pmm.modular.stagedetail.mapper.StageDetailMapper;
import org.nl.pmm.modular.stagedetail.service.StageDetailService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 项目阶段明细Service接口实现类
@@ -113,6 +116,78 @@ public class StageDetailServiceImpl extends ServiceImpl<StageDetailMapper, Stage
*/
@Override
public List<TreeStageDetailVo> getSuperior(StageDetailTreeParam detailTreeParam) {
return List.of();
// 查询当前阶段下的所有节点
LambdaQueryWrapper<StageDetail> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(StageDetail::getStageId, detailTreeParam.getStageId())
.orderByAsc(StageDetail::getSeq, StageDetail::getDetailId);
List<StageDetail> allDetails = this.list(wrapper);
if(CollUtil.isEmpty(allDetails)) {
return List.of();
}
// 构建 id -> 节点、 parentId -> children 映射
Map<String, TreeStageDetailVo> idNodeMap = allDetails.stream()
.map(item -> BeanUtil.copyProperties(item, TreeStageDetailVo.class))
.collect(Collectors.toMap(TreeStageDetailVo::getDetailId, item -> item));
Map<String, List<TreeStageDetailVo>> parentChildrenMap = idNodeMap.values().stream()
.collect(Collectors.groupingBy(node -> StrUtil.emptyToNull(node.getParentDetailId())));
// 组装 children
idNodeMap.values().forEach(node -> {
List<TreeStageDetailVo> children = parentChildrenMap.getOrDefault(node.getDetailId(), List.of());
node.setChildren(children);
});
// 根节点
List<TreeStageDetailVo> rootNodes = parentChildrenMap.getOrDefault(null, List.of());
// 无 parentDetailId 表示取顶层
if(StrUtil.isBlank(detailTreeParam.getParentDetailId())) {
rootNodes.forEach(node -> node.setIsLeaf(CollUtil.isEmpty(node.getChildren())));
return rootNodes;
}
// 计算祖先链(包含目标父节点)
String targetParentId = detailTreeParam.getParentDetailId();
List<String> ancestorIds = CollUtil.newArrayList();
String cursor = targetParentId;
while(StrUtil.isNotBlank(cursor)) {
ancestorIds.add(cursor);
TreeStageDetailVo current = idNodeMap.get(cursor);
if(current == null || StrUtil.isBlank(current.getParentDetailId())) {
cursor = null;
} else {
cursor = current.getParentDetailId();
}
}
// 构建仅保留祖先链上下级关系的树(顶层节点默认展开一层子节点)
return rootNodes.stream()
.map(node -> buildPrunedTree(node, ancestorIds, targetParentId, parentChildrenMap))
.peek(node -> node.setIsLeaf(CollUtil.isEmpty(node.getChildren())))
.collect(Collectors.toList());
}
/**
* 仅保留祖先链上节点的子集,顶层节点默认展开一层,其余节点不展开 children。
*/
private TreeStageDetailVo buildPrunedTree(TreeStageDetailVo current,
List<String> ancestorIds,
String targetParentId,
Map<String, List<TreeStageDetailVo>> parentChildrenMap) {
TreeStageDetailVo copy = BeanUtil.copyProperties(current, TreeStageDetailVo.class);
boolean expandChildren = ancestorIds.contains(current.getDetailId()) || StrUtil.isBlank(current.getParentDetailId());
if(expandChildren) {
List<TreeStageDetailVo> children = parentChildrenMap.getOrDefault(current.getDetailId(), List.of())
.stream()
.map(child -> buildPrunedTree(child, ancestorIds, targetParentId, parentChildrenMap))
.collect(Collectors.toList());
copy.setChildren(children);
} else {
copy.setChildren(List.of());
}
copy.setIsLeaf(CollUtil.isEmpty(copy.getChildren()));
return copy;
}
}