| | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Boolean addProductStructureDto(ProductStructureDto dto) { |
| | | public Boolean addProductStructureDto(Integer bomId, List<ProductStructureDto> list) { |
| | | |
| | | Integer bomId = dto.getBomId(); |
| | | |
| | | // 将树扁平化 |
| | | // 扁平化传入的树 |
| | | List<ProductStructureDto> flatDtoList = new ArrayList<>(); |
| | | flattenTree(dto.getChildren(), flatDtoList); |
| | | for (ProductStructureDto root : list) { |
| | | flatDtoList.add(root); |
| | | flattenTree(root.getChildren(), flatDtoList); |
| | | } |
| | | |
| | | // 查询数据库中已有的 BOM 数据 |
| | | List<ProductStructure> dbList = this.list(new LambdaQueryWrapper<ProductStructure>().eq(ProductStructure::getBomId, bomId)); |
| | |
| | | entity.setBomId(bomId); |
| | | |
| | | if (psDto.getId() == null) { |
| | | // 新增 |
| | | entity.setId(null); |
| | | entity.setParentId(null); |
| | | insertList.add(entity); |
| | | if (psDto.getTempId() != null) { |
| | | tempEntityMap.put(psDto.getTempId(), entity); |
| | | } |
| | | } else { |
| | | // 更新 |
| | | updateList.add(entity); |
| | | } |
| | | } |
| | |
| | | |
| | | // 回写新增节点 parentId |
| | | List<ProductStructure> parentFixList = new ArrayList<>(); |
| | | // 真实的父节点 ID |
| | | Long realParentId; |
| | | for (ProductStructureDto psDto : flatDtoList) { |
| | | String parentTempId = psDto.getParentTempId(); |
| | | if (psDto.getId() == null && parentTempId != null && !parentTempId.isEmpty()) { |
| | | if (psDto.getId() != null) continue; |
| | | ProductStructure child = tempEntityMap.get(psDto.getTempId()); |
| | | if (child == null) continue; |
| | | String parentTempId = psDto.getParentTempId(); |
| | | if (parentTempId != null && !parentTempId.isEmpty()) { |
| | | if (tempEntityMap.containsKey(parentTempId)) { |
| | | // 父节点是新节点 |
| | | realParentId = tempEntityMap.get(parentTempId).getId(); |
| | | } else { |
| | | // 父节点是老节点 |
| | | try { |
| | | realParentId = Long.valueOf(parentTempId); |
| | | } catch (NumberFormatException e) { |
| | | realParentId = 0L; |
| | | } |
| | | } |
| | | |
| | | child.setParentId(realParentId); |
| | | parentFixList.add(child); |
| | | } else if (psDto.getId() == null) { |
| | | // 如果 parentTempId 为空,则是顶级节点 |
| | | ProductStructure child = tempEntityMap.get(psDto.getTempId()); |
| | | } else { |
| | | child.setParentId(0L); |
| | | parentFixList.add(child); |
| | | } |
| | | parentFixList.add(child); |
| | | } |
| | | |
| | | if (!parentFixList.isEmpty()) { |