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/production/service/impl/ProductionProductMainServiceImpl.java |   30 ++++++++++++++++++++++++++++--
 1 files changed, 28 insertions(+), 2 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);
             //瀵瑰簲鐨勫簱瀛樺嚭搴�

--
Gitblit v1.9.3