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 |   29 +++++++++++++++++++++--------
 1 files changed, 21 insertions(+), 8 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 3fa75da..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;
 
 /**
@@ -116,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