From 3481d209ec847542b73fa16616ffe0e13c949e80 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期六, 18 四月 2026 18:11:03 +0800
Subject: [PATCH] fix: 入库与出库数量绑定

---
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |   88 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
index 2d0ceae..3e3bafc 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -51,7 +51,13 @@
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
 import com.ruoyi.stock.dto.StockInventoryDto;
+import com.ruoyi.stock.mapper.StockInRecordMapper;
+import com.ruoyi.stock.mapper.StockOutRecordMapper;
+import com.ruoyi.stock.pojo.StockInRecord;
+import com.ruoyi.stock.pojo.StockOutRecord;
+import com.ruoyi.stock.service.StockInRecordService;
 import com.ruoyi.stock.service.StockInventoryService;
+import com.ruoyi.stock.service.StockOutRecordService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.BeanUtils;
@@ -154,6 +160,14 @@
     private SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
     @Autowired
     private StockInventoryService stockInventoryService;
+    @Autowired
+    private StockInRecordMapper stockInRecordMapper;
+    @Autowired
+    private StockOutRecordMapper stockOutRecordMapper;
+    @Autowired
+    private StockInRecordService stockInRecordService;
+    @Autowired
+    private StockOutRecordService stockOutRecordService;
     @Autowired
     private StockUtils stockUtils;
     @Value("${file.upload-dir}")
@@ -447,6 +461,34 @@
                 .eq(SalesLedgerProduct::getType, 2);
         List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductQueryWrapper);
         if (CollectionUtils.isNotEmpty(salesLedgerProducts)) {
+            List<Long> productIds = salesLedgerProducts.stream().map(SalesLedgerProduct::getId).filter(Objects::nonNull).collect(Collectors.toList());
+            // 鍒犻櫎鍙拌处鍓嶅厛鍥為��搴撳瓨璁板綍锛堝厛鍒犲嚭搴撳啀鍒犲叆搴擄級锛岄伩鍏嶅簱瀛樺洖閫�鏍¢獙琚鎷�
+            if (CollectionUtils.isNotEmpty(productIds)) {
+                List<Long> stockOutRecordIds = stockOutRecordMapper.selectList(new LambdaQueryWrapper<StockOutRecord>()
+                                .and(w -> w
+                                        .in(StockOutRecord::getSalesLedgerProductId, productIds)
+                                        .or(q -> q.in(StockOutRecord::getRecordId, productIds)
+                                                .in(StockOutRecord::getRecordType, Arrays.asList(
+                                                        StockOutUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_OUT.getCode()
+                                                ))))
+                                .select(StockOutRecord::getId))
+                        .stream().map(StockOutRecord::getId).collect(Collectors.toList());
+                if (CollectionUtils.isNotEmpty(stockOutRecordIds)) {
+                    stockOutRecordService.batchDelete(stockOutRecordIds);
+                }
+                List<Long> stockInRecordIds = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                                .and(w -> w
+                                        .in(StockInRecord::getSalesLedgerProductId, productIds)
+                                        .or(q -> q.in(StockInRecord::getRecordId, productIds)
+                                                .in(StockInRecord::getRecordType, Arrays.asList(
+                                                        StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode()
+                                                ))))
+                                .select(StockInRecord::getId))
+                        .stream().map(StockInRecord::getId).collect(Collectors.toList());
+                if (CollectionUtils.isNotEmpty(stockInRecordIds)) {
+                    stockInRecordService.batchDelete(stockInRecordIds);
+                }
+            }
             salesLedgerProducts.stream().forEach(salesLedgerProduct -> {
                 // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︿骇鍝�
                 LambdaQueryWrapper<ProcurementRecordStorage> queryWrapper = new LambdaQueryWrapper<>();
@@ -828,6 +870,9 @@
         if (purchaseLedger == null) {
             throw new ServiceException("鍏ュ簱澶辫触,閲囪喘鍙拌处涓嶅瓨鍦�");
         }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("鍏ュ簱澶辫触,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸叆搴�");
+        }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘鍏ュ簱澶辫触,鍏ュ簱浜у搧涓嶈兘涓虹┖");
         }
@@ -901,6 +946,9 @@
         if (purchaseLedger == null) {
             throw new ServiceException("鍑哄簱澶辫触,閲囪喘鍙拌处涓嶅瓨鍦�");
         }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("鍑哄簱澶辫触,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸嚭搴�");
+        }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘鍑哄簱澶辫触,鍑哄簱浜у搧涓嶈兘涓虹┖");
         }
@@ -938,12 +986,6 @@
             }
             stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
 
-            BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
-            BigDecimal newStocked = oldStocked.subtract(outboundThisLine);
-            if (newStocked.compareTo(BigDecimal.ZERO) < 0) {
-                newStocked = BigDecimal.ZERO;
-            }
-
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
             stockInventoryDto.setRecordId(dbProduct.getId());
             stockInventoryDto.setRecordType(StockOutQualifiedRecordTypeEnum.PURCHASE_SCAN_STOCK_OUT.getCode());
@@ -953,17 +995,8 @@
             stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
             stockInventoryService.subtractStockInventory(stockInventoryDto);
 
-            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
-            int lineStockStatus;
-            if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
-                lineStockStatus = 0;
-            } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
-                lineStockStatus = 1;
-            } else {
-                lineStockStatus = 2;
-            }
-            dbProduct.setStockedQuantity(newStocked);
-            dbProduct.setProductStockStatus(lineStockStatus);
+            BigDecimal oldShipped = dbProduct.getShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getShippedQuantity();
+            dbProduct.setShippedQuantity(oldShipped.add(outboundThisLine));
             dbProduct.fillRemainingQuantity();
             salesLedgerProductMapper.updateById(dbProduct);
         }
@@ -978,6 +1011,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸叆搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱浜у搧涓嶈兘涓虹┖");
@@ -1016,6 +1052,11 @@
             }
             stockUtils.addUnStock(null, null, dbProduct.getProductModelId(), inboundThisLine,
                     StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
+
+            BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 
@@ -1028,6 +1069,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸嚭搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱浜у搧涓嶈兘涓虹┖");
@@ -1064,9 +1108,19 @@
             if (dbProduct.getProductModelId() == null) {
                 throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍑哄簱");
             }
+            BigDecimal unStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            BigDecimal unShipped = dbProduct.getUnqualifiedShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedShippedQuantity();
+            BigDecimal canUnShip = unStocked.subtract(unShipped);
+            if (outboundThisLine.compareTo(canUnShip) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘澶т簬涓嶅悎鏍煎叆搴撴暟閲�");
+            }
             stockUtils.assertUnqualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
             stockUtils.subtractUnStock(null, null, dbProduct.getProductModelId(), outboundThisLine,
                     StockOutUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_OUT.getCode(), dbProduct.getId());
+
+            dbProduct.setUnqualifiedShippedQuantity(unShipped.add(outboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 

--
Gitblit v1.9.3