src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -14,6 +14,7 @@
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.production.dto.BomImportDto;
import com.ruoyi.production.dto.BomImportErrorDto;
import com.ruoyi.production.dto.ProductBomDto;
import com.ruoyi.production.dto.ProductStructureDto;
import com.ruoyi.production.mapper.ProductBomMapper;
@@ -23,6 +24,7 @@
import com.ruoyi.production.service.ProductBomService;
import com.ruoyi.production.service.ProductProcessService;
import com.ruoyi.production.service.ProductStructureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -103,14 +105,17 @@
     * @param children 当前父项的子项列表
     * @return 保存后的父项产品ID
     */
    private void saveBomRecursive(List<BomImportDto> children,ProductBom bom,ProductModel rootModel,Map<String, Long> processMap) {
    private void saveBomRecursive(List<BomImportDto> children,
                                  ProductBom bom,ProductModel rootModel,
                                  Map<String, Long> processMap,
                                  List<BomImportErrorDto> errorList ) {
        // 1. 获取children中子项产品编号为空的数据
        List<BomImportDto> parentChildren = children
                .stream()
                .filter(child -> StringUtils.isEmpty(child.getChildCode()))
                .collect(Collectors.toList());
        if(CollectionUtils.isEmpty(parentChildren)){
            throw new ServiceException("请选择父项产品编号");
            return;
        }
        BomImportDto parentId = parentChildren.get(0); // 父级数据
        ProductStructure rootNode = new ProductStructure();
@@ -129,7 +134,13 @@
            }
            //  获取子项模型信息
            ProductModel childModel = findModel(child.getChildName(), child.getChildSpec());
            if(childModel.getId() == null){
                BomImportErrorDto errorDto = new BomImportErrorDto();
                BeanUtils.copyProperties(child, errorDto);
                errorDto.setErrorMsg(childModel.getErrorMsg());
                errorList.add(errorDto);
                continue;
            }
            //  插入结构表
            ProductStructure node = new ProductStructure();
            node.setBomId(bom.getId());
@@ -147,16 +158,16 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult uploadBom(MultipartFile file) {
    public void uploadBom(MultipartFile file,HttpServletResponse response) {
        ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class);
        List<BomImportDto> list;
        try {
            list = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            return AjaxResult.error("Excel解析失败");
            throw new ServiceException("Excel解析失败");
        }
        if (list == null || list.isEmpty()) return AjaxResult.error("数据为空");
        if (list == null || list.isEmpty()) throw new ServiceException("文件为空");
        //  处理工序
        list.forEach(dto -> {
@@ -172,11 +183,20 @@
        Map<String, List<BomImportDto>> parentMap = list.stream()
                .filter(bom -> StringUtils.isNotBlank(bom.getParentCode()))
                .collect(Collectors.groupingBy(BomImportDto::getParentCode));
        List<BomImportErrorDto> errorList = new ArrayList<>();
        // 2. 遍历所有父项,递归保存
        for (Map.Entry<String, List<BomImportDto>> entry : parentMap.entrySet()) {
            //  创建 BOM 数据
            BomImportDto first = entry.getValue().get(0);
            ProductModel rootModel = findModel(first.getParentName(), first.getParentSpec());
            if(rootModel.getId() == null){
                BomImportErrorDto error = new BomImportErrorDto();
                BeanUtils.copyProperties(first, error);
                error.setErrorMsg(rootModel.getErrorMsg());
                errorList.add(error);
                continue;
            }
            ProductBom bom = new ProductBom();
            bom.setProductModelId(rootModel.getId());
            bom.setVersion("1.0");
@@ -185,9 +205,13 @@
            productBomMapper.updateById(bom);
            // 处理bom子表数据
            List<BomImportDto> children = entry.getValue();
            saveBomRecursive(children,bom,rootModel,processMap);
            saveBomRecursive(children,bom,rootModel,processMap,errorList);
        }
        return AjaxResult.success("BOM导入成功");
        // 判断是否有错误数据,有就导出
        if(CollectionUtils.isNotEmpty(errorList)){
            ExcelUtil<BomImportErrorDto> utils = new ExcelUtil<>(BomImportErrorDto.class);
            utils.exportExcel(response,errorList, "BOM错误数据");
        }
    }
@@ -276,7 +300,10 @@
        ProductModel model = productModelService.getOne(new LambdaQueryWrapper<ProductModel>()
//                .eq(ProductModel::getProductId, product.getId())
                .eq(ProductModel::getModel, spec).last("limit 1"));
        if (model == null) throw new ServiceException("图纸编号未维护:" + name + "[" + spec + "]");
        if (model == null){
            model = new ProductModel();
            model.setErrorMsg("图纸编号未维护:" + "[" + spec + "]");
        }
        return model;
    }