From 49f390c11c7e6d8efdeab17b384c88ab8071fd5c Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 13 三月 2026 11:07:59 +0800
Subject: [PATCH] fix: 物料数据类别数据集合接口优化

---
 src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java |   92 +++++++++++++++++++++++++++++++++++++---------
 1 files changed, 74 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
index 76a52d3..154f7d4 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
@@ -4,11 +4,13 @@
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.http.HttpUtils;
 import com.ruoyi.framework.config.AliDingConfig;
+import com.ruoyi.production.dto.ProductMaterialDto;
 import com.ruoyi.production.dto.ProductMaterialGroupDto;
 import com.ruoyi.production.enums.MaterialConfigTypeEnum;
 import com.ruoyi.production.mapper.ProductMaterialMapper;
@@ -31,6 +33,7 @@
 import java.time.format.DateTimeParseException;
 import java.util.*;
 import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
 
 /**
  * <br>
@@ -302,10 +305,17 @@
 
         for (ProductMaterialSku sku : list) {
 
-            ProductMaterialSku exist =
-                    productMaterialSkuService.getOne(new LambdaQueryWrapper<ProductMaterialSku>()
-                            .eq(ProductMaterialSku::getMaterialId, sku.getMaterialId())
-                            .eq(ProductMaterialSku::getSpecification, sku.getSpecification()));
+            LambdaQueryWrapper<ProductMaterialSku> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(ProductMaterialSku::getMaterialId, sku.getMaterialId())
+                    .eq(ProductMaterialSku::getSpecification, sku.getSpecification());
+
+            if (StringUtils.isNotEmpty(sku.getMaterialCode())) {
+                wrapper.eq(ProductMaterialSku::getMaterialCode, sku.getMaterialCode());
+            } else {
+                wrapper.isNull(ProductMaterialSku::getMaterialCode);
+            }
+
+            ProductMaterialSku exist = productMaterialSkuService.getOne(wrapper);
             if (exist == null) {
                 productMaterialSkuService.save(sku);
                 affected++;
@@ -338,7 +348,7 @@
     }
 
     @Override
-    public List<ProductMaterialGroupDto> ProductMaterialList(String materialName) {
+    public List<ProductMaterialGroupDto> ProductMaterialList() {
 
         List<ProductMaterialConfig> materialConfigList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>()
                 .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
@@ -348,24 +358,70 @@
             return productMaterialMap;
         }
         for (ProductMaterialConfig materialConfig : materialConfigList) {
-            LambdaQueryWrapper<ProductMaterial> wrapper = new LambdaQueryWrapper<>();
-            wrapper.eq(ProductMaterial::getMaterialTypeId, materialConfig.getId())
-                    .select(ProductMaterial::getId, ProductMaterial::getMaterialName)
-                    .like(materialName != null && !materialName.isEmpty(), ProductMaterial::getMaterialName, materialName);
-            List<ProductMaterial> productMaterialList = list(wrapper);
-
-            if (productMaterialList != null && !productMaterialList.isEmpty()) {
-                ProductMaterialGroupDto dto = new ProductMaterialGroupDto();
-                dto.setConfigId(materialConfig.getId());
-                dto.setConfigName(materialConfig.getConfigName());
-                dto.setMaterialList(productMaterialList);
-                productMaterialMap.add(dto);
-            }
+            ProductMaterialGroupDto dto = new ProductMaterialGroupDto();
+            dto.setConfigId(materialConfig.getId());
+            dto.setConfigName(materialConfig.getConfigName());
+            productMaterialMap.add(dto);
         }
         return productMaterialMap;
     }
 
     @Override
+    public List<ProductMaterialGroupDto> productMaterialListByQuery(String materialName, Integer materialTypeId) {
+        if (StringUtils.isEmpty(materialName) && materialTypeId == null) {
+            return new ArrayList<>(0);
+        }
+        // 鏌ヨ鐗╂枡绫诲瀷閰嶇疆
+        List<ProductMaterialConfig> materialConfigList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>()
+                .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
+
+        List<ProductMaterialGroupDto> result = new ArrayList<>();
+        if (CollectionUtils.isEmpty(materialConfigList)) {
+            return result;
+        }
+        LambdaQueryWrapper<ProductMaterial> wrapper;
+        for (ProductMaterialConfig materialConfig : materialConfigList) {
+            wrapper = new LambdaQueryWrapper<>();
+            //  鎸囧畾闇�瑕佺殑瀛楁
+            wrapper.select(
+                    ProductMaterial::getId,
+                    ProductMaterial::getMaterialTypeId,
+                    ProductMaterial::getInventoryCategoryId,
+                    ProductMaterial::getMaterialName
+            );
+            if (StringUtils.isNotEmpty(materialName)) {
+                wrapper.eq(ProductMaterial::getMaterialTypeId, materialConfig.getId())
+                        .like(ProductMaterial::getMaterialName, materialName);
+            } else {
+                if (!materialConfig.getId().equals(materialTypeId)) {
+                    continue;
+                }
+                wrapper.eq(ProductMaterial::getMaterialTypeId, materialTypeId);
+            }
+            List<ProductMaterial> materialList = list(wrapper);
+            if (CollectionUtils.isEmpty(materialList)) {
+                continue;
+            }
+
+            List<ProductMaterialDto> dtoList = materialList.stream().map(m -> {
+                ProductMaterialDto dto = new ProductMaterialDto();
+                dto.setId(m.getId());
+                dto.setMaterialTypeId(m.getMaterialTypeId());
+                dto.setInventoryCategoryId(m.getInventoryCategoryId());
+                dto.setMaterialName(m.getMaterialName());
+                return dto;
+            }).collect(Collectors.toList());
+            ProductMaterialGroupDto dto = new ProductMaterialGroupDto();
+            dto.setConfigId(materialConfig.getId());
+            dto.setConfigName(materialConfig.getConfigName());
+            dto.setMaterialList(dtoList);
+            result.add(dto);
+        }
+
+        return result;
+    }
+
+    @Override
     @Transactional(rollbackFor = Exception.class)
     public void addProductMaterial(ProductMaterial productMaterial) {
         validateProductMaterial(productMaterial, false);

--
Gitblit v1.9.3