gongchunyi
3 天以前 5ac4f450b6b4bd726cefb57a626a46c56dbbc42e
src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -121,8 +121,6 @@
                .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.setDictCode(dictCode);
        bom.setVersion("1.0");
@@ -133,58 +131,74 @@
        // 记录已经插入结构的节点: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();
        for (BomImportDto dto : list) {
            String parentName = dto.getParentName();
            String parentSpec = dto.getParentSpec();
            String childName = dto.getChildName();
            String childSpec = dto.getChildSpec();
            //处理根节点,第一行且子项为空
            if (i == 0 && StringUtils.isBlank(dto.getChildName())) {
                ProductStructure rootNode = new ProductStructure();
                rootNode.setBomId(bom.getId());
                rootNode.setParentId(null); // 顶层没有父节点
                rootNode.setProductModelId(rootModel.getId());
                rootNode.setUnitQuantity(BigDecimal.ONE);
                ProductMaterial rootMaterial = productMaterialService.getById(rootModel.getProductId());
                rootNode.setUnit(rootMaterial != null ? rootMaterial.getUnit() : null);
                productStructureService.save(rootNode);
            String parentKey = parentName + "|" + (parentSpec == null ? "" : parentSpec);
            String childKey = childName + "|" + (childSpec == null ? "" : childSpec);
                treePathMap.put(parentKey, rootNode.getId());
            //  处理顶级节点 (子项及其规格均为空当作顶级节点定义)
            if (StringUtils.isBlank(childName) && StringUtils.isBlank(childSpec)) {
                if (!treePathMap.containsKey(parentKey)) {
                    ProductMaterialSku model = findModel(parentName, parentSpec);
                    ProductStructure node = saveStructure(bom.getId(), null, model, dto.getUnitQty(), null);
                    treePathMap.put(parentKey, node.getId());
                } else {
                    // 如果已经存在,仅在它是顶级节点时更新
                    Long structureId = treePathMap.get(parentKey);
                    ProductStructure node = productStructureService.getById(structureId);
                    if (node != null && node.getParentId() == null && dto.getUnitQty() != null) {
                        node.setUnitQuantity(dto.getUnitQty());
                        productStructureService.updateById(node);
                    }
                }
                continue;
            }
            //  处理子层级节点
            //  找到父节点在数据库里的 ID
            //  处理父子层级关系
            Long parentStructureId = treePathMap.get(parentKey);
            if (parentStructureId == null) {
                // 如果 Map 里找不到,说明 Excel 顺序乱了或者数据有误
                throw new ServiceException("导入失败: 父项[" + dto.getParentName() + "]必须在其子项之前定义");
                // 如果 Map 里找不到父节点,视为顶级父节点
                ProductMaterialSku parentModel = findModel(parentName, parentSpec);
                ProductStructure parentNode = saveStructure(bom.getId(), null, parentModel, BigDecimal.ONE, null);
                parentStructureId = parentNode.getId();
                treePathMap.put(parentKey, parentStructureId);
            }
            //  获取子项模型信息
            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()));
            ProductMaterialSku childModel = findModel(childName, childSpec);
            Long processId = null;
            if (StringUtils.isNotBlank(dto.getProcess())) {
                processId = processMap.get(dto.getProcess());
            }
            productStructureService.save(node);
            //  把当前子项记录到 Map,作为以后更深层级的父项查找依据
            //  同一父项下的同名子项不需要重复记录
            treePathMap.put(childKey, node.getId());
            // 插入子节点结构表
            ProductStructure childNode = saveStructure(bom.getId(), parentStructureId, childModel, dto.getUnitQty(), processId);
            // 把当前子项记录到 Map,作为以后更深层级的父项查找依据
            treePathMap.put(childKey, childNode.getId());
        }
        return AjaxResult.success("BOM导入成功");
    }
    /**
     * 保存结构节点
     */
    private ProductStructure saveStructure(Integer bomId, Long parentId, ProductMaterialSku model, BigDecimal qty, Long processId) {
        ProductStructure node = new ProductStructure();
        node.setBomId(bomId);
        node.setParentId(parentId);
        node.setProductModelId(model.getId());
        node.setUnitQuantity(qty != null ? qty : BigDecimal.ONE);
        ProductMaterial material = productMaterialService.getById(model.getProductId());
        node.setUnit(material != null ? material.getUnit() : null);
        node.setProcessId(processId);
        productStructureService.save(node);
        return node;
    }
    @Override
    public void exportBom(HttpServletResponse response, Integer bomId) {