zss
2 天以前 9d42f647f5589e4a560d745d6b359ae6c273bd8d
src/main/java/com/ruoyi/production/service/impl/ProductionBomStructureServiceImpl.java
@@ -33,6 +33,7 @@
     */
    @Override
    public List<ProductionBomStructureVo> listByBomId(Long bomId) {
        // 按BOMID查询生产结构数据
        List<ProductionBomStructureVo> list = productionBomStructureMapper.listByBomId(bomId);
        Map<Long, ProductionBomStructureVo> map = new HashMap<>();
        for (ProductionBomStructureVo node : list) {
@@ -58,13 +59,17 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addProductionBomStructure(ProductionBomStructureDto dto) {
        // 新增生产BOM结构
        // 读取当前订单BOM主键,并把前端树结构拍平成列表
        Long orderBomId = dto.getProductionOrderBomId();
        List<ProductionBomStructureDto> flatDtoList = new ArrayList<>();
        flattenTree(dto.getChildren(), flatDtoList);
        // 查询数据库已有结构,用于后续做增删改对比
        List<ProductionBomStructure> dbList = this.list(new LambdaQueryWrapper<ProductionBomStructure>()
                .eq(ProductionBomStructure::getProductionOrderBomId, orderBomId));
        // 收集前端仍然存在的节点ID
        Set<Long> frontendIds = new HashSet<>();
        for (ProductionBomStructureDto item : flatDtoList) {
            if (item.getId() != null) {
@@ -72,16 +77,19 @@
            }
        }
        // 计算需要删除的节点(数据库有、前端已删除)
        Set<Long> deleteIds = new HashSet<>();
        for (ProductionBomStructure dbItem : dbList) {
            if (!frontendIds.contains(dbItem.getId())) {
                deleteIds.add(dbItem.getId());
            }
        }
        // 先删掉前端已经移除的节点
        if (!deleteIds.isEmpty()) {
            this.removeByIds(deleteIds);
        }
        // 按是否有ID拆分为新增和更新,同时缓存新增节点的临时ID映射
        List<ProductionBomStructure> insertList = new ArrayList<>();
        List<ProductionBomStructure> updateList = new ArrayList<>();
        Map<String, ProductionBomStructure> tempEntityMap = new HashMap<>();
@@ -99,10 +107,12 @@
            }
        }
        // 批量新增,拿到数据库生成的真实ID
        if (!insertList.isEmpty()) {
            this.saveBatch(insertList);
        }
        // 新增节点二次回写父ID(前端传的是临时父ID)
        List<ProductionBomStructure> parentFixList = new ArrayList<>();
        for (ProductionBomStructureDto item : flatDtoList) {
            if (item.getId() == null && item.getParentTempId() != null) {
@@ -111,15 +121,18 @@
                    continue;
                }
                ProductionBomStructure parent = tempEntityMap.get(item.getParentTempId());
                // 父节点是本次新增时,直接用新增后的真实ID;否则回退为前端传入父ID
                Long realParentId = parent != null ? parent.getId() : Long.valueOf(item.getParentTempId());
                child.setParentId(realParentId);
                parentFixList.add(child);
            }
        }
        // 回写新增节点的父子关系
        if (!parentFixList.isEmpty()) {
            this.updateBatchById(parentFixList);
        }
        // 批量更新已有节点
        if (!updateList.isEmpty()) {
            this.updateBatchById(updateList);
        }
@@ -130,6 +143,7 @@
     * 将树形结构拍平成列表,便于统一保存。
     */
    private void flattenTree(List<ProductionBomStructureDto> source, List<ProductionBomStructureDto> result) {
        // 扁平化处理树
        if (source == null) {
            return;
        }