From 754431328c667fc5bfecb3220a27c6afbaf6f5ea Mon Sep 17 00:00:00 2001
From: buhuazhen <hua100783@gmail.com>
Date: 星期一, 16 三月 2026 13:33:38 +0800
Subject: [PATCH] fix(production): 修复首末工序数量计算逻辑

---
 src/main/java/com/ruoyi/production/service/impl/ProductionProductMainServiceImpl.java |   52 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 45 insertions(+), 7 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..dc66033 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,33 @@
         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<>();
+        List<Long> firstProcessIds = new ArrayList<>();
+
+        if (!allProcess.isEmpty()) {
+            String lastProcessName = allProcess.get(allProcess.size() - 1).getProcessName();
+            String firstProcessName = allProcess.get(0).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;
+                }
+            }
+            for (ProductStructureDto d : allProcess) {
+                if (firstProcessName.equals(d.getProcessName())) {
+                    firstProcessIds.add(d.getId());
+                } else {
+                    break;
+                }
+            }
+        }
+
         if (productStructureDtos.size() == 0) {
             //濡傛灉璇ュ伐搴忔病鏈変骇鍝佺粨鏋勭殑鎶曞叆鍝�,閭h繖涓姇鍏ュ搧鍜屼骇鍑哄搧鏄悓涓�涓�
             ProductStructureDto productStructureDto = new ProductStructureDto();
@@ -171,12 +197,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()) || firstProcessIds.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 +220,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);
             //瀵瑰簲鐨勫簱瀛樺嚭搴�
@@ -195,11 +229,15 @@
             ProcurementRecordOut procurementRecordOut1 = procurementRecordOutMapper.selectCode(dateFormat.format(now));
             Long aLong = procurementRecordOut1 == null ? 1L : Long.valueOf(procurementRecordOut1.getCode().split("LS" + dateFormat.format(now))[1]);
             // 鏌ヨ閲囪喘鍏ュ簱璁板綍
-            List<ProcurementRecordStorage> procurementRecordStorages = procurementRecordMapper.selectList(new LambdaQueryWrapper<ProcurementRecordStorage>()
-                    .in(ProcurementRecordStorage::getSalesLedgerProductId, salesLedgerProducts.stream()
-                            .map(SalesLedgerProduct::getId)
-                            .collect(Collectors.toList()))
-                    .orderByDesc(ProcurementRecordStorage::getInboundNum));
+            LambdaQueryWrapper<ProcurementRecordStorage> procurementRecordStorageLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            if (!org.springframework.util.CollectionUtils.isEmpty(salesLedgerProducts)) {
+                procurementRecordStorageLambdaQueryWrapper.in(ProcurementRecordStorage::getSalesLedgerProductId, salesLedgerProducts.stream()
+                        .map(SalesLedgerProduct::getId)
+                        .collect(Collectors.toList()));
+            }
+
+            procurementRecordStorageLambdaQueryWrapper.orderByDesc(ProcurementRecordStorage::getInboundNum);
+            List<ProcurementRecordStorage> procurementRecordStorages = procurementRecordMapper.selectList(procurementRecordStorageLambdaQueryWrapper);
             if(CollectionUtils.isNotEmpty(procurementRecordStorages)) {
                 // 鐢熶骇鎵i櫎閲囪喘搴撳瓨鍘熸枡
                 BigDecimal quantity = productionProductInput.getQuantity();

--
Gitblit v1.9.3