fix: 通用懒加载树形搜索

This commit is contained in:
2024-08-28 17:07:47 +08:00
parent 83724c5bd7
commit babb1108b3
4 changed files with 45 additions and 113 deletions
@@ -197,6 +197,13 @@ public class SysDictItem implements TreeNode {
@JsonProperty("isLeaf")
private boolean leafFlag;
/**
* 第一级节点
*/
@TableField(exist = false)
@ApiModelProperty(value = "第一级节点")
private String firstNode;
@Override
public String obtainTreeNodeId() {
return id;
@@ -33,7 +33,7 @@ public class LawsDictTreeController {
public Result<List<SysDictItem>> queryTreeList(
@RequestParam(name = "nodeName", required = false, defaultValue = "") @ApiParam("节点名称") String nodeName,
@RequestParam("dictId") @ApiParam("字典编码") String dictId) {
List<SysDictItem> list = dictTreeService.queryTreeList(nodeName, dictId);
List<SysDictItem> list = dictTreeService.getTree("0", dictId, nodeName);
return Result.OK(list);
}
@@ -51,7 +51,7 @@ public class LawsDictTreeController {
public Result<List<SysDictItem>> getChild(@RequestParam("dictItemId") String dictItemId,
@RequestParam("dictId") @ApiParam("字典编码") String dictId,
@RequestParam("nodeName") String nodeName) {
return Result.OK(dictTreeService.getChild(dictItemId, dictId, nodeName));
return Result.OK(dictTreeService.getTree(dictItemId, dictId, nodeName));
}
@@ -1,5 +1,6 @@
package com.jero.modules.laws.standard.service;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.modules.system.entity.SysDictItem;
import com.jero.modules.system.service.ISysDictItemService;
@@ -7,6 +8,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@@ -15,119 +18,60 @@ public class DictTreeService {
@Resource
private ISysDictItemService dictItemService;
public List<SysDictItem> queryTreeList(String nodeName, String dictId) {
public List<SysDictItem> getTree(String dictItemId, String dictId, String nodeName) {
// 查询所有节点
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysDictItem::getDictId, dictId);
List<SysDictItem> allNodes = dictItemService.list(queryWrapper);
// 通过 nodeName 过滤符合条件的节点
List<SysDictItem> matchedNodes = allNodes.stream()
// 过滤符合条件的节点
List<SysDictItem> matchedNodes = allNodes.parallelStream()
.filter(node -> node.getItemText() != null && node.getItemText().contains(nodeName))
.collect(Collectors.toList());
// 构建节点映射,以及父子关系映射
Map<String, SysDictItem> nodeMap = allNodes.parallelStream()
.collect(Collectors.toConcurrentMap(SysDictItem::getId, Function.identity()));
Map<String, List<SysDictItem>> parentChildMap = matchedNodes.parallelStream()
.collect(Collectors.groupingByConcurrent(SysDictItem::getParentId));
if (matchedNodes.isEmpty()) {
return Collections.emptyList();
}
// 用于存储一级节点
Set<SysDictItem> rootNodes = new HashSet<>();
// 查找每个匹配节点的一级父节点
for (SysDictItem matchedNode : matchedNodes) {
SysDictItem currentNode = matchedNode;
while (currentNode != null && !currentNode.getParentId().equals("0")) {
// 查找当前节点的父节点
String parentId = currentNode.getParentId();
currentNode = allNodes.stream()
.filter(node -> node.getId().equals(parentId))
.findFirst()
.orElse(null);
}
// 添加一级节点到结果集
if (currentNode != null) {
rootNodes.add(currentNode);
}
}
// 设置叶子标志的逻辑调整
for (SysDictItem node : rootNodes) {
Set<String> descendantIds = getAllDescendantIds(node.getId(), allNodes); // 获取所有子孙节点的ID
boolean isLeaf = true; // 默认设置为叶子节点
for (SysDictItem matchedNode : matchedNodes) {
if (descendantIds.contains(matchedNode.getId())) {
isLeaf = false;
break; // 如果在子孙节点中找到匹配节点,当前根节点不是叶子节点
}
}
node.setLeafFlag(isLeaf); // 设置叶子标志
}
return new ArrayList<>(rootNodes);
}
// 方法:获取给定节点ID的所有子孙节点ID
private Set<String> getAllDescendantIds(String parentId, List<SysDictItem> allNodes) {
Set<String> descendantIds = new HashSet<>();
List<SysDictItem> directChildren = allNodes.stream()
.filter(node -> node.getParentId().equals(parentId))
.collect(Collectors.toList());
for (SysDictItem child : directChildren) {
descendantIds.add(child.getId());
descendantIds.addAll(getAllDescendantIds(child.getId(), allNodes)); // 递归获取子孙节点
}
return descendantIds;
}
public List<SysDictItem> getChild(String dictItemId, String dictId, String nodeName) {
// 查询所有节点
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysDictItem::getDictId, dictId);
List<SysDictItem> allNodes = dictItemService.list(queryWrapper);
// 通过 nodeName 过滤符合条件的节点
List<SysDictItem> matchedNodes = allNodes.stream()
.filter(node -> node.getItemText() != null && node.getItemText().contains(nodeName))
.collect(Collectors.toList());
if (matchedNodes.isEmpty()) {
return Collections.emptyList();
}
// 用于存储一级节点
Set<SysDictItem> rootNodes = new HashSet<>();
// 查找每个匹配节点的一级父节点
for (SysDictItem matchedNode : matchedNodes) {
Set<SysDictItem> rootNodes = ConcurrentHashMap.newKeySet();
Set<String> allParentNodeIds = ConcurrentHashMap.newKeySet();
matchedNodes.parallelStream().forEach(matchedNode -> {
SysDictItem currentNode = matchedNode;
while (currentNode != null && !currentNode.getParentId().equals(dictItemId)) {
// 查找当前节点的父节点
String parentId = currentNode.getParentId();
currentNode = allNodes.stream()
.filter(node -> node.getId().equals(parentId))
.findFirst()
.orElse(null);
currentNode = nodeMap.get(currentNode.getParentId());
if (currentNode != null) {
allParentNodeIds.add(currentNode.getId());
}
}
// 添加一级节点到结果集
if (currentNode != null) {
rootNodes.add(currentNode);
}
}
// 设置叶子标志
Set<String> rootNodeIds = rootNodes.stream().map(SysDictItem::getId).collect(Collectors.toSet());
rootNodes.forEach(node -> {
node.setLeafFlag(matchedNodes.stream()
.noneMatch(n -> (rootNodeIds.contains(n.getParentId()) &&
n.getParentId().equals(node.getId()))));
});
return new ArrayList<>(rootNodes);
// 设置叶子标志
rootNodes.parallelStream().forEach(rootNode -> {
List<SysDictItem> children = parentChildMap.get(rootNode.getId());
boolean hasNoChildren = (children == null || children.isEmpty());
if (StrUtil.isBlank(nodeName)) {
rootNode.setLeafFlag(hasNoChildren);
} else {
boolean isNotInAllParents = !allParentNodeIds.contains(rootNode.getId());
rootNode.setLeafFlag(hasNoChildren && isNotInAllParents);
}
});
// 按名字排序
return rootNodes.stream()
.sorted(Comparator.comparing(SysDictItem::getSortOrder))
.collect(Collectors.toList());
}
public List<SysDictItem> queryEcho(String selectNodeCodes, String dictId) {
@@ -182,21 +182,6 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
.collect(Collectors.toList());
}
// 方法:获取给定节点ID的所有子孙节点ID
private Set<String> getAllDescendantCodes(String parentCode, List<PartName> allNodes) {
Set<String> descendantCodes = new HashSet<>();
List<PartName> directChildren = allNodes.stream()
.filter(node -> node.getParentCode().equals(parentCode))
.collect(Collectors.toList());
for (PartName child : directChildren) {
descendantCodes.add(child.getCode());
descendantCodes.addAll(getAllDescendantCodes(child.getCode(), allNodes)); // 递归获取子孙节点
}
return descendantCodes;
}
@Override
public List<PartName> getChild(String parentCode, String nodeName) {
// 查询所有节点
@@ -214,10 +199,6 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
.filter(node -> node.getName() != null && node.getName().contains(nodeName))
.collect(Collectors.toList());
if (matchedNodes.isEmpty()) {
return Collections.emptyList();
}
// 查找每个匹配节点的一级父节点
Set<PartName> rootNodes = ConcurrentHashMap.newKeySet();
matchedNodes.parallelStream().forEach(matchedNode -> {