From 53e810316c9f9a7512971c1ecff2261e1e14ad5c Mon Sep 17 00:00:00 2001 From: zch Date: Fri, 7 Feb 2025 16:44:24 +0800 Subject: [PATCH] =?UTF-8?q?change(mes):=20=E6=96=B0=E5=A2=9EBOM=E7=9A=84?= =?UTF-8?q?=E9=9D=9E=E9=A1=B6=E7=BA=A7=E8=8A=82=E7=82=B9=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?topFlag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化了顶级节点和子节点的处理流程 - 添加了 topFlag 字段,用于区分顶级节点和非顶级节点 --- .../impl/ProdMaterialBomServiceImpl.java | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/service/impl/ProdMaterialBomServiceImpl.java b/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/service/impl/ProdMaterialBomServiceImpl.java index a94b3a12..6767f87f 100644 --- a/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/service/impl/ProdMaterialBomServiceImpl.java +++ b/ruoyi-modules/hwmom-mes/src/main/java/org/dromara/mes/service/impl/ProdMaterialBomServiceImpl.java @@ -179,57 +179,48 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService { try { // 按层级分组(根据parentId分组) Map> levelMap = new HashMap<>(); - // 1. 首先找出所有顶级节点 List topNodes = boList.stream() .filter(bo -> bo.getParentId() == null || bo.getParentId() == 0) .collect(Collectors.toList()); - // 2. 其他节点按parentId分组 Map> childrenMap = boList.stream() .filter(bo -> bo.getParentId() != null && bo.getParentId() != 0) .collect(Collectors.groupingBy(bo -> Math.abs(bo.getParentId()))); - // 存储临时ID到实际ID的映射 Map idMapping = new HashMap<>(); - - // 3. 先处理顶级节点 + // 3. 优先处理顶级节点,设置基础数据并插入 for (ProdMaterialBomBo topNode : topNodes) { Long tempId = Math.abs(topNode.getMaterialBomId()); topNode.setMaterialBomId(null); // 清除临时ID topNode.setParentId(0L); // 确保顶级节点parentId为0 topNode.setAncestors("0"); // 顶级节点的ancestors为"0" - + topNode.setTopFlag(1L); // 设置顶级节点的topFlag为1 // 插入顶级节点 ProdMaterialBom add = MapstructUtils.convert(topNode, ProdMaterialBom.class); if (baseMapper.insert(add) <= 0) { throw new ServiceException("插入顶级节点失败"); } - // 记录映射关系 idMapping.put(tempId, add.getMaterialBomId()); } - // 4. 处理子节点,直到所有节点都处理完 while (!childrenMap.isEmpty()) { // 找出当前可以处理的节点(父节点已经在idMapping中的节点) List processableParentIds = new ArrayList<>(childrenMap.keySet()); - boolean processed = false; - + boolean processed = false;// 标记本轮是否有节点被处理 for (Long parentTempId : processableParentIds) { // 检查父节点是否已处理 if (idMapping.containsKey(parentTempId)) { List children = childrenMap.remove(parentTempId); - // 处理同一父节点的所有子节点 for (ProdMaterialBomBo child : children) { Long childTempId = Math.abs(child.getMaterialBomId()); - child.setMaterialBomId(null); // 清除临时ID - // 设置实际的父节点ID + child.setMaterialBomId(null);// 清除临时ID + child.setTopFlag(0L); // 设置非顶级节点的topFlag为0 Long actualParentId = idMapping.get(parentTempId); child.setParentId(actualParentId); - // 设置祖级列表 ProdMaterialBom parent = baseMapper.selectById(actualParentId); child.setAncestors(parent.getAncestors() + "," + actualParentId); @@ -246,13 +237,11 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService { processed = true; } } - // 如果一轮循环没有处理任何节点,说明数据有问题 if (!processed && !childrenMap.isEmpty()) { throw new ServiceException("存在无法处理的节点,可能是父节点丢失"); } } - return true; } catch (Exception e) { throw new ServiceException("批量插入失败: " + e.getMessage());