gongchunyi
5 小时以前 bc116c55a44bc344e5575b5bdd4c2591bc0475aa
src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -4,8 +4,6 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.production.pojo.ProductMaterial;
import com.ruoyi.production.pojo.ProductMaterialSku;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -18,6 +16,8 @@
import com.ruoyi.production.pojo.ProductProcess;
import com.ruoyi.production.pojo.ProductStructure;
import com.ruoyi.production.service.*;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -50,44 +50,30 @@
    private ProductProcessService productProcessService;
    @Autowired
    private ProductMaterialService productMaterialService;
    @Autowired
    private ProductMaterialSkuService productMaterialSkuService;
    private SysDictDataMapper sysDictDataMapper;
    @Override
    public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) {
    public IPage<ProductBomDto> listPage(Page<ProductBom> page, ProductBomDto productBomDto) {
        return productBomMapper.listPage(page, productBomDto);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(ProductBom productBom) {
        if (productBom == null || productBom.getDictCode() == null) {
            throw new ServiceException("新增失败,产品类型不能为空");
        }
        SysDictData sysDictData = sysDictDataMapper.selectDictDataById(productBom.getDictCode());
        if (sysDictData == null) {
            throw new ServiceException("新增失败,产品类型不存在");
        }
        boolean save = productBomMapper.insert(productBom) > 0;
        if (save) {
            String no = "BM." + String.format("%05d", productBom.getId());
            productBom.setBomNo(no);
            productBomMapper.updateById(productBom);
            //  查询出产品模型信息
            if (productBom.getProductModelId() == null) {
                throw new ServiceException("请选择产品模型");
            }
            ProductMaterialSku productModel = productMaterialSkuService.getById(productBom.getProductModelId());
            if (productModel == null) {
                throw new ServiceException("选择的产品模型不存在");
            }
            ProductMaterial productMaterial = productMaterialService.getById(productModel.getProductId());
            //  添加初始的产品结构
            ProductStructure productStructure = new ProductStructure();
            productStructure.setProductModelId(productBom.getProductModelId());
            productStructure.setUnit(productMaterial != null ? productMaterial.getUnit() : null);
            productStructure.setUnitQuantity(BigDecimal.valueOf(1));
            productStructure.setBomId(productBom.getId());
            productStructureService.save(productStructure);
            return AjaxResult.success();
        }
@@ -96,7 +82,15 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult uploadBom(MultipartFile file) {
    public AjaxResult uploadBom(MultipartFile file, Long dictCode) {
        if (dictCode == null) {
            return AjaxResult.error("导入失败,产品类型不能为空");
        }
        SysDictData sysDictData = sysDictDataMapper.selectDictDataById(dictCode);
        if (sysDictData == null) {
            return AjaxResult.error("导入失败,产品类型不存在");
        }
        ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class);
        List<BomImportDto> list;
        try {
@@ -107,77 +101,66 @@
        if (list == null || list.isEmpty()) return AjaxResult.error("数据为空");
        //  处理工序
        //  处理字段清理
        list.forEach(dto -> {
            dto.setParentName(clean(dto.getParentName()));
            dto.setParentSpec(clean(dto.getParentSpec()));
            dto.setChildName(clean(dto.getChildName()));
            dto.setChildSpec(clean(dto.getChildSpec()));
            dto.setParentCode(clean(dto.getParentCode()));
            dto.setCode(clean(dto.getCode()));
        });
        handleProcess(list);
        Map<String, Long> processMap = productProcessService.list().stream()
                .collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1));
        //  创建 BOM 数据
        BomImportDto first = list.get(0);
        ProductMaterialSku rootModel = findModel(first.getParentName(), first.getParentSpec());
        ProductBom bom = new ProductBom();
        bom.setProductModelId(rootModel.getId());
        bom.setVersion("1.0");
        bom.setDictCode(dictCode);
        productBomMapper.insert(bom);
        bom.setBomNo("BM." + String.format("%05d", bom.getId()));
        productBomMapper.updateById(bom);
        // 记录已经插入结构的节点:Key = "名称+规格", Value = structure_id
        // 记录已经插入结构的节点:Key = "编码", Value = structure_id
        Map<String, Long> treePathMap = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            BomImportDto dto = list.get(i);
            String parentKey = dto.getParentName() + "|" + dto.getParentSpec();
            String childKey = dto.getChildName() + "|" + dto.getChildSpec();
            String currentCode = dto.getCode();
            String parentCode = dto.getParentCode();
            //处理根节点,第一行且子项为空
            if (i == 0 && StringUtils.isBlank(dto.getChildName())) {
            // 处理根节点:一般指第一行且没有父项编号
            if (i == 0 && StringUtils.isBlank(parentCode)) {
                ProductStructure rootNode = new ProductStructure();
                rootNode.setBomId(bom.getId());
                rootNode.setParentId(null); // 顶层没有父节点
                rootNode.setProductModelId(rootModel.getId());
                if (processMap.containsKey(dto.getProcess())) {
                    rootNode.setProcessId(processMap.get(dto.getProcess()));
                }
                rootNode.setUnitQuantity(BigDecimal.ONE);
                ProductMaterial rootMaterial = productMaterialService.getById(rootModel.getProductId());
                rootNode.setUnit(rootMaterial != null ? rootMaterial.getUnit() : null);
                productStructureService.save(rootNode);
                treePathMap.put(parentKey, rootNode.getId());
                treePathMap.put(currentCode, rootNode.getId());
                continue;
            }
            //  处理子层级节点
            //  找到父节点在数据库里的 ID
            Long parentStructureId = treePathMap.get(parentKey);
            Long parentStructureId = treePathMap.get(parentCode);
            if (parentStructureId == null) {
                // 如果 Map 里找不到,说明 Excel 顺序乱了或者数据有误
                throw new ServiceException("导入失败: 父项[" + dto.getParentName() + "]必须在其子项之前定义");
                throw new ServiceException("导入失败: 父项[" + parentCode + "]必须在其子项之前定义");
            }
            //  获取子项模型信息
            ProductMaterialSku childModel = findModel(dto.getChildName(), dto.getChildSpec());
            //  插入结构表
            ProductStructure node = new ProductStructure();
            node.setBomId(bom.getId());
            node.setParentId(parentStructureId); // 父节点ID
            node.setProductModelId(childModel.getId());
            node.setUnitQuantity(dto.getUnitQty());
            ProductMaterial childMaterial = productMaterialService.getById(childModel.getProductId());
            node.setUnit(childMaterial != null ? childMaterial.getUnit() : null);
            if (processMap.containsKey(dto.getProcess())) {
                node.setProcessId(processMap.get(dto.getProcess()));
            }
            productStructureService.save(node);
            //  把当前子项记录到 Map,作为以后更深层级的父项查找依据
            //  同一父项下的同名子项不需要重复记录
            treePathMap.put(childKey, node.getId());
            //  把当前项记录到 Map, 作为以后更深层级的父项查找依据
            treePathMap.put(currentCode, node.getId());
        }
        return AjaxResult.success("BOM导入成功");
@@ -206,8 +189,7 @@
        for (ProductStructureDto root : treeData) {
            //  添加根节点
            BomImportDto rootRow = new BomImportDto();
            rootRow.setParentName(root.getProductName());
            rootRow.setParentSpec(root.getModel());
            rootRow.setCode(root.getId().toString());
            rootRow.setUnitQty(root.getUnitQuantity());
            rootRow.setRemark("");
            exportList.add(rootRow);
@@ -229,14 +211,14 @@
                }
                BomImportDto row = new BomImportDto();
                // 父类信息
                row.setParentName(parent.getProductName());
                row.setParentSpec(parent.getModel());
                // 子类信息
                row.setChildName(child.getProductName());
                row.setChildSpec(child.getModel());
                // 父类编号
                row.setParentCode(parent.getId().toString());
                // 本身编号
                row.setCode(child.getId().toString());
                row.setUnitQty(child.getUnitQuantity());
                row.setProcess(child.getProcessName());
//                row.setProcess();
                row.setRemark("");
                exportList.add(row);
@@ -259,18 +241,6 @@
            map.put(node.getId(), node);
            populateMap(node.getChildren(), map);
        }
    }
    private ProductMaterialSku findModel(String name, String spec) {
        ProductMaterial product = productMaterialService.getOne(new LambdaQueryWrapper<ProductMaterial>()
                .eq(ProductMaterial::getProductName, name).last("limit 1"));
        if (product == null) throw new ServiceException("产品未维护:" + name);
        ProductMaterialSku model = productMaterialSkuService.getOne(new LambdaQueryWrapper<ProductMaterialSku>()
                .eq(ProductMaterialSku::getProductId, product.getId())
                .eq(ProductMaterialSku::getModel, spec).last("limit 1"));
        if (model == null) throw new ServiceException("规格未维护:" + name + "[" + spec + "]");
        return model;
    }
    private void handleProcess(List<BomImportDto> list) {