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
+7
View File
@@ -195,3 +195,10 @@ INSERT INTO `laws_tag` (`id`, `table_name`, `db_field_name`, `db_field_en_name`,
INSERT INTO `laws_tag` (`id`, `table_name`, `db_field_name`, `db_field_en_name`, `db_field_txt`, `order_num`, `db_field_name_old`, `db_is_key`, `db_is_null`, `db_type`, `db_length`, `db_point_length`, `db_default_val`, `dict_field`, `dict_table`, `dict_text`, `field_show_type`, `field_href`, `field_length`, `field_valid_type`, `field_must_input`, `field_extend_json`, `field_default_value`, `is_show_form_create`, `is_query`, `is_show_form`, `is_show_list`, `is_read_only`, `query_mode`, `main_table`, `main_field`, `update_by`, `update_time`, `create_time`, `create_by`, `converter`, `query_def_val`, `query_dict_text`, `query_dict_field`, `query_dict_table`, `query_show_type`, `query_config_flag`, `query_valid_type`, `query_must_input`, `sort_flag`, `show_area`, `is_show_laws_list`, `is_show_search`, `del_flag`, `is_model`, `dict_id`, `home_search_context_order`, `home_search_file_order`, `dict_field_search`, `dict_table_search`, `dict_text_search`, `usable_range`) VALUES ('1818916543200575489', 'laws_domestic_standard', 'new_model_implementation_date_remarks', 'New model implementation date Remarks', '新车型实施日期备注', 12, 'new_model_implementation_date_remarks', 0, 1, 'String', 500, NULL, NULL, NULL, NULL, NULL, '5', NULL, NULL, NULL, '0', NULL, NULL, 1, 0, 1, 0, 0, NULL, NULL, NULL, NULL, NULL, '2024-08-01 15:47:52', 'ldq1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '0', '1499657602378825729', NULL, 0, 0, '1', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `laws_tag` (`id`, `table_name`, `db_field_name`, `db_field_en_name`, `db_field_txt`, `order_num`, `db_field_name_old`, `db_is_key`, `db_is_null`, `db_type`, `db_length`, `db_point_length`, `db_default_val`, `dict_field`, `dict_table`, `dict_text`, `field_show_type`, `field_href`, `field_length`, `field_valid_type`, `field_must_input`, `field_extend_json`, `field_default_value`, `is_show_form_create`, `is_query`, `is_show_form`, `is_show_list`, `is_read_only`, `query_mode`, `main_table`, `main_field`, `update_by`, `update_time`, `create_time`, `create_by`, `converter`, `query_def_val`, `query_dict_text`, `query_dict_field`, `query_dict_table`, `query_show_type`, `query_config_flag`, `query_valid_type`, `query_must_input`, `sort_flag`, `show_area`, `is_show_laws_list`, `is_show_search`, `del_flag`, `is_model`, `dict_id`, `home_search_context_order`, `home_search_file_order`, `dict_field_search`, `dict_table_search`, `dict_text_search`, `usable_range`) VALUES ('1818916929537916923', 'laws_overseas_standard', 'on_the_production_date_remarks', 'Remarks on date of implementation in production ve', '在产车实施日期备注', 13, 'on_the_production_date_remarks', 0, 1, 'String', 500, NULL, NULL, NULL, NULL, NULL, '5', NULL, NULL, NULL, '0', NULL, NULL, 1, 0, 1, 0, 0, NULL, NULL, NULL, NULL, NULL, '2024-08-01 15:49:25', 'ldq1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '0', '1694630597254553601', NULL, 0, 0, '1', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `laws_tag` (`id`, `table_name`, `db_field_name`, `db_field_en_name`, `db_field_txt`, `order_num`, `db_field_name_old`, `db_is_key`, `db_is_null`, `db_type`, `db_length`, `db_point_length`, `db_default_val`, `dict_field`, `dict_table`, `dict_text`, `field_show_type`, `field_href`, `field_length`, `field_valid_type`, `field_must_input`, `field_extend_json`, `field_default_value`, `is_show_form_create`, `is_query`, `is_show_form`, `is_show_list`, `is_read_only`, `query_mode`, `main_table`, `main_field`, `update_by`, `update_time`, `create_time`, `create_by`, `converter`, `query_def_val`, `query_dict_text`, `query_dict_field`, `query_dict_table`, `query_show_type`, `query_config_flag`, `query_valid_type`, `query_must_input`, `sort_flag`, `show_area`, `is_show_laws_list`, `is_show_search`, `del_flag`, `is_model`, `dict_id`, `home_search_context_order`, `home_search_file_order`, `dict_field_search`, `dict_table_search`, `dict_text_search`, `usable_range`) VALUES ('1818916929537916929', 'laws_domestic_standard', 'on_the_production_date_remarks', 'Remarks on date of implementation in production ve', '在产车实施日期备注', 12, 'on_the_production_date_remarks', 0, 1, 'String', 500, NULL, NULL, NULL, NULL, NULL, '5', NULL, NULL, NULL, '0', NULL, NULL, 1, 0, 1, 0, 0, NULL, NULL, NULL, NULL, NULL, '2024-08-01 15:49:25', 'ldq1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '0', '1499657602378825729', NULL, 0, 0, '1', NULL, NULL, NULL, NULL, NULL, NULL, NULL);
-- 2024-08-12 零部件性能优化
ALTER TABLE `laws_tag`
ADD COLUMN `tree_first_href` varchar(255) NULL DEFAULT NULL COMMENT '懒加载树形结构一级接口' AFTER `usable_range`;
UPDATE `laws_tag` SET `field_show_type` = '17', `tree_first_href` = '/laws/standard/partName/queryFirstList' WHERE `id` = '1696038850540003301';
UPDATE `laws_tag` SET `field_show_type` = '17', `tree_first_href` = '/laws/standard/partName/queryFirstList' WHERE `id` = '1696038850540003330';
UPDATE `laws_tag` SET `field_show_type` = '17', `tree_first_href` = '/laws/standard/partName/queryFirstList' WHERE `id` = '1703608885251108865';
@@ -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);
}
}