9 小时以前 94462f5e4f6f78adf678101923eb2fd54056c62f
src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -62,14 +62,42 @@
    @Autowired
    private ProductProcessService productProcessService;
    @Override
    public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) {
        return productBomMapper.listPage(page, productBomDto);
    }
    /**
     * 产品根据bom计算子项数量(只统计二级)
     */
    public Long countChild(Long productModelId) {
        ProductBom productBom = productBomMapper.selectOne(new LambdaQueryWrapper<ProductBom>()
                .eq(ProductBom::getProductModelId, productModelId));
        if(productBom != null){
           return productStructureService.countBybomId(productBom.getId());
        }
        return 0L;
    }
    /**
     * 判断产品是否存在bom false不存在 true存在
     * @param productBom
     * @return
     */
    public boolean checkBom(ProductBom productBom) {
        ProductBom productBom1 = productBomMapper.selectOne(new LambdaQueryWrapper<ProductBom>()
                .eq(ProductBom::getProductModelId, productBom.getProductModelId()));
        return productBom1 == null ? false : true;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(ProductBom productBom) {
        boolean b = checkBom(productBom);
        if (b) {
            return AjaxResult.error("产品模型已存在BOM");
        }
        boolean save = productBomMapper.insert(productBom) > 0;
        if (save) {
            String no = "BM." + String.format("%05d", productBom.getId());
@@ -109,15 +137,7 @@
                                  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)){
            return;
        }
        BomImportDto parentId = parentChildren.get(0); // 父级数据
        ProductStructure rootNode = new ProductStructure();
        rootNode.setBomId(bom.getId());
        rootNode.setParentId(null); // 顶层没有父节点
@@ -133,7 +153,7 @@
                continue;
            }
            //  获取子项模型信息
            ProductModel childModel = findModel(child.getChildName(), child.getChildSpec());
            ProductModel childModel = findModel(child.getChildName(), child.getChildCode());
            if(childModel.getId() == null){
                BomImportErrorDto errorDto = new BomImportErrorDto();
                BeanUtils.copyProperties(child, errorDto);
@@ -189,7 +209,7 @@
            //  创建 BOM 数据
            BomImportDto first = entry.getValue().get(0);
            ProductModel rootModel = findModel(first.getParentName(), first.getParentSpec());
            ProductModel rootModel = findModel(first.getParentName(), first.getParentCode());
            if(rootModel.getId() == null){
                BomImportErrorDto error = new BomImportErrorDto();
                BeanUtils.copyProperties(first, error);
@@ -197,9 +217,18 @@
                errorList.add(error);
                continue;
            }
            ProductBom bom = new ProductBom();
            bom.setProductModelId(rootModel.getId());
            bom.setVersion("1.0");
            boolean b = checkBom(bom);
            if(b){
                BomImportErrorDto error = new BomImportErrorDto();
                BeanUtils.copyProperties(first, error);
                error.setErrorMsg("产品【"+ first.getParentSpec() + "】BOM已存在");
                errorList.add(error);
                continue;
            }
            productBomMapper.insert(bom);
            bom.setBomNo("BM." + String.format("%05d", bom.getId()));
            productBomMapper.updateById(bom);
@@ -282,6 +311,22 @@
        util.exportExcel(response, exportList, "BOM结构导出");
    }
    @Override
    public String updateBom(ProductBom productBom) {
        ProductBom productBom1 = productBomMapper.selectById(productBom.getId());
        if(productBom.getProductModelId().equals(productBom1.getProductModelId())){
            productBomMapper.updateById(productBom);
        }else{
            boolean b = checkBom(productBom);
            if(b){
               throw new ServiceException("产品BOM已存在");
            }else{
                productBomMapper.updateById(productBom);
            }
        }
        return "修改成功";
    }
    private void populateMap(List<ProductStructureDto> nodes, Map<Long, ProductStructureDto> map) {
        if (nodes == null || nodes.isEmpty()) {
            return;