From f7ec7c473f87cd086d2ade54adb86b7a32835870 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期二, 02 七月 2024 10:58:00 +0800
Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/DolphinEnglish

---
 ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java |   25 ++++++++++++++++++++-----
 1 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java b/ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java
index 41348e6..e967318 100644
--- a/ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java
+++ b/ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java
@@ -66,7 +66,8 @@
         if (null != goods.getTotal()) {
             // 库存预热,redisson分布式锁
             String key = String.format(RedisConstants.GOOD_STOCK, goods.getId());
-            List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getGoodsId, goodId)
+            List<TOrder> orderList = orderService.lambdaQuery()
+                    .eq(TOrder::getGoodsId, goodId)
                     .eq(TOrder::getDisabled, 0).list();
             int sum = 0;
             if (!orderList.isEmpty()) {
@@ -88,6 +89,7 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public R<String> goodExchange(GoodExchangeDTO goodExchange, Recipient recipient) {
+        // 用户本次兑换数量
         Integer number = goodExchange.getNumber();
         Integer goodId = goodExchange.getGoodId();
         TGoods good = lambdaQuery().eq(TGoods::getId, goodId).one();
@@ -122,6 +124,7 @@
             // 检查用户兑换数量是否超过单用户最大兑换数量
             List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getUserId, user.getId())
                     .eq(TOrder::getGoodsId, goodId).list();
+            // 用户已兑换数量
             int totalNumber;
             if (orderList.isEmpty()) {
                 totalNumber = 0;
@@ -130,8 +133,14 @@
                 totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
                         .stream().mapToInt(Integer::intValue).sum();
             }
-            boolean canExchange = orderList.isEmpty() || null == good.getUserCount() ||
-                    (totalNumber + number) <= good.getUserCount();
+            boolean canExchange = true;
+            if (null != good.getUserCount()) {
+                if (number > good.getUserCount()) {
+                    canExchange = false;
+                } else {
+                    canExchange = (totalNumber + number) <= good.getUserCount();
+                }
+            }
             if (!canExchange) {
                 return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
                         (good.getUserCount() - totalNumber) + "!");
@@ -259,8 +268,14 @@
                 totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
                         .stream().mapToInt(Integer::intValue).sum();
             }
-            boolean canExchange = orderList.isEmpty() || null == good.getUserCount() ||
-                    (totalNumber + number) <= good.getUserCount();
+            boolean canExchange = true;
+            if (null != good.getUserCount()) {
+                if (number > good.getUserCount()) {
+                    canExchange = false;
+                } else {
+                    canExchange = (totalNumber + number) <= good.getUserCount();
+                }
+            }
             if (!canExchange) {
                 return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
                         (good.getUserCount() - totalNumber) + "!");

--
Gitblit v1.7.1