update 优化构建多根节点的树结构(支持多个顶级节点)

This commit is contained in:
AprilWind
2025-06-26 14:36:18 +08:00
committed by 疯狂的狮子Li
parent e659740cb8
commit eea96e87d9
3 changed files with 58 additions and 36 deletions

View File

@ -95,27 +95,20 @@ public class FlwCategoryServiceImpl implements IFlwCategoryService {
*/
@Override
public List<Tree<String>> selectCategoryTreeList(FlowCategoryBo category) {
LambdaQueryWrapper<FlowCategory> lqw = buildQueryWrapper(category);
List<FlowCategoryVo> categorys = baseMapper.selectVoList(lqw);
if (CollUtil.isEmpty(categorys)) {
List<FlowCategoryVo> categoryList = this.queryList(category);
if (CollUtil.isEmpty(categoryList)) {
return CollUtil.newArrayList();
}
// 获取当前列表中每一个节点的parentId然后在列表中查找是否有id与其parentId对应若无对应则表明此时节点列表中该节点在当前列表中属于顶级节点
List<Tree<String>> treeList = CollUtil.newArrayList();
for (FlowCategoryVo d : categorys) {
String parentId = d.getParentId().toString();
FlowCategoryVo categoryVo = StreamUtils.findFirst(categorys, it -> it.getCategoryId().toString().equals(parentId));
if (ObjectUtil.isNull(categoryVo)) {
List<Tree<String>> trees = TreeBuildUtils.build(categorys, parentId, (dept, tree) ->
tree.setId(dept.getCategoryId().toString())
.setParentId(dept.getParentId().toString())
.setName(dept.getCategoryName())
.setWeight(dept.getOrderNum()));
Tree<String> tree = StreamUtils.findFirst(trees, it -> it.getId().equals(d.getCategoryId().toString()));
treeList.add(tree);
}
}
return treeList;
return TreeBuildUtils.buildMultiRoot(
categoryList,
node -> String.valueOf(node.getCategoryId()),
node -> String.valueOf(node.getParentId()),
(node, treeNode) -> treeNode
.setId(String.valueOf(node.getCategoryId()))
.setParentId(String.valueOf(node.getParentId()))
.setName(node.getCategoryName())
.setWeight(node.getOrderNum())
);
}
/**