From ccef20c2aa1bda974f2b7908cd106b46e556c86b Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 03 四月 2026 15:44:00 +0800
Subject: [PATCH] fix: 销售台账页面发货查询库存是否充足修改

---
 src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java     |   57 +++++++++++++++++++++++++++++++++++----------------------
 src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml |    3 +++
 2 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java b/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
index 77d7a84..aec1b42 100644
--- a/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
+++ b/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
@@ -30,6 +30,7 @@
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
@@ -279,29 +280,41 @@
         if(CollectionUtils.isEmpty(list)){
             throw new RuntimeException("璇ヤ骇鍝佷笉瀛樺湪");
         }
-        List<ProcurementPageDto> procurementPageDtoList = new ArrayList<>();
-        list.forEach(item -> {
-            ProcurementPageDto procurementDto = new ProcurementPageDto();
-            IPage<ProcurementPageDto> procurementPageDtoIPage = procurementRecordService.listPage(new Page<>(1, -1), procurementDto);
-            procurementPageDtoList.addAll(procurementPageDtoIPage.getRecords());
-        });
-        if(CollectionUtils.isEmpty(procurementPageDtoList)){
-            throw new RuntimeException("璇ヤ骇鍝佸簱瀛樹笉瀛樺湪");
+
+        Map<Long, BigDecimal> requiredQuantities = list.stream()
+                .filter(p -> p.getProductModelId() != null)
+                .collect(Collectors.groupingBy(
+                        SalesLedgerProduct::getProductModelId,
+                        Collectors.mapping(SalesLedgerProduct::getQuantity, Collectors.reducing(BigDecimal.ZERO, (a, b) -> a.add(b != null ? b : BigDecimal.ZERO)))
+                ));
+
+        for (Map.Entry<Long, BigDecimal> entry : requiredQuantities.entrySet()) {
+            Long modelId = entry.getKey();
+            BigDecimal neededQty = entry.getValue();
+
+            ProcurementPageDto queryDto = new ProcurementPageDto();
+            queryDto.setProductModelId(modelId);
+
+            // 鑾峰彇璇ヨ鏍煎瀷鍙风殑鎵�鏈夊簱瀛樿褰�
+            IPage<ProcurementPageDto> inventoryPage = procurementRecordService.listPage(new Page<>(1, -1), queryDto);
+            
+            // 姹囨�绘墍鏈夋壒娆$殑鍙敤搴撳瓨
+            BigDecimal totalStock = inventoryPage.getRecords().stream()
+                    .map(ProcurementPageDto::getInboundNum0)
+                    .filter(Objects::nonNull)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+            if (totalStock.compareTo(neededQty) < 0) {
+                String modelName = list.stream()
+                        .filter(p -> modelId.equals(p.getProductModelId()))
+                        .map(SalesLedgerProduct::getSpecificationModel)
+                        .findFirst()
+                        .orElse("鏈煡鍨嬪彿");
+                return AjaxResult.error("浜у搧 [" + modelName + "] 搴撳瓨涓嶈冻锛屾墍闇�: " + neededQty + ", 褰撳墠鎬诲簱瀛�: " + totalStock);
+            }
         }
-        AtomicInteger num = new AtomicInteger();
-        list.forEach(item -> {
-            procurementPageDtoList.forEach(procurementPageDto -> {
-                if(item.getProductModelId().equals(procurementPageDto.getProductModelId())){
-                    if (item.getQuantity().compareTo(procurementPageDto.getInboundNum0()) <= 0) {
-                        num.getAndIncrement();
-                    }
-                }
-            });
-        });
-        if(num.get() == list.size()){
-            return AjaxResult.success("璇ヤ骇鍝佸簱瀛樺厖瓒�");
-        }
-        return AjaxResult.error("璇ヤ骇鍝佸簱瀛樹笉瓒�");
+
+        return AjaxResult.success("璇ヤ骇鍝佸簱瀛樺厖瓒�");
     }
 
     /**
diff --git a/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml b/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml
index 3c8f870..a80c23f 100644
--- a/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml
+++ b/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml
@@ -70,6 +70,9 @@
             <if test="req.timeStr != null and req.timeStr != ''">
                 and t1.create_time like concat('%',#{req.timeStr},'%')
             </if>
+            <if test="req.productModelId != null">
+                and t1.product_model_id = #{req.productModelId}
+            </if>
         </where>
     </select>
 

--
Gitblit v1.9.3