gongchunyi
2 天以前 0a1f000d5031c090059d5ae960019825196d4ba9
src/main/java/com/ruoyi/production/service/impl/ProductBomServiceImpl.java
@@ -109,18 +109,48 @@
            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.setProcess(clean(dto.getProcess()));
        });
        handleProcess(list);
        Map<String, Long> processMap = productProcessService.list().stream()
                .collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1));
        //  创建 BOM 数据
        Set<String> processNames = list.stream()
                .map(BomImportDto::getProcess)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toSet());
        Map<String, Long> processMap = new HashMap<>();
        if (!processNames.isEmpty()) {
            List<ProductProcess> exists = productProcessService.list(
                    new LambdaQueryWrapper<ProductProcess>().in(ProductProcess::getName, processNames)
            );
            processMap = exists.stream().collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1));
            Map<String, Long> finalProcessMap = processMap;
            List<String> missingProcesses = processNames.stream()
                    .filter(n -> !finalProcessMap.containsKey(n))
                    .collect(Collectors.toList());
            if (!missingProcesses.isEmpty()) {
                throw new ServiceException("导入失败,以下工序不存在:" + String.join(", ", missingProcesses));
            }
        }
        Set<String> materialKeys = new HashSet<>();
        for (BomImportDto dto : list) {
            if (StringUtils.isNotBlank(dto.getParentName())) {
                materialKeys.add(dto.getParentName() + "|" + (dto.getParentSpec() == null ? "" : dto.getParentSpec()));
            }
            if (StringUtils.isNotBlank(dto.getChildName())) {
                materialKeys.add(dto.getChildName() + "|" + (dto.getChildSpec() == null ? "" : dto.getChildSpec()));
            }
        }
        Map<String, ProductMaterialSku> skuMap = validateAndGetSkuMap(materialKeys);
        ProductBom bom = new ProductBom();
        bom.setDictCode(dictCode);
        bom.setVersion("1.0");
@@ -128,7 +158,7 @@
        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 (BomImportDto dto : list) {
@@ -143,7 +173,7 @@
            //  处理顶级节点 (子项及其规格均为空当作顶级节点定义)
            if (StringUtils.isBlank(childName) && StringUtils.isBlank(childSpec)) {
                if (!treePathMap.containsKey(parentKey)) {
                    ProductMaterialSku model = findModel(parentName, parentSpec);
                    ProductMaterialSku model = skuMap.get(parentKey);
                    ProductStructure node = saveStructure(bom.getId(), null, model, dto.getUnitQty(), null);
                    treePathMap.put(parentKey, node.getId());
                } else {
@@ -162,14 +192,14 @@
            Long parentStructureId = treePathMap.get(parentKey);
            if (parentStructureId == null) {
                // 如果 Map 里找不到父节点,视为顶级父节点
                ProductMaterialSku parentModel = findModel(parentName, parentSpec);
                ProductMaterialSku parentModel = skuMap.get(parentKey);
                ProductStructure parentNode = saveStructure(bom.getId(), null, parentModel, BigDecimal.ONE, null);
                parentStructureId = parentNode.getId();
                treePathMap.put(parentKey, parentStructureId);
            }
            //  获取子项模型信息
            ProductMaterialSku childModel = findModel(childName, childSpec);
            ProductMaterialSku childModel = skuMap.get(childKey);
            Long processId = null;
            if (StringUtils.isNotBlank(dto.getProcess())) {
                processId = processMap.get(dto.getProcess());
@@ -285,51 +315,68 @@
        }
    }
    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) {
        Set<String> processNames = list.stream()
                .map(BomImportDto::getProcess)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toSet());
        if (processNames.isEmpty()) {
            return;
    /**
     * 批量验证并获取产品SKU信息
     */
    private Map<String, ProductMaterialSku> validateAndGetSkuMap(Set<String> materialKeys) {
        if (materialKeys == null || materialKeys.isEmpty()) {
            return Collections.emptyMap();
        }
        List<ProductProcess> exists = productProcessService.list(
                new LambdaQueryWrapper<ProductProcess>().in(ProductProcess::getName, processNames)
        );
        Set<String> existNames = exists.stream()
                .map(ProductProcess::getName)
        // 1. 获取所有产品名称并查询
        Set<String> productNames = materialKeys.stream()
                .map(k -> k.split("\\|")[0])
                .collect(Collectors.toSet());
        List<ProductProcess> needSave = processNames.stream()
                .filter(n -> !existNames.contains(n))
                .map(n -> {
                    ProductProcess p = new ProductProcess();
                    p.setName(n);
                    return p;
        List<ProductMaterial> products = productMaterialService.list(
                new LambdaQueryWrapper<ProductMaterial>().in(ProductMaterial::getProductName, productNames)
        );
        Map<String, ProductMaterial> productMap = products.stream()
                .collect(Collectors.toMap(ProductMaterial::getProductName, p -> p, (k1, k2) -> k1));
        // 检查缺失的产品
        List<String> missingProducts = productNames.stream()
                .filter(n -> !productMap.containsKey(n))
                .collect(Collectors.toList());
        if (!missingProducts.isEmpty()) {
            throw new ServiceException("导入失败,以下产品未维护:" + String.join(", ", missingProducts));
        }
        // 2. 查询所有相关的 SKU
        // 构造查询:(productId = ? AND model = ?) OR (productId = ? AND model = ?) ...
        // 由于 MyBatis Plus 的 Or 嵌套可能较慢且此处数量通常可控,我们可以先查出所有这些 Product 的所有 SKU
        List<Long> productIds = products.stream().map(ProductMaterial::getId).collect(Collectors.toList());
        List<ProductMaterialSku> skus = productMaterialSkuService.list(
                new LambdaQueryWrapper<ProductMaterialSku>().in(ProductMaterialSku::getProductId, productIds)
        );
        // 构建 Map: "产品名称|规格" -> Sku
        Map<String, ProductMaterialSku> skuMap = new HashMap<>();
        for (ProductMaterialSku sku : skus) {
            ProductMaterial p = productMap.values().stream()
                    .filter(pm -> pm.getId().equals(sku.getProductId()))
                    .findFirst().orElse(null);
            if (p != null) {
                String key = p.getProductName() + "|" + (sku.getModel() == null ? "" : sku.getModel());
                skuMap.put(key, sku);
            }
        }
        // 检查缺失的规格
        List<String> missingSkus = materialKeys.stream()
                .filter(k -> !skuMap.containsKey(k))
                .map(k -> {
                    String[] parts = k.split("\\|");
                    return parts[0] + "[" + (parts.length > 1 ? parts[1] : "") + "]";
                })
                .collect(Collectors.toList());
        if (!needSave.isEmpty()) {
            productProcessService.saveBatch(needSave);
            needSave.forEach(p -> p.setNo("GX" + String.format("%08d", p.getId())));
            productProcessService.updateBatchById(needSave);
        if (!missingSkus.isEmpty()) {
            throw new ServiceException("导入失败,以下产品规格未维护:" + String.join(", ", missingSkus));
        }
        return skuMap;
    }
    private String clean(String s) {