From 7bde96aaac4fcbd3afe4532e53f515f82c04a121 Mon Sep 17 00:00:00 2001
From: buhuazhen <hua100783@gmail.com>
Date: 星期一, 16 三月 2026 11:59:09 +0800
Subject: [PATCH] fix(production): 优化报工投入品数量计算及库存校验逻辑

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java         |   19 +++++++++++++++++--
 src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java |   30 ++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
index b31801d..15f2596 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java
@@ -160,7 +160,25 @@
         productionProductMain.setStatus(0);
         productionProductMainMapper.insert(productionProductMain);
         /*鏂板鎶ュ伐鎶曞叆琛�*/
+        List<ProductStructureDto> allProcess = productStructureMapper.listBybomId(productProcessRoute.getBomId());
         List<ProductStructureDto> productStructureDtos = productStructureMapper.listBybomAndProcess(productProcessRoute.getBomId(), productProcess.getId());
+
+        List<Long> lastProcessIds = new ArrayList<>();
+
+        if (!allProcess.isEmpty()) {
+            String lastProcessName = allProcess.get(allProcess.size() - 1).getProcessName();
+
+            for (int i = allProcess.size() - 1; i >= 0; i--) {
+                ProductStructureDto d = allProcess.get(i);
+
+                if (lastProcessName.equals(d.getProcessName())) {
+                    lastProcessIds.add(d.getId());
+                } else {
+                    break;
+                }
+            }
+        }
+
         if (productStructureDtos.size() == 0) {
             //濡傛灉璇ュ伐搴忔病鏈変骇鍝佺粨鏋勭殑鎶曞叆鍝�,閭h繖涓姇鍏ュ搧鍜屼骇鍑哄搧鏄悓涓�涓�
             ProductStructureDto productStructureDto = new ProductStructureDto();
@@ -171,12 +189,20 @@
         for (ProductStructureDto productStructureDto : productStructureDtos) {
             ProductModel productModel1 = productModelMapper.selectById(productStructureDto.getProductModelId());
             Product product = productMapper.selectById(productModel1.getProductId());
+
+            // 鍒ゆ柇鏄惁涓烘渶鍚庝竴閬撳伐搴�
+            BigDecimal needQuantity = dto.getQuantity().divide(productStructureDto.getUnitQuantity(), 2, BigDecimal.ROUND_HALF_UP);
+            if (lastProcessIds.contains(productStructureDto.getId())) {
+                needQuantity = dto.getQuantity().multiply(productStructureDto.getUnitQuantity());
+            }
+
+
             // 鏌ヨ浜у搧搴撳瓨
             BigDecimal stockQuantity = stockUtils.getStockQuantity(productModel1.getId()).get("stockQuantity");
             if (!(stockQuantity.compareTo(BigDecimal.ZERO) > 0)) {
                 throw new RuntimeException(product.getProductName()+"浜у搧鐨�"+productModel1.getModel() + "鐨勮鏍煎簱瀛樹负0");
             }
-            if (stockQuantity.compareTo(productStructureDto.getUnitQuantity().multiply(dto.getQuantity())) < 0) {
+            if (stockQuantity.compareTo(needQuantity) < 0) {
                 throw new RuntimeException(product.getProductName()+"浜у搧鐨�"+productModel1.getModel() + "鐨勮鏍煎簱瀛樹笉瓒�");
             }
             // 鏌ヨ閲囪喘浜у搧
@@ -186,7 +212,7 @@
 
             ProductionProductInput productionProductInput = new ProductionProductInput();
             productionProductInput.setProductModelId(productStructureDto.getProductModelId());
-            productionProductInput.setQuantity(productStructureDto.getUnitQuantity().multiply(dto.getQuantity()));
+            productionProductInput.setQuantity(needQuantity);
             productionProductInput.setProductMainId(productionProductMain.getId());
             productionProductInputMapper.insert(productionProductInput);
             //瀵瑰簲鐨勫簱瀛樺嚭搴�
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 08f431c..3ba1104 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -31,6 +31,7 @@
 
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
@@ -365,12 +366,26 @@
             Assert.notNull(productStructureDto.getUnitQuantity(), "鍗曚綅浜у嚭鎵�闇�鏁伴噺涓嶅瓨鍦�");
             if (productStructureDto.getMaterialType().equals("0")) {
                 quantity = quantity.multiply(productStructureDto.getUnitQuantity());
-                resultMap.put(productStructureDto.getProcessId(), quantity);
+                // 绗竴閬撳伐搴�
+                BigDecimal result = quantity;
+//                if(i == 0){
+//                     result = quantity.divide(productStructureDto.getUnitQuantity(), 2, RoundingMode.HALF_UP);
+//                }
+                resultMap.put(productStructureDto.getProcessId(),result );
             } else {
-                resultMap.put(productStructureDto.getProcessId(), originalQuantity.multiply(productStructureDto.getUnitQuantity()));
+                if(Optional.ofNullable(productStructureDto.getProcessName()).orElse("").contains("鍖呰")){
+                    resultMap.put(productStructureDto.getProcessId(), quantity);
+                }else {
+                    resultMap.put(productStructureDto.getProcessId(), originalQuantity.multiply(productStructureDto.getUnitQuantity()));
+                }
+
             }
 
         }
+
+
+
+
         return resultMap;
     }
 

--
Gitblit v1.9.3