|
|
|
@ -179,57 +179,48 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
|
|
|
|
|
try {
|
|
|
|
|
// 按层级分组(根据parentId分组)
|
|
|
|
|
Map<Long, List<ProdMaterialBomBo>> levelMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
// 1. 首先找出所有顶级节点
|
|
|
|
|
List<ProdMaterialBomBo> topNodes = boList.stream()
|
|
|
|
|
.filter(bo -> bo.getParentId() == null || bo.getParentId() == 0)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// 2. 其他节点按parentId分组
|
|
|
|
|
Map<Long, List<ProdMaterialBomBo>> childrenMap = boList.stream()
|
|
|
|
|
.filter(bo -> bo.getParentId() != null && bo.getParentId() != 0)
|
|
|
|
|
.collect(Collectors.groupingBy(bo -> Math.abs(bo.getParentId())));
|
|
|
|
|
|
|
|
|
|
// 存储临时ID到实际ID的映射
|
|
|
|
|
Map<Long, Long> 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<Long> processableParentIds = new ArrayList<>(childrenMap.keySet());
|
|
|
|
|
boolean processed = false;
|
|
|
|
|
|
|
|
|
|
boolean processed = false;// 标记本轮是否有节点被处理
|
|
|
|
|
for (Long parentTempId : processableParentIds) {
|
|
|
|
|
// 检查父节点是否已处理
|
|
|
|
|
if (idMapping.containsKey(parentTempId)) {
|
|
|
|
|
List<ProdMaterialBomBo> 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());
|
|
|
|
|