fix: 零部件懒加载树形搜索
This commit is contained in:
+2
-2
@@ -40,7 +40,7 @@ public class LawsPartNameController {
|
||||
@GetMapping("/queryFirstList")
|
||||
public Result<List<PartName>> queryFirstList(
|
||||
@RequestParam(name = "nodeName", required = false, defaultValue = "") @ApiParam("节点名称") String nodeName) {
|
||||
List<PartName> list = IPartNameService.queryList(nodeName);
|
||||
List<PartName> list = IPartNameService.getTree("Part", nodeName);
|
||||
return Result.OK(list);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class LawsPartNameController {
|
||||
@GetMapping("/getChild")
|
||||
public Result<List<PartName>> getChild(@RequestParam("parentCode") String parentCode,
|
||||
@RequestParam("nodeName") String nodeName) {
|
||||
return Result.OK(IPartNameService.getChild(parentCode, nodeName));
|
||||
return Result.OK(IPartNameService.getTree(parentCode, nodeName));
|
||||
}
|
||||
|
||||
@AutoLog(value = "企业标准-零部件树历史数据处理")
|
||||
|
||||
+4
-3
@@ -32,7 +32,8 @@ public interface ILawsPartNameService extends IService<PartName> {
|
||||
*/
|
||||
void fixFirstNodeCode();
|
||||
|
||||
List<PartName> queryList(String nodeName);
|
||||
|
||||
List<PartName> getChild(String parentCode, String nodeName);
|
||||
/**
|
||||
* 零部件树懒加载
|
||||
*/
|
||||
List<PartName> getTree(String parentCode, String nodeName);
|
||||
}
|
||||
|
||||
+21
-50
@@ -140,71 +140,35 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PartName> queryList(String nodeName) {
|
||||
public List<PartName> getTree(String parentCode, String nodeName) {
|
||||
// 查询所有节点
|
||||
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
|
||||
List<PartName> allNodes = list(queryWrapper);
|
||||
|
||||
// 通过 nodeName 过滤符合条件的节点
|
||||
Map<String, PartName> nodeMap = allNodes.parallelStream()
|
||||
.collect(Collectors.toConcurrentMap(PartName::getCode, Function.identity()));
|
||||
Map<String, List<PartName>> parentChildMap = allNodes.parallelStream()
|
||||
.collect(Collectors.groupingByConcurrent(PartName::getParentCode));
|
||||
List<PartName> allNodes = list();
|
||||
|
||||
// 过滤符合条件的节点
|
||||
List<PartName> matchedNodes = allNodes.parallelStream()
|
||||
.filter(node -> node.getName() != null && node.getName().contains(nodeName))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建节点映射,以及父子关系映射
|
||||
Map<String, PartName> nodeMap = allNodes.parallelStream()
|
||||
.collect(Collectors.toConcurrentMap(PartName::getCode, Function.identity()));
|
||||
Map<String, List<PartName>> parentChildMap = matchedNodes.parallelStream()
|
||||
.collect(Collectors.groupingByConcurrent(PartName::getParentCode));
|
||||
|
||||
if (matchedNodes.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 查找每个匹配节点的一级父节点
|
||||
Set<PartName> rootNodes = ConcurrentHashMap.newKeySet();
|
||||
matchedNodes.parallelStream().forEach(matchedNode -> {
|
||||
PartName currentNode = matchedNode;
|
||||
while (currentNode != null && !currentNode.getParentCode().equals("Part")) {
|
||||
currentNode = nodeMap.get(currentNode.getParentCode());
|
||||
}
|
||||
if (currentNode != null) {
|
||||
rootNodes.add(currentNode);
|
||||
}
|
||||
});
|
||||
|
||||
// 设置叶子标志
|
||||
rootNodes.parallelStream().forEach(rootNode -> {
|
||||
List<PartName> children = parentChildMap.get(rootNode.getCode());
|
||||
rootNode.setLeafFlag(children == null || children.isEmpty());
|
||||
});
|
||||
|
||||
return rootNodes.stream()
|
||||
.sorted(Comparator.comparing(PartName::getName))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PartName> getChild(String parentCode, String nodeName) {
|
||||
// 查询所有节点
|
||||
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
|
||||
List<PartName> allNodes = list(queryWrapper);
|
||||
|
||||
// 构建节点映射,以及父子关系映射
|
||||
Map<String, PartName> nodeMap = allNodes.parallelStream()
|
||||
.collect(Collectors.toConcurrentMap(PartName::getCode, Function.identity()));
|
||||
Map<String, List<PartName>> parentChildMap = allNodes.parallelStream()
|
||||
.collect(Collectors.groupingByConcurrent(PartName::getParentCode));
|
||||
|
||||
// 过滤符合条件的节点
|
||||
List<PartName> matchedNodes = allNodes.parallelStream()
|
||||
.filter(node -> node.getName() != null && node.getName().contains(nodeName))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查找每个匹配节点的一级父节点
|
||||
Set<PartName> rootNodes = ConcurrentHashMap.newKeySet();
|
||||
Set<String> allParentNodeCodes = ConcurrentHashMap.newKeySet();
|
||||
matchedNodes.parallelStream().forEach(matchedNode -> {
|
||||
PartName currentNode = matchedNode;
|
||||
while (currentNode != null && !currentNode.getParentCode().equals(parentCode)) {
|
||||
currentNode = nodeMap.get(currentNode.getParentCode());
|
||||
if (currentNode != null) {
|
||||
allParentNodeCodes.add(currentNode.getCode());
|
||||
}
|
||||
}
|
||||
if (currentNode != null) {
|
||||
rootNodes.add(currentNode);
|
||||
@@ -214,7 +178,14 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
|
||||
// 设置叶子标志
|
||||
rootNodes.parallelStream().forEach(rootNode -> {
|
||||
List<PartName> children = parentChildMap.get(rootNode.getCode());
|
||||
rootNode.setLeafFlag(children == null || children.isEmpty());
|
||||
boolean hasNoChildren = (children == null || children.isEmpty());
|
||||
|
||||
if (StrUtil.isBlank(nodeName)) {
|
||||
rootNode.setLeafFlag(hasNoChildren);
|
||||
} else {
|
||||
boolean isNotInAllParents = !allParentNodeCodes.contains(rootNode.getCode());
|
||||
rootNode.setLeafFlag(hasNoChildren && isNotInAllParents);
|
||||
}
|
||||
});
|
||||
|
||||
// 按名字排序
|
||||
|
||||
Reference in New Issue
Block a user