From 423ac2a5e7e451248d8cdfc2cda3f32dba0ec8f8 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期三, 11 三月 2026 17:59:52 +0800
Subject: [PATCH] feat: 生产计划关联物料信息表

---
 src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java |   89 ++++++++++++++++++++++++++++----------------
 1 files changed, 57 insertions(+), 32 deletions(-)

diff --git a/src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java b/src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java
index f0584d7..6025b24 100644
--- a/src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java
+++ b/src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java
@@ -15,7 +15,9 @@
 import com.ruoyi.common.utils.http.HttpUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.framework.config.AliDingConfig;
+import com.ruoyi.production.pojo.ProductMaterial;
 import com.ruoyi.production.pojo.ProductOrder;
+import com.ruoyi.production.service.ProductMaterialService;
 import com.ruoyi.production.service.ProductOrderService;
 import com.ruoyi.productionPlan.dto.ProductionPlanDto;
 import com.ruoyi.productionPlan.dto.ProductionPlanImportDto;
@@ -69,6 +71,9 @@
     @Autowired
     private ProductOrderPlanMapper productOrderPlanMapper;
 
+    @Autowired
+    private ProductMaterialService productMaterialService;
+
     /**
      * 鍚屾閿侊紝纭繚鎵嬪姩鍜屽畾鏃朵换鍔′笉鍚屾椂鎵ц
      */
@@ -107,6 +112,7 @@
 
         //  鏌ヨ涓荤敓浜ц鍒�
         List<ProductionPlan> plans = productionPlanMapper.selectBatchIds(productionPlanDto.getIds());
+        plans.sort(Comparator.comparingLong(ProductionPlan::getId));
 
         //  鏍¢獙鏄惁瀛樺湪涓嶅悓鐨勪骇鍝佸悕绉�
         String firstProductName = plans.get(0).getProductName();
@@ -121,37 +127,14 @@
         }
 
 
-        // 鍙犲姞鏂规暟
-        BigDecimal totalVolume = plans.stream()
-                .map(ProductionPlan::getVolume)
+        // 鍙犲姞鍓╀綑鏂规暟
+        BigDecimal totalRemainingVolume = plans.stream()
+                .map(ProductionPlan::getRemainingVolume)
                 .filter(v -> v != null)
                 .reduce(BigDecimal.ZERO, BigDecimal::add);
