From 481d8165f26fc7e37f0e87f6444ac0340bb563a3 Mon Sep 17 00:00:00 2001
From: xiaoyi <908147524@qq.com>
Date: 星期二, 03 十一月 2026 18:53:22 +0800
Subject: [PATCH] fix: 移植长辉已验证的四类接口 500/体验问题(不含物料颜色字段)

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesLineServiceImpl.java |   91 ++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 76 insertions(+), 15 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesLineServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesLineServiceImpl.java
index a1ec2f7..e684794 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesLineServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesLineServiceImpl.java
@@ -5,8 +5,10 @@
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.ObjUtil;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
+import cn.iocoder.yudao.module.mes.controller.admin.wm.batch.vo.MesWmBatchGenerateReqVO;
 import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.line.MesWmProductSalesLinePageReqVO;
 import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.line.MesWmProductSalesLineSaveReqVO;
+import cn.iocoder.yudao.module.mes.dal.dataobject.mdm.MesMdmItemDO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO;
 import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesLineMapper;
@@ -17,6 +19,7 @@
 import cn.iocoder.yudao.module.mes.dal.dataobject.wm.batch.MesWmBatchDO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.wm.salesnotice.MesWmSalesNoticeLineDO;
 import cn.iocoder.yudao.module.mes.service.wm.salesnotice.MesWmSalesNoticeLineService;
+import cn.iocoder.yudao.module.mes.service.qc.oqc.MesQcOqcService;
 import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Lazy;
@@ -34,7 +37,7 @@
 /**
  * MES 閿�鍞嚭搴撳崟琛� Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
@@ -56,6 +59,9 @@
     @Resource
     @Lazy
     private MesWmSalesNoticeLineService salesNoticeLineService;
+    @Resource
+    @Lazy
+    private MesQcOqcService oqcService;
 
     @Override
     public Long createProductSalesLine(MesWmProductSalesLineSaveReqVO createReqVO) {
@@ -79,6 +85,34 @@
         // 鏇存柊
         MesWmProductSalesLineDO updateObj = BeanUtils.toBean(updateReqVO, MesWmProductSalesLineDO.class);
         productSalesLineMapper.updateById(updateObj);
+
+        // 妫�鏌ユ槸鍚﹂渶瑕佸垱寤� OQC 妫�楠屽崟锛氫粠涓嶉渶瑕佹楠屽彉涓洪渶瑕佹楠�
+        if (Boolean.TRUE.equals(updateReqVO.getOqcCheckFlag())
+                && !Boolean.TRUE.equals(oldLine.getOqcCheckFlag())) {
+            // 鑾峰彇鍑哄簱鍗曚俊鎭�
+            MesWmProductSalesDO sales = productSalesService.getProductSales(updateReqVO.getSalesId());
+            if (sales == null) {
+                log.warn("[updateProductSalesLine] 鍑哄簱鍗曚笉瀛樺湪锛宻alesId={}", updateReqVO.getSalesId());
+                return;
+            }
+
+            // 鏋勫缓鍑哄簱鏄庣粏 DO
+            MesWmProductSalesLineDO line = new MesWmProductSalesLineDO();
+            line.setId(updateReqVO.getId());
+            line.setItemId(updateReqVO.getItemId());
+            line.setBatchCode(updateReqVO.getBatchCode());
+            line.setQuantity(updateReqVO.getQuantity());
+
+            // 鍒涘缓 OQC 妫�楠屽崟锛岃寮傚父鎶涘嚭
+            Long oqcId = oqcService.createOqcFromProductSales(
+                    sales.getId(), line, sales.getCode(), sales.getClientId());
+
+            // 鏇存柊鍑哄簱鏄庣粏鐨� oqcId 鍜岃川閲忕姸鎬�
+            productSalesLineMapper.updateById(new MesWmProductSalesLineDO()
+                    .setId(updateReqVO.getId())
+                    .setOqcId(oqcId)
+                    .setQualityStatus(MesWmQualityStatusEnum.PENDING.getStatus()));
+        }
     }
 
     @Override
@@ -135,15 +169,45 @@
     }
 
     /**
-     * 鏍规嵁 batchCode 瑙f瀽 batchId
+     * 瑙f瀽骞跺~鍏呮壒娆�
+     *
+     * <p>鍚敤鎵规绠$悊鐨勭墿鏂欏嚭搴撴椂蹇呴』钀芥壒娆★紱鏈褰曟壒娆℃椂鎸夌墿鏂� + 瀹㈡埛 + 閿�鍞鍗曡嚜鍔ㄧ敓鎴愶紝
+     * 渚涘悗缁��璐х瓑涓氬姟杩芥函銆�
      */
-    private void fillBatchId(MesWmProductSalesLineSaveReqVO reqVO) {
-        if (StrUtil.isBlank(reqVO.getBatchCode())) {
+    private void fillBatchId(MesWmProductSalesDO sales, MesMdmItemDO item,
+                             MesWmProductSalesLineSaveReqVO reqVO) {
+        if (StrUtil.isNotBlank(reqVO.getBatchCode())) {
+            MesWmBatchDO batch = batchService.getBatchByCode(reqVO.getBatchCode());
+            if (batch != null) {
+                reqVO.setBatchId(batch.getId());
+                return;
+            }
+            if (!Boolean.TRUE.equals(item.getIsBatchManaged())) {
+                reqVO.setBatchId(null);
+                return;
+            }
+            // 鎸囧畾浜嗘壒娆″彿浣嗙郴缁熶腑涓嶅瓨鍦紝鎸夎鎵规鍙峰缓妗�
+            MesWmBatchDO created = batchService.getOrGenerateBatchCode(buildBatchGenerateReq(sales, reqVO));
+            reqVO.setBatchId(created != null ? created.getId() : null);
+            return;
+        }
+        if (!Boolean.TRUE.equals(item.getIsBatchManaged())) {
             reqVO.setBatchId(null);
             return;
         }
-        MesWmBatchDO batch = batchService.getBatchByCode(reqVO.getBatchCode());
-        reqVO.setBatchId(batch != null ? batch.getId() : null);
+        // 鏈褰曟壒娆★紝鑷姩鐢熸垚
+        MesWmBatchDO batch = batchService.getOrGenerateBatchCode(buildBatchGenerateReq(sales, reqVO));
+        reqVO.setBatchId(batch != null ? batch.getId() : null)
+                .setBatchCode(batch != null ? batch.getCode() : null);
+    }
+
+    private MesWmBatchGenerateReqVO buildBatchGenerateReq(MesWmProductSalesDO sales,
+                                                          MesWmProductSalesLineSaveReqVO reqVO) {
+        return new MesWmBatchGenerateReqVO()
+                .setItemId(reqVO.getItemId())
+                .setBatchCode(reqVO.getBatchCode())
+                .setClientId(sales.getClientId())
+                .setSalesOrderCode(sales.getSalesOrderCode());
     }
 
     /**
@@ -153,11 +217,11 @@
         // 鏍¢獙涓诲崟瀛樺湪涓斾负鑽夌鐘舵��
         MesWmProductSalesDO sales = productSalesService.validateProductSalesExistsAndDraft(reqVO.getSalesId());
         // 鏍¢獙鐗╂枡瀛樺湪
-        mdmItemService.validateItemExistsAndEnable(reqVO.getItemId());
+        MesMdmItemDO item = mdmItemService.validateItemExistsAndEnable(reqVO.getItemId());
         // 鏍¢獙鍏宠仈鍙戣揣閫氱煡鍗曡锛堝瓨鍦ㄦ�� + 褰掑睘 + 瀛楁涓�鑷存�э級
         validateSalesNoticeLine(sales, reqVO);
-        // 鏍规嵁 batchCode 瑙f瀽 batchId
-        fillBatchId(reqVO);
+        // 瑙f瀽鎵规
+        fillBatchId(sales, item, reqVO);
     }
 
     /**
@@ -175,7 +239,8 @@
             }
             MesWmSalesNoticeLineDO noticeLine = salesNoticeLineService.validateSalesNoticeLineExists(
                     noticeLineId, sales.getNoticeId());
-            // 鏍¢獙鍏抽敭瀛楁涓�鑷存�э細鐗╂枡銆佹暟閲忋�佹壒娆″彿銆丱QC 妫�楠屾爣璇�
+            // 鏍¢獙鍏抽敭瀛楁涓�鑷存�э細鐗╂枡銆佹暟閲忋�佹壒娆″彿
+            // 娉細OQC 妫�楠屾爣璇嗗厑璁稿湪鍑哄簱鏃剁嫭绔嬭缃紝涓嶅己鍒朵笌閫氱煡鍗曚竴鑷�
             if (ObjUtil.notEqual(reqVO.getItemId(), noticeLine.getItemId())) {
                 throw exception(WM_PRODUCT_SALES_LINE_NOTICE_LINE_ITEM_MISMATCH);
             }
@@ -186,10 +251,6 @@
             if (StrUtil.isNotBlank(noticeLine.getBatchCode())
                     && ObjUtil.notEqual(reqVO.getBatchCode(), noticeLine.getBatchCode())) {
                 throw exception(WM_PRODUCT_SALES_LINE_NOTICE_LINE_BATCH_MISMATCH);
-            }
-            if (noticeLine.getOqcCheckFlag() != null
-                    && ObjUtil.notEqual(reqVO.getOqcCheckFlag(), noticeLine.getOqcCheckFlag())) {
-                throw exception(WM_PRODUCT_SALES_LINE_NOTICE_LINE_OQC_MISMATCH);
             }
             return;
         }
@@ -204,7 +265,7 @@
     @Transactional(rollbackFor = Exception.class)
     public void updateProductSalesLineWhenOqcFinish(Long id, Long oqcId, Integer checkResult) {
         // 1. 鏇存柊琛岀殑 oqcId + qualityStatus
-        Integer qualityStatus = Objects.equals(checkResult, MesQcCheckResultEnum.PASS.getType())
+        Integer qualityStatus = Objects.equals(checkResult, MesQcCheckResultEnum.QUALIFIED.getType())
                 ? MesWmQualityStatusEnum.PASS.getStatus() : MesWmQualityStatusEnum.FAIL.getStatus();
         productSalesLineMapper.updateById(new MesWmProductSalesLineDO()
                 .setId(id).setOqcId(oqcId).setQualityStatus(qualityStatus));

--
Gitblit v1.9.3