perf: 零部件性能优化(懒加载)

This commit is contained in:
2024-08-12 15:48:56 +08:00
parent d08cc65395
commit 184d39577b
5 changed files with 153 additions and 8 deletions
@@ -1,10 +1,7 @@
package com.jero.modules.laws.standard.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.AutoLog;
import com.jero.modules.laws.enterprise.entity.vo.LawsTreeNodeModel;
import com.jero.modules.laws.standard.entity.LawsTreeNode;
import com.jero.modules.laws.standard.entity.PartName;
import com.jero.modules.laws.standard.entity.TreePage;
import com.jero.modules.laws.standard.service.PartNameService;
@@ -64,5 +61,22 @@ public class LawsPartNameController {
return Result.OK();
}
@AutoLog(value = "企业标准-零部件树")
@ApiOperation(value = "企业标准-零部件树", notes = "企业标准-零部件树")
@GetMapping("/queryFirstList")
public Result<List<PartName>> queryFirstList(
@RequestParam(name = "nodeName", required = false, defaultValue = "") @ApiParam("节点名称") String nodeName) {
List<PartName> list = partNameService.queryList(nodeName);
return Result.OK(list);
}
@AutoLog(value = "企业标准-获取子集")
@ApiOperation(value = "企业标准-获取子集", notes = "企业标准-获取子集")
@GetMapping("/getChild")
public Result<List<PartName>> getChild(@RequestParam("parentCode") String parentCode,
@RequestParam("nodeName") String nodeName) {
return Result.OK(partNameService.getChild(parentCode, nodeName));
}
}
@@ -4,17 +4,17 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* @TableName laws_part_name
*/
@@ -107,6 +107,14 @@ public class PartName implements Serializable {
@ApiModelProperty(value = "备用字段3")
private String remark3;
/**
* 是否是叶子节点
*/
@ApiModelProperty(value = "是否是叶子节点")
@TableField(exist = false)
@JsonProperty("isLeaf")
private boolean leafFlag;
/**
* 子零部件
*/
@@ -35,4 +35,8 @@ public interface PartNameService extends IService<PartName> {
* 零部件树历史数据处理
*/
void fixFirstNodeCode();
List<PartName> queryList(String nodeName);
List<PartName> getChild(String parentCode, String nodeName);
}
@@ -193,6 +193,118 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
}
@Override
public List<PartName> queryList(String nodeName) {
// 查询所有节点
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
List<PartName> allNodes = list(queryWrapper);
// 通过 nodeName 过滤符合条件的节点
List<PartName> matchedNodes = allNodes.stream()
.filter(node -> node.getName() != null && node.getName().contains(nodeName))
.collect(Collectors.toList());
if (matchedNodes.isEmpty()) {
return Collections.emptyList();
}
// 用于存储一级节点
Set<PartName> rootNodes = new HashSet<>();
// 查找每个匹配节点的一级父节点
for (PartName matchedNode : matchedNodes) {
PartName currentNode = matchedNode;
while (currentNode != null && !currentNode.getParentCode().equals("Part")) {
// 查找当前节点的父节点
String parentCode = currentNode.getParentCode();
currentNode = allNodes.stream()
.filter(node -> node.getCode().equals(parentCode))
.findFirst()
.orElse(null);
}
// 添加一级节点到结果集
if (currentNode != null) {
rootNodes.add(currentNode);
}
}
// 设置叶子标志的逻辑调整
for (PartName node : rootNodes) {
Set<String> descendantCodes = getAllDescendantCodes(node.getCode(), allNodes); // 获取所有子孙节点的ID
boolean isLeaf = true; // 默认设置为叶子节点
for (PartName matchedNode : matchedNodes) {
if (descendantCodes.contains(matchedNode.getCode())) {
isLeaf = false;
break; // 如果在子孙节点中找到匹配节点,当前根节点不是叶子节点
}
}
node.setLeafFlag(isLeaf); // 设置叶子标志
}
return new ArrayList<>(rootNodes);
}
// 方法:获取给定节点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) {
// 查询所有节点
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
List<PartName> allNodes = list(queryWrapper);
// 通过 nodeName 过滤符合条件的节点
List<PartName> matchedNodes = allNodes.stream()
.filter(node -> node.getName() != null && node.getName().contains(nodeName))
.collect(Collectors.toList());
if (matchedNodes.isEmpty()) {
return Collections.emptyList();
}
// 用于存储一级节点
Set<PartName> rootNodes = new HashSet<>();
// 查找每个匹配节点的一级父节点
for (PartName matchedNode : matchedNodes) {
PartName currentNode = matchedNode;
while (currentNode != null && !currentNode.getParentCode().equals(parentCode)) {
// 查找当前节点的父节点
String pCode = currentNode.getParentCode();
currentNode = allNodes.stream()
.filter(node -> node.getCode().equals(pCode))
.findFirst()
.orElse(null);
}
// 添加一级节点到结果集
if (currentNode != null) {
rootNodes.add(currentNode);
}
}
// 设置叶子标志
Set<String> rootNodeCodes = rootNodes.stream().map(PartName::getCode).collect(Collectors.toSet());
rootNodes.forEach(node -> {
node.setLeafFlag(matchedNodes.stream()
.noneMatch(n -> rootNodeCodes.contains(n.getParentCode())));
});
return new ArrayList<>(rootNodes);
}
}