From 69dc6b16ef04bdfbfa65f77c169c0847dc7e65c2 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 06 五月 2026 16:26:43 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New_pro' into dev_New_pro

---
 src/main/java/com/ruoyi/production/service/impl/ProductionPlanServiceImpl.java |   93 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 77 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionPlanServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionPlanServiceImpl.java
index 597ed34..b3ad742 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionPlanServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionPlanServiceImpl.java
@@ -73,31 +73,30 @@
             throw new ServiceException("涓嬪彂澶辫触锛屾湭閫夋嫨鐢熶骇璁″垝");
         }
 
-        List<ProductionPlanDto> plans = productionPlanMapper.selectWithMaterialByIds(planIds);
-        if (plans == null || plans.isEmpty() || plans.size() != planIds.size()) {
+        List<ProductionPlanDto> planLists = productionPlanMapper.selectWithMaterialByIds(planIds);
+        if (planLists == null || planLists.isEmpty() || planLists.size() != planIds.size()) {
             throw new ServiceException("涓嬪彂澶辫触锛岀敓浜ц鍒掍笉瀛樺湪鎴栧凡琚垹闄�");
         }
 
-        ProductionPlanDto firstPlan = plans.getFirst();
+        ProductionPlanDto firstPlan = planLists.getFirst();
         if (firstPlan.getProductModelId() == null) {
             throw new ServiceException("涓嬪彂澶辫触锛岀敓浜ц鍒掔己灏戜骇鍝佸瀷鍙�");
         }
 
-        boolean hasDifferentModel = plans.stream()
+        boolean hasDifferentModel = planLists.stream()
                 .anyMatch(item -> !Objects.equals(item.getProductModelId(), firstPlan.getProductModelId()));
         if (hasDifferentModel) {
             throw new BaseException("鍚堝苟澶辫触锛屾墍閫夌敓浜ц鍒掔殑浜у搧鍨嬪彿涓嶄竴鑷�");
         }
 
-        boolean hasIssuedPlan = plans.stream()
-                .anyMatch(item -> item.getStatus() != null && item.getStatus() != PLAN_STATUS_WAIT);
+        boolean hasIssuedPlan = planLists.stream()
+                .anyMatch(item -> item.getStatus() != null && item.getStatus() == PLAN_STATUS_ISSUED);
         if (hasIssuedPlan) {
             throw new BaseException("鍚堝苟澶辫触锛屾墍閫夌敓浜ц鍒掑瓨鍦ㄥ凡涓嬪彂鎴栭儴鍒嗕笅鍙戞暟鎹�");
         }
 
-        BigDecimal totalRequiredQuantity = plans.stream()
-                .map(ProductionPlan::getQtyRequired)
-                .filter(Objects::nonNull)
+        BigDecimal totalRequiredQuantity = planLists.stream()
+                .map(this::resolveRemainingQuantity)
                 .reduce(BigDecimal.ZERO, BigDecimal::add);
         if (totalRequiredQuantity.compareTo(BigDecimal.ZERO) <= 0) {
             throw new ServiceException("涓嬪彂澶辫触锛屾墍閫夌敓浜ц鍒掗渶姹傛�婚噺蹇呴』澶т簬0");
@@ -111,8 +110,25 @@
             throw new ServiceException("涓嬪彂澶辫触锛屼笅鍙戞暟閲忎笉鑳藉ぇ浜庤鍒掗渶姹傛�婚噺");
         }
 
+        BigDecimal remainingForOrderBind = assignedQuantity;
+        List<Long> issuedPlanIds = new ArrayList<>();
+        for (ProductionPlanDto plan : planLists) {
+            BigDecimal remainingQuantity = resolveRemainingQuantity(plan);
+            if (remainingForOrderBind.compareTo(BigDecimal.ZERO) <= 0 || remainingQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+                continue;
+            }
+            BigDecimal issueForThisPlan = remainingForOrderBind.min(remainingQuantity);
+            remainingForOrderBind = remainingForOrderBind.subtract(issueForThisPlan);
+            if (issueForThisPlan.compareTo(BigDecimal.ZERO) > 0) {
+                issuedPlanIds.add(plan.getId());
+            }
+        }
+        if (issuedPlanIds.isEmpty()) {
+            throw new ServiceException("Issue failed, no quantity available for dispatch");
+        }
+
         ProductionOrder productionOrder = new ProductionOrder();
-        productionOrder.setProductionPlanIds(formatPlanIds(planIds));
+        productionOrder.setProductionPlanIds(formatPlanIds(issuedPlanIds));
         productionOrder.setProductModelId(firstPlan.getProductModelId());
         productionOrder.setQuantity(assignedQuantity);
         productionOrder.setPlanCompleteTime(productionPlanDto.getPlanCompleteTime());
@@ -122,13 +138,30 @@
             throw new ServiceException("涓嬪彂澶辫触锛岀敓浜ц鍗曚繚瀛樺け璐�");
         }
 