-        // 鍒ゆ柇涓嬪彂鏁伴噺鏄惁澶т簬绛変簬鏂规暟
-        if (productionPlanDto.getTotalAssignedQuantity().compareTo(totalVolume) > 0) {
-
-            log.warn("鎿嶄綔澶辫触锛屼笅鍙戞暟閲忎笉鑳藉ぇ浜庢柟鏁�");
-            return false;
-        }
-
-        // 鏍规嵁涓嬪彂鏁伴噺锛屼粠绗竴涓敓浜ц鍒掑紑濮嬪垎閰嶆柟鏁�
-        BigDecimal assignedVolume = BigDecimal.ZERO;
-        for (ProductionPlan plan : plans) {
-            BigDecimal volume = plan.getVolume();
-            if (volume == null) {
-                continue;
-            }
-
-            if (assignedVolume.add(volume).compareTo(productionPlanDto.getTotalAssignedQuantity()) >= 0) {
-                // 鏈�鍚庝竴涓鍒掞紝鍒嗛厤鍓╀綑鏂规暟
-                plan.setAssignedQuantity(productionPlanDto.getTotalAssignedQuantity().subtract(assignedVolume));
-                productionPlanMapper.updateById(plan);
-                break;
-            }
-
-            // 鍒嗛厤褰撳墠璁″垝鏂规暟
-            plan.setAssignedQuantity(volume);
-            productionPlanMapper.updateById(plan);
-            assignedVolume = assignedVolume.add(volume);
+        // 鍒ゆ柇涓嬪彂鏁伴噺鏄惁澶т簬绛変簬鍓╀綑鏂规暟
+        if (productionPlanDto.getTotalAssignedQuantity().compareTo(totalRemainingVolume) > 0) {
+            throw new BaseException("鎿嶄綔澶辫触锛屼笅鍙戞暟閲忎笉鑳藉ぇ浜庡墿浣欐柟鏁�");
         }
 
         // 鍒涘缓鐢熶骇璁㈠崟
@@ -160,11 +143,41 @@
         productOrder.setPlanCompleteTime(productionPlanDto.getPlanCompleteTime());
         productOrderService.addProductOrder(productOrder);
 
-        for (Long planId : productionPlanDto.getIds()) {
+        // 鏍规嵁涓嬪彂鏁伴噺锛屼粠绗竴涓敓浜ц鍒掑紑濮嬪垎閰嶆柟鏁�
+        BigDecimal assignedVolume = BigDecimal.ZERO;
+        for (ProductionPlan plan : plans) {
+            BigDecimal volume = plan.getVolume();
+            if (volume == null) {
+                continue;
+            }
+            // 璁$畻鍓╀綑鏂规暟
+            BigDecimal remainingVolume = plan.getRemainingVolume();
+            if (remainingVolume.compareTo(BigDecimal.ZERO) <= 0) {
+                continue;
+            }
+
             ProductOrderPlan productOrderPlan = new ProductOrderPlan();
             productOrderPlan.setProductOrderId(productOrder.getId());
-            productOrderPlan.setProductionPlanId(planId);
+            productOrderPlan.setProductionPlanId(plan.getId());
+
+            if (assignedVolume.add(remainingVolume).compareTo(productionPlanDto.getTotalAssignedQuantity()) >= 0) {
+                // 鏈�鍚庝竴涓鍒掞紝鍒嗛厤鍓╀綑鏂规暟
+                BigDecimal lastRemainingVolume = productionPlanDto.getTotalAssignedQuantity().subtract(assignedVolume);
+                plan.setAssignedQuantity(plan.getAssignedQuantity().add(lastRemainingVolume));
+                productOrderPlan.setAssignedQuantity(lastRemainingVolume);
+                productionPlanMapper.updateById(plan);
+                productOrderPlanMapper.insert(productOrderPlan);
+                break;
+            }
+
+            // 鍒嗛厤褰撳墠璁″垝鏂规暟
+            plan.setAssignedQuantity(plan.getAssignedQuantity().add(remainingVolume));
+            productOrderPlan.setAssignedQuantity(remainingVolume);
+            // 鏇存柊鐢熶骇璁″垝
+            productionPlanMapper.updateById(plan);
+            // 鍒涘缓鍏宠仈鍏崇郴
             productOrderPlanMapper.insert(productOrderPlan);
+            assignedVolume = assignedVolume.add(remainingVolume);
         }
         return true;
     }
@@ -372,7 +385,19 @@
                 plan.setApplyNo(formData.getString("textField_l7fytfco"));
                 plan.setCustomerName(formData.getString("textField_lbkozohg"));
 
-                plan.setMaterialCode(row.getString("textField_l9xo62q5"));
+                String materialCode = row.getString("textField_l9xo62q5");
+                plan.setMaterialCode(materialCode);
+
+                // 鏍规嵁鐗╂枡缂栫爜鏌ヨ鐗╂枡淇℃伅琛紝鍏宠仈鐗╂枡ID
+                if (StringUtils.isNotEmpty(materialCode)) {
+                    LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>();
+                    queryWrapper.eq(ProductMaterial::getMaterialCode, materialCode);
+                    ProductMaterial productMaterial = productMaterialService.getOne(queryWrapper);
+                    if (productMaterial != null) {
+                        plan.setProductMaterialId(productMaterial.getId());
+                    }
+                }
+
                 plan.setProductName(row.getString("textField_l9xo62q7"));
                 plan.setProductSpec(row.getString("textField_l9xo62q8"));
                 plan.setLength(row.getInteger("numberField_lb7lgatg_value"));

--
Gitblit v1.9.3