gongchunyi
8 小时以前 346d463346701e8714b3a3a3ebb21e21960b5484
src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
@@ -25,13 +25,14 @@
    @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));
@@ -65,13 +66,13 @@
            entity.setBomId(bomId);
            if (psDto.getId() == null) {
                // 新增
                entity.setId(null);
                entity.setParentId(null);
                insertList.add(entity);
                tempEntityMap.put(psDto.getTempId(), entity);
                if (psDto.getTempId() != null) {
                    tempEntityMap.put(psDto.getTempId(), entity);
                }
            } else {
                // 更新
                updateList.add(entity);
            }
        }
@@ -83,28 +84,32 @@
        //  回写新增节点 parentId
        List<ProductStructure> parentFixList = new ArrayList<>();
        //  真实的父节点 ID
        Long realParentId;
        for (ProductStructureDto psDto : flatDtoList) {
            if (psDto.getId() == null && psDto.getParentTempId() != null) {
                ProductStructure child = tempEntityMap.get(psDto.getTempId());
                if (tempEntityMap.containsKey(psDto.getParentTempId())) {
                    // 父节点是新节点
                    realParentId = tempEntityMap.get(psDto.getParentTempId()).getId();
            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 {
                    // 父节点是老节点
                    realParentId = Long.valueOf(psDto.getParentTempId());
                    try {
                        realParentId = Long.valueOf(parentTempId);
                    } catch (NumberFormatException e) {
                        realParentId = 0L;
                    }
                }
                child.setParentId(realParentId);
                parentFixList.add(child);
            } else {
                child.setParentId(0L);
            }
            parentFixList.add(child);
        }
        if (!parentFixList.isEmpty()) {
            this.updateBatchById(parentFixList);
        }
        if (!updateList.isEmpty()) {
            this.updateBatchById(updateList);
        }