-        int targetStatus = assignedQuantity.compareTo(totalRequiredQuantity) < 0 ? PLAN_STATUS_PARTIAL : PLAN_STATUS_ISSUED;
-        List<ProductionPlan> updates = planIds.stream().map(id -> {
+        //宸蹭笅鍙戞暟閲�
+        BigDecimal remainingAssignedQuantity = assignedQuantity;
+        List<ProductionPlan> updates = new ArrayList<>();
+        for (ProductionPlanDto plan : planLists) {
+            BigDecimal requiredQuantity = Optional.ofNullable(plan.getQtyRequired()).orElse(BigDecimal.ZERO);
+            if (requiredQuantity.compareTo(BigDecimal.ZERO) < 0) {
+                requiredQuantity = BigDecimal.ZERO;
+            }
+            BigDecimal remainingQuantity = resolveRemainingQuantity(plan);
+            BigDecimal historicalIssuedQuantity = requiredQuantity.subtract(remainingQuantity);
+            BigDecimal issuedQuantity = BigDecimal.ZERO;
+            if (remainingAssignedQuantity.compareTo(BigDecimal.ZERO) > 0 && remainingQuantity.compareTo(BigDecimal.ZERO) > 0) {
+                issuedQuantity = remainingAssignedQuantity.min(remainingQuantity);
+                remainingAssignedQuantity = remainingAssignedQuantity.subtract(issuedQuantity);
+            }
+
+            BigDecimal totalIssuedQuantity = historicalIssuedQuantity.add(issuedQuantity);
+            int planStatus = resolvePlanStatus(requiredQuantity, totalIssuedQuantity);
             ProductionPlan update = new ProductionPlan();
-            update.setId(id);
-            update.setStatus(targetStatus);
-            return update;
-        }).collect(Collectors.toList());
+            update.setId(plan.getId());
+            update.setStatus(planStatus);
+            update.setQuantityIssued(totalIssuedQuantity);
+            updates.add(update);
+        }
         if (!updates.isEmpty()) {
             this.updateBatchById(updates);
         }
@@ -261,6 +294,34 @@
         util.exportExcel(response, exportList, "涓荤敓浜ц鍒�");
     }
 
+    private BigDecimal resolveRemainingQuantity(ProductionPlan plan) {
+        if (plan == null) {
+            return BigDecimal.ZERO;
+        }
+        BigDecimal requiredQuantity = Optional.ofNullable(plan.getQtyRequired()).orElse(BigDecimal.ZERO);
+        if (requiredQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+            return BigDecimal.ZERO;
+        }
+        BigDecimal issuedQuantity = Optional.ofNullable(plan.getQuantityIssued()).orElse(BigDecimal.ZERO);
+        if (issuedQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+            return requiredQuantity;
+        }
+        if (issuedQuantity.compareTo(requiredQuantity) >= 0) {
+            return BigDecimal.ZERO;
+        }
+        return requiredQuantity.subtract(issuedQuantity);
+    }
+
+    private int resolvePlanStatus(BigDecimal requiredQuantity, BigDecimal issuedQuantity) {
+        if (requiredQuantity == null || requiredQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+            return PLAN_STATUS_WAIT;
+        }
+        if (issuedQuantity == null || issuedQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+            return PLAN_STATUS_WAIT;
+        }
+        return issuedQuantity.compareTo(requiredQuantity) < 0 ? PLAN_STATUS_PARTIAL : PLAN_STATUS_ISSUED;
+    }
+
     private String formatPlanIds(List<Long> planIds) {
         return planIds.stream()
                 .filter(Objects::nonNull)

--
Gitblit v1.9.3