From f4c288c55d08c04cd026508b358beebfcdce5fc2 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 22 五月 2026 09:29:09 +0800
Subject: [PATCH] feat(product): 添加产品型号向下复制功能并优化生产报工重量计算 - 在ProductController中新增downCopy接口实现产品型号批量复制功能 - 将ProductionProductMainDto中的bomInputQty字段重命名为inputWeight - 在ProductionProductMainServiceImpl中添加JSON解析逻辑支持从otherData中提取投入重量 - 新增resolveInputWeight、findParameterValue、findFieldValue等工具方法处理复杂参数解析 - 为ProductModelDto添加targetProductId字段用于指定复制目标 - 修复销售台账按调度员ID和姓名分组的SQL查询问题 - 优化库存服务中剩余数量计算的空值处理逻辑 - 完善生产投料数量为空时的默认值处理机制

---
 src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java |   89 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 82 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
index 16a45b1..4a4ece9 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -3,6 +3,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.basic.dto.ProductDto;
@@ -12,6 +13,7 @@
 import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.basic.pojo.ProductModel;
 import com.ruoyi.basic.service.IProductModelService;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -23,9 +25,7 @@
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -103,13 +103,24 @@
             ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class);
             List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
 
-            if (productModelList == null || productModelList.isEmpty()) {
+            if (CollectionUtils.isEmpty(productModelList)) {
                 return AjaxResult.error("瀵煎叆鏁版嵁涓嶈兘涓虹┖");
             }
+
+            //  鑾峰彇褰撳墠浜у搧涓嬫墍鏈夌殑瑙勬牸鍨嬪彿鍚�
+            List<ProductModel> existingModels = list(new LambdaQueryWrapper<ProductModel>().eq(ProductModel::getProductId, productId));
+            Set<String> existingModelNames = existingModels.stream().map(ProductModel::getModel).collect(Collectors.toSet());
+
+            List<ProductModel> waitToSaveList = new ArrayList<>();
+            int skipCount = 0;
 
             for (int i = 0; i < productModelList.size(); i++) {
                 ProductModel item = productModelList.get(i);
                 int rowNum = i + 2;
+
+                if (StringUtils.isEmpty(item.getMaterialCode())) {
+                    return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [鐗╂枡缂栧彿] 涓嶈兘涓虹┖");
+                }
 
                 if (StringUtils.isEmpty(item.getModel())) {
                     return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [瑙勬牸鍨嬪彿] 涓嶈兘涓虹┖");
@@ -117,14 +128,78 @@
                 if (StringUtils.isEmpty(item.getUnit())) {
                     return AjaxResult.error("绗� " + rowNum + " 琛屽鍏ュけ璐�: [鍗曚綅] 涓嶈兘涓虹┖");
                 }
+
+                //  鍘婚噸,濡傛灉宸插寘鍚鍨嬪彿,鍒欒烦杩�
+                if (existingModelNames.contains(item.getModel())) {
+                    skipCount++;
+                    continue;
+                }
+
                 item.setProductId(product.getId());
+                waitToSaveList.add(item);
+
+                existingModelNames.add(item.getModel());
             }
 
-            saveOrUpdateBatch(productModelList);
-            return AjaxResult.success("鎴愬姛瀵煎叆 " + productModelList.size() + " 鏉℃暟鎹�");
+            if (!waitToSaveList.isEmpty()) {
+                saveBatch(waitToSaveList);
+            }
+
+            if (skipCount == 0) {
+                return AjaxResult.success(String.format("鎴愬姛瀵煎叆 %d 鏉℃暟鎹�", waitToSaveList.size()));
+            } else {
+                return AjaxResult.success(String.format("鎴愬姛瀵煎叆 %d 鏉★紝璺宠繃宸插瓨鍦ㄦ暟鎹� %d 鏉�", waitToSaveList.size(), skipCount));
+            }
         } catch (Exception e) {
             log.error("瀵煎叆浜у搧瑙勬牸寮傚父", e);
-            return AjaxResult.error("瀵煎叆澶辫触");
+            throw new ServiceException("瀵煎叆澶辫触");
         }
     }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public int downCopy(ProductModelDto productModelDto) {
+        if (productModelDto == null || productModelDto.getProductId() == null || productModelDto.getTargetProductId() == null) {
+            throw new ServiceException("婧愪骇鍝両D鍜岀洰鏍囦骇鍝両D涓嶈兘涓虹┖");
+        }
+
+        Long sourceProductId = productModelDto.getProductId();
+        Long targetProductId = productModelDto.getTargetProductId();
+
+        if (sourceProductId.equals(targetProductId)) {
+            throw new ServiceException("婧愪骇鍝佸拰鐩爣浜у搧涓嶈兘鐩稿悓");
+        }
+
+        Product sourceProduct = productMapper.selectById(sourceProductId);
+        if (sourceProduct == null) {
+            throw new ServiceException("婧愪骇鍝佷笉瀛樺湪");
+        }
+
+        Product targetProduct = productMapper.selectById(targetProductId);
+        if (targetProduct == null) {
+            throw new ServiceException("鐩爣浜у搧涓嶅瓨鍦�");
+        }
+
+        List<ProductModel> sourceModels = productModelMapper.selectList(
+                new LambdaQueryWrapper<ProductModel>()
+                        .eq(ProductModel::getProductId, sourceProductId)
+                        .orderByAsc(ProductModel::getId)
+        );
+        if (CollectionUtils.isEmpty(sourceModels)) {
+            throw new ServiceException("婧愪骇鍝佷笅娌℃湁鍙鍒剁殑鍨嬪彿");
+        }
+
+        List<ProductModel> copyList = new ArrayList<>();
+        for (ProductModel sourceModel : sourceModels) {
+            ProductModel copy = new ProductModel();
+            BeanUtils.copyProperties(sourceModel, copy);
+            copy.setId(null);
+            copy.setProductId(targetProductId);
+            copy.setTenantId(null);
+            copyList.add(copy);
+        }
+
+        saveBatch(copyList);
+        return copyList.size();
+    }
 }

--
Gitblit v1.9.3