feat: 零部件树同步接口 同步完成后自动设置firstNode字段

This commit is contained in:
2024-02-26 15:55:46 +08:00
parent f7159831fe
commit efe8cd99b9
4 changed files with 38 additions and 24 deletions
@@ -60,7 +60,7 @@ public class LawsPartNameController {
@ApiOperation(value = "企业标准-零部件树历史数据处理", notes = "企业标准-零部件树历史数据处理")
@PostMapping("/hisDataFix")
public Result<List<PartName>> hisDataFix() {
partNameService.hisDataFix();
partNameService.fixFirstNodeCode();
return Result.OK();
}
@@ -34,5 +34,5 @@ public interface PartNameService extends IService<PartName> {
/**
* 零部件树历史数据处理
*/
void hisDataFix();
void fixFirstNodeCode();
}
@@ -140,36 +140,45 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
}
@Override
public void hisDataFix() {
List<PartName> allDataList = list();
// 遍历所有数据,填充firstNode字段
// 找到树结构中每个节点的一级节点
Map<String, PartName> nodeMap = allDataList.stream()
.collect(Collectors.toMap(PartName::getCode, Function.identity()));
public void fixFirstNodeCode() {
try {
List<PartName> allDataList = list();
// 遍历所有数据,填充firstNode字段
// 找到树结构中每个节点的一级节点
Map<String, PartName> nodeMap = allDataList.stream()
.collect(Collectors.toMap(PartName::getCode, Function.identity()));
// 构建树形结构
allDataList.forEach(node -> {
PartName parentNode = nodeMap.get(node.getParentCode());
if (parentNode != null) {
if (parentNode.getChildPartNameList() == null) {
parentNode.setChildPartNameList(new ArrayList<>());
List<PartName> needUpdateList = new ArrayList<>();
// 构建树形结构
allDataList.forEach(node -> {
PartName parentNode = nodeMap.get(node.getParentCode());
if (parentNode != null) {
if (parentNode.getChildPartNameList() == null) {
parentNode.setChildPartNameList(new ArrayList<>());
}
parentNode.getChildPartNameList().add(node);
}
parentNode.getChildPartNameList().add(node);
}
});
if (StrUtil.isBlank(node.getFirstNode()) &&
!node.getParentCode().equals("Part") &&
!node.getParentCode().equals("0")) {
needUpdateList.add(node);
}
});
// 数据整理
allDataList.forEach(node -> {
if (!node.getParentCode().equals("Part") && node.getFirstNode() == null) {
// 数据整理
needUpdateList.forEach(node -> {
PartName firstNode = this.getFirstLevelNode(node, nodeMap);
if (firstNode != null) {
node.setFirstNode(firstNode.getCode());
}
}
});
});
// 更新数据库
updateBatchById(allDataList);
// 更新数据库
updateBatchById(needUpdateList);
} catch (Exception e) {
log.error("零部件树设置firstNode字段数据异常", e);
}
}
private PartName getFirstLevelNode(PartName node, Map<String, PartName> nodeMap) {