change(mes): 新增BOM的非顶级节点表示topFlag

- 优化了顶级节点和子节点的处理流程
- 添加了 topFlag 字段,用于区分顶级节点和非顶级节点
master
zch 2 weeks ago
parent 9572796335
commit 53e810316c

@ -179,57 +179,48 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
try { try {
// 按层级分组根据parentId分组 // 按层级分组根据parentId分组
Map<Long, List<ProdMaterialBomBo>> levelMap = new HashMap<>(); Map<Long, List<ProdMaterialBomBo>> levelMap = new HashMap<>();
// 1. 首先找出所有顶级节点 // 1. 首先找出所有顶级节点
List<ProdMaterialBomBo> topNodes = boList.stream() List<ProdMaterialBomBo> topNodes = boList.stream()
.filter(bo -> bo.getParentId() == null || bo.getParentId() == 0) .filter(bo -> bo.getParentId() == null || bo.getParentId() == 0)
.collect(Collectors.toList()); .collect(Collectors.toList());
// 2. 其他节点按parentId分组 // 2. 其他节点按parentId分组
Map<Long, List<ProdMaterialBomBo>> childrenMap = boList.stream() Map<Long, List<ProdMaterialBomBo>> childrenMap = boList.stream()
.filter(bo -> bo.getParentId() != null && bo.getParentId() != 0) .filter(bo -> bo.getParentId() != null && bo.getParentId() != 0)
.collect(Collectors.groupingBy(bo -> Math.abs(bo.getParentId()))); .collect(Collectors.groupingBy(bo -> Math.abs(bo.getParentId())));
// 存储临时ID到实际ID的映射 // 存储临时ID到实际ID的映射
Map<Long, Long> idMapping = new HashMap<>(); Map<Long, Long> idMapping = new HashMap<>();
// 3. 优先处理顶级节点,设置基础数据并插入
// 3. 先处理顶级节点
for (ProdMaterialBomBo topNode : topNodes) { for (ProdMaterialBomBo topNode : topNodes) {
Long tempId = Math.abs(topNode.getMaterialBomId()); Long tempId = Math.abs(topNode.getMaterialBomId());
topNode.setMaterialBomId(null); // 清除临时ID topNode.setMaterialBomId(null); // 清除临时ID
topNode.setParentId(0L); // 确保顶级节点parentId为0 topNode.setParentId(0L); // 确保顶级节点parentId为0
topNode.setAncestors("0"); // 顶级节点的ancestors为"0" topNode.setAncestors("0"); // 顶级节点的ancestors为"0"
topNode.setTopFlag(1L); // 设置顶级节点的topFlag为1
// 插入顶级节点 // 插入顶级节点
ProdMaterialBom add = MapstructUtils.convert(topNode, ProdMaterialBom.class); ProdMaterialBom add = MapstructUtils.convert(topNode, ProdMaterialBom.class);
if (baseMapper.insert(add) <= 0) { if (baseMapper.insert(add) <= 0) {
throw new ServiceException("插入顶级节点失败"); throw new ServiceException("插入顶级节点失败");
} }
// 记录映射关系 // 记录映射关系
idMapping.put(tempId, add.getMaterialBomId()); idMapping.put(tempId, add.getMaterialBomId());
} }
// 4. 处理子节点,直到所有节点都处理完 // 4. 处理子节点,直到所有节点都处理完
while (!childrenMap.isEmpty()) { while (!childrenMap.isEmpty()) {
// 找出当前可以处理的节点父节点已经在idMapping中的节点 // 找出当前可以处理的节点父节点已经在idMapping中的节点
List<Long> processableParentIds = new ArrayList<>(childrenMap.keySet()); List<Long> processableParentIds = new ArrayList<>(childrenMap.keySet());
boolean processed = false; boolean processed = false;// 标记本轮是否有节点被处理
for (Long parentTempId : processableParentIds) { for (Long parentTempId : processableParentIds) {
// 检查父节点是否已处理 // 检查父节点是否已处理
if (idMapping.containsKey(parentTempId)) { if (idMapping.containsKey(parentTempId)) {
List<ProdMaterialBomBo> children = childrenMap.remove(parentTempId); List<ProdMaterialBomBo> children = childrenMap.remove(parentTempId);
// 处理同一父节点的所有子节点 // 处理同一父节点的所有子节点
for (ProdMaterialBomBo child : children) { for (ProdMaterialBomBo child : children) {
Long childTempId = Math.abs(child.getMaterialBomId()); Long childTempId = Math.abs(child.getMaterialBomId());
child.setMaterialBomId(null); // 清除临时ID
// 设置实际的父节点ID // 设置实际的父节点ID
child.setMaterialBomId(null);// 清除临时ID
child.setTopFlag(0L); // 设置非顶级节点的topFlag为0
Long actualParentId = idMapping.get(parentTempId); Long actualParentId = idMapping.get(parentTempId);
child.setParentId(actualParentId); child.setParentId(actualParentId);
// 设置祖级列表 // 设置祖级列表
ProdMaterialBom parent = baseMapper.selectById(actualParentId); ProdMaterialBom parent = baseMapper.selectById(actualParentId);
child.setAncestors(parent.getAncestors() + "," + actualParentId); child.setAncestors(parent.getAncestors() + "," + actualParentId);
@ -246,13 +237,11 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
processed = true; processed = true;
} }
} }
// 如果一轮循环没有处理任何节点,说明数据有问题 // 如果一轮循环没有处理任何节点,说明数据有问题
if (!processed && !childrenMap.isEmpty()) { if (!processed && !childrenMap.isEmpty()) {
throw new ServiceException("存在无法处理的节点,可能是父节点丢失"); throw new ServiceException("存在无法处理的节点,可能是父节点丢失");
} }
} }
return true; return true;
} catch (Exception e) { } catch (Exception e) {
throw new ServiceException("批量插入失败: " + e.getMessage()); throw new ServiceException("批量插入失败: " + e.getMessage());

Loading…
Cancel
Save