From 02cf72a4443db4f1a2406c3919718c38aa6367db Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 24 四月 2026 14:59:31 +0800
Subject: [PATCH] fix: 领用、冻结与解冻数量限制不能超过最大值

---
 src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java |   49 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
index 2d5b8f5..f37c023 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
@@ -21,6 +21,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.servlet.http.HttpServletResponse;
+import java.math.BigDecimal;
 import java.util.List;
 
 /**
@@ -54,14 +55,20 @@
         stockInRecordDto.setStockInNum(stockUninventoryDto.getQualitity());
         stockInRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
         stockInRecordDto.setType("1");
+        stockInRecordDto.setSalesLedgerId(stockUninventoryDto.getSalesLedgerId());
+        stockInRecordDto.setSalesLedgerProductId(stockUninventoryDto.getSalesLedgerProductId());
         stockInRecordService.add(stockInRecordDto);
         //鍐嶈繘琛屾柊澧炲簱瀛樻暟閲忓簱瀛�
         //鍏堟煡璇㈠簱瀛樿〃涓殑浜у搧鏄惁瀛樺湪锛屼笉瀛樺湪鏂板锛屽瓨鍦ㄦ洿鏂�
-        StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(new QueryWrapper<StockUninventory>().lambda().eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId()));
+        List<StockUninventory> stockUninventories = stockUninventoryMapper.selectList(new QueryWrapper<StockUninventory>().lambda()
+                .eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId())
+                .orderByDesc(StockUninventory::getId));
+        StockUninventory oldStockUnInventory = (stockUninventories == null || stockUninventories.isEmpty()) ? null : stockUninventories.get(0);
         if (ObjectUtils.isEmpty(oldStockUnInventory)) {
             StockUninventory newStockUnInventory = new StockUninventory();
             newStockUnInventory.setProductModelId(stockUninventoryDto.getProductModelId());
             newStockUnInventory.setQualitity(stockUninventoryDto.getQualitity());
+            newStockUnInventory.setLockedQuantity(stockUninventoryDto.getLockedQuantity());
             newStockUnInventory.setVersion(1);
             newStockUnInventory.setRemark(stockUninventoryDto.getRemark());
             stockUninventoryMapper.insert(newStockUnInventory);
@@ -82,12 +89,20 @@
         stockOutRecordDto.setStockOutNum(stockUninventoryDto.getQualitity());
         stockOutRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
         stockOutRecordDto.setType("1");
+        stockOutRecordDto.setSalesLedgerId(stockUninventoryDto.getSalesLedgerId());
+        stockOutRecordDto.setSalesLedgerProductId(stockUninventoryDto.getSalesLedgerProductId());
         stockOutRecordService.add(stockOutRecordDto);
-        StockUninventory oldStockInventory = stockUninventoryMapper.selectOne(new QueryWrapper<StockUninventory>().lambda().eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId()));
+        List<StockUninventory> stockUninventories = stockUninventoryMapper.selectList(new QueryWrapper<StockUninventory>().lambda()
+                .eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId())
+                .orderByDesc(StockUninventory::getId));
+        StockUninventory oldStockInventory = (stockUninventories == null || stockUninventories.isEmpty()) ? null : stockUninventories.get(0);
         if (ObjectUtils.isEmpty(oldStockInventory)) {
             throw new RuntimeException("浜у搧搴撳瓨涓嶅瓨鍦�");
         }else {
-            stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto);
+            int affectRows = stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto);
+            if (affectRows <= 0) {
+                throw new RuntimeException("搴撳瓨涓嶈冻鏃犳硶鍑哄簱");
+            }
         }
         return 1;
     }
@@ -102,24 +117,36 @@
     @Override
     public Boolean frozenStock(StockInventoryDto stockInventoryDto) {
         StockUninventory stockUninventory = stockUninventoryMapper.selectById(stockInventoryDto.getId());
-        if (stockUninventory.getQualitity().compareTo(stockInventoryDto.getLockedQuantity())<0) {
-            throw new RuntimeException("鍐荤粨鏁伴噺涓嶈兘瓒呰繃搴撳瓨鏁伴噺");
+        if (ObjectUtils.isEmpty(stockUninventory)) {
+            throw new RuntimeException("搴撳瓨璁板綍涓嶅瓨鍦�");
         }
-        if (ObjectUtils.isEmpty(stockUninventory.getLockedQuantity())) {
-            stockUninventory.setLockedQuantity(stockInventoryDto.getLockedQuantity());
-        }else {
-            stockUninventory.setLockedQuantity(stockUninventory.getLockedQuantity().add(stockInventoryDto.getLockedQuantity()));
+        if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
+            throw new RuntimeException("鍐荤粨鏁伴噺蹇呴』澶т簬0");
         }
+        BigDecimal totalQty = ObjectUtils.isEmpty(stockUninventory.getQualitity()) ? BigDecimal.ZERO : stockUninventory.getQualitity();
+        BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockUninventory.getLockedQuantity()) ? BigDecimal.ZERO : stockUninventory.getLockedQuantity();
+        BigDecimal availableQty = totalQty.subtract(currentLockedQty);
+        if (stockInventoryDto.getLockedQuantity().compareTo(availableQty) > 0) {
+            throw new RuntimeException("鍐荤粨鏁伴噺涓嶈兘瓒呰繃鍙喕缁撳簱瀛樻暟閲�");
+        }
+        stockUninventory.setLockedQuantity(currentLockedQty.add(stockInventoryDto.getLockedQuantity()));
         return this.updateById(stockUninventory);
     }
 
     @Override
     public Boolean thawStock(StockInventoryDto stockInventoryDto) {
         StockUninventory stockUninventory = stockUninventoryMapper.selectById(stockInventoryDto.getId());
-        if (stockUninventory.getLockedQuantity().compareTo(stockInventoryDto.getLockedQuantity())<0) {
+        if (ObjectUtils.isEmpty(stockUninventory)) {
+            throw new RuntimeException("搴撳瓨璁板綍涓嶅瓨鍦�");
+        }
+        if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
+            throw new RuntimeException("瑙e喕鏁伴噺蹇呴』澶т簬0");
+        }
+        BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockUninventory.getLockedQuantity()) ? BigDecimal.ZERO : stockUninventory.getLockedQuantity();
+        if (currentLockedQty.compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
             throw new RuntimeException("瑙e喕鏁伴噺涓嶈兘瓒呰繃鍐荤粨鏁伴噺");
         }
-        stockUninventory.setLockedQuantity(stockUninventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
+        stockUninventory.setLockedQuantity(currentLockedQty.subtract(stockInventoryDto.getLockedQuantity()));
         return this.updateById(stockUninventory);
     }
 }

--
Gitblit v1.9.3