From 1511296b46ddc0ed8a285d2d19e5491ef3506488 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期一, 21 九月 2026 23:23:17 +0800
Subject: [PATCH] feat: 首检字段

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java |   55 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
index 038396d..38013f1 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -4,6 +4,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.AmountUtils;
 import com.ruoyi.framework.web.domain.R;
 import com.ruoyi.procurementrecord.utils.StockUtils;
@@ -38,6 +39,7 @@
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.Set;
@@ -177,12 +179,9 @@
             result = salesLedgerProductMapper.insert(salesLedgerProduct);
             addProductionData(salesLedgerProduct);
         } else {
-            //鏌ヨ鍘熸湰鐨勪骇鍝佸瀷鍙穒d
             result = salesLedgerProductMapper.updateById(salesLedgerProduct);
-            /*鍒犻櫎瀵瑰簲鐨勭敓浜ф暟鎹苟閲嶆柊鏂板*/
-            deleteProductionData(Arrays.asList(salesLedgerProduct.getId()));
-
-            addProductionData(salesLedgerProduct);
+            /*鍚屾瀵瑰簲鐨勭敓浜ф暟鎹細璁″垝鏈笅鍙戞椂鍘熷湴鏇存柊闇�姹傛暟閲忥紝淇濈暀鐢熶骇璁″垝鍙�*/
+            syncProductionData(salesLedgerProduct);
         }
 
         // 濡傛灉鎻掑叆鎴栨洿鏂版垚鍔燂紝骞朵笖鏈� salesLedgerId锛屾墠缁х画鏇存柊涓昏〃閲戦
@@ -220,7 +219,7 @@
      */
     public void addProductionData(SalesLedgerProduct salesLedgerProduct) {
         //鍏堝垽鏂浜у搧鏄惁闇�瑕佺敓浜�
-        if (salesLedgerProduct.getIsProduction()) {
+        if (Boolean.TRUE.equals(salesLedgerProduct.getIsProduction())) {
             SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerProduct.getSalesLedgerId());
             ProductionPlan productionPlan = new ProductionPlan();
             productionPlan.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
@@ -238,6 +237,38 @@
     }
 
     /**
+     * 鍚屾鐢熶骇鏁版嵁
+     * 闇�瑕佺敓浜э細鐢熶骇璁″垝宸蹭笅鍙戞椂绂佹淇敼锛涙湭涓嬪彂鏃跺師鍦版洿鏂伴渶姹傛暟閲忥紝淇濈暀鐢熶骇璁″垝鍙凤紱
+     * 鏀逛负璧板簱瀛橈細鍒犻櫎鏈笅鍙戠殑鐢熶骇璁″垝銆�
+     */
+    public void syncProductionData(SalesLedgerProduct salesLedgerProduct) {
+        if (salesLedgerProduct.getId() == null) {
+            return;
+        }
+        List<ProductionPlan> productionPlans = listProductionData(salesLedgerProduct.getId());
+        if (CollectionUtils.isEmpty(productionPlans)) {
+            //鍘熸潵涓嶉渶瑕佺敓浜э紝鐜板湪鏀逛负闇�瑕佺敓浜э紝琛ュ缓鐢熶骇璁″垝
+            if (Boolean.TRUE.equals(salesLedgerProduct.getIsProduction())) {
+                addProductionData(salesLedgerProduct);
+            }
+            return;
+        }
+        //鏀逛负璧板簱瀛橈紝鍒犻櫎鏈笅鍙戠殑鐢熶骇璁″垝
+        if (!Boolean.TRUE.equals(salesLedgerProduct.getIsProduction())) {
+            deleteProductionData(Collections.singletonList(salesLedgerProduct.getId()));
+            return;
+        }
+        if (productionPlans.stream().anyMatch(plan -> plan.getStatus() != null && plan.getStatus() != 0)) {
+            throw new ServiceException("鐢熶骇璁″垝宸蹭笅鍙戯紝涓嶈兘淇敼璇ラ攢鍞骇鍝�");
+        }
+        for (ProductionPlan productionPlan : productionPlans) {
+            productionPlan.setQtyRequired(salesLedgerProduct.getQuantity());
+            productionPlan.setProductModelId(salesLedgerProduct.getProductModelId());
+            productionPlanMapper.updateById(productionPlan);
+        }
+    }
+
+    /**
      * 鍒犻櫎鐢熶骇璁″垝
      */
     public void deleteProductionData(List<Long> productIds) {
@@ -246,12 +277,12 @@
         }
         List<ProductionPlan> productionPlans = productionPlanMapper.selectList(
                 new LambdaQueryWrapper<ProductionPlan>()
-                        .in(ProductionPlan::getSalesLedgerProductId, productIds.stream().map(Long::intValue).collect(Collectors.toList())));
+                        .in(ProductionPlan::getSalesLedgerProductId, productIds));
         if (CollectionUtils.isEmpty(productionPlans)) {
             return;
         }
         //濡傛灉鐢熶骇璁″垝宸蹭笅鍙戝垯涓嶈兘鍒犻櫎
-        if (productionPlans.stream().anyMatch(productionPlan -> productionPlan.getStatus() != 0)) {
+        if (productionPlans.stream().anyMatch(productionPlan -> productionPlan.getStatus() != null && productionPlan.getStatus() != 0)) {
             throw new RuntimeException("鐢熶骇璁″垝宸蹭笅鍙戯紝涓嶈兘鍒犻櫎璇ラ攢鍞骇鍝�");
         }
         List<Long> ids = productionPlans.stream().map(ProductionPlan::getId).collect(Collectors.toList());
@@ -259,6 +290,14 @@
     }
 
     /**
+     * 鏌ヨ閿�鍞骇鍝佸搴旂殑鐢熶骇璁″垝
+     */
+    private List<ProductionPlan> listProductionData(Long productId) {
+        return productionPlanMapper.selectList(new LambdaQueryWrapper<ProductionPlan>()
+                .eq(ProductionPlan::getSalesLedgerProductId, productId));
+    }
+
+    /**
      * 閫氱敤鏂规硶锛氭牴鎹富琛↖D鍜屽瓙琛ㄥ垪琛紝鏇存柊涓昏〃鐨勫悎鍚岄噾棰�
      */
     public <T, S> void updateMainContractAmount(

--
Gitblit v1.9.3