From 402aba359adb4b10cfa98ad7fcb424eb55641129 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期四, 04 七月 2024 15:54:52 +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 |  238 +++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 165 insertions(+), 73 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 04491b7..ed335dd 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();
@@ -108,22 +110,37 @@
             // 检查库存总数是否足够
             List<TOrder> order = orderService.lambdaQuery().eq(TOrder::getGoodsId, goodId)
                     .eq(TOrder::getDisabled, 0).list();
+            int item;
             if (!order.isEmpty()) {
-                int totalNumber = order.stream().map(TOrder::getCount).collect(Collectors.toList())
+                item = order.stream().map(TOrder::getCount).collect(Collectors.toList())
                         .stream().mapToInt(Integer::intValue).sum();
-                boolean canBuy = good.getTotal() - totalNumber >= number;
-                if (!canBuy) {
-                    throw new GlobalException("商品库存不足,兑换失败!");
-                }
+            } else {
+                item = 0;
+            }
+            boolean canBuy = good.getTotal() - item >= number;
+            if (!canBuy) {
+                throw new GlobalException("商品库存不足,兑换失败!");
             }
             // 检查用户兑换数量是否超过单用户最大兑换数量
             List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getUserId, user.getId())
                     .eq(TOrder::getGoodsId, goodId).list();
-            // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
-            int 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();
+            // 用户已兑换数量
+            int totalNumber;
+            if (orderList.isEmpty()) {
+                totalNumber = 0;
+            } else {
+                // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
+                totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
+                        .stream().mapToInt(Integer::intValue).sum();
+            }
+            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) + "!");
@@ -146,6 +163,29 @@
                 return R.exchangeError("商品兑换失败!");
             }
         } else {
+            if (null != good.getUserCount()) {
+                // 检查用户兑换数量是否超过单用户最大兑换数量
+                List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getUserId, user.getId())
+                        .eq(TOrder::getGoodsId, goodId).list();
+                int totalNumber;
+                if (orderList.isEmpty()) {
+                    totalNumber = 0;
+                } else {
+                    // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
+                    totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
+                            .stream().mapToInt(Integer::intValue).sum();
+                }
+                Integer userCount = good.getUserCount();
+                if (userCount > totalNumber) {
+                    int i = userCount - totalNumber;
+                    if (number > i) {
+                        return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
+                                i + "!");
+                    }
+                } else {
+                    return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量!");
+                }
+            }
             boolean result = exchangeGood(goodExchange, recipient, number, goodId, needIntegral);
             if (!result) {
                 return R.exchangeError("商品兑换失败!");
@@ -153,7 +193,116 @@
         }
         return R.ok();
     }
-
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R goodExchange1(GoodExchangeDTO goodExchange, Recipient recipient) {
+        // 用户本次兑换数量
+        Integer number = goodExchange.getNumber();
+        Integer goodId = goodExchange.getGoodId();
+        TGoods good = lambdaQuery().eq(TGoods::getId, goodId).one();
+        if (null == good) {
+            return R.exchangeError("商品不存在,请稍后重试!");
+        }
+        LoginUserParent loginUser1 = tokenService.getLoginUser1();
+        if (null == loginUser1) {
+            return R.tokenError("登录失效");
+        }
+        // 校验用户积分是否足够兑换
+        TUser user = studyClient.userInfoParent().getData();
+        if (null == user) {
+            throw new GlobalException("用户信息获取失败!");
+        }
+        int needIntegral = good.getIntegral() * number;
+        if (user.getIntegral() < needIntegral) {
+            return R.exchangeError("兑换失败,当前剩余积分不足!");
+        }
+        if (null != good.getTotal()) {
+            // 检查库存总数是否足够
+            List<TOrder> order = orderService.lambdaQuery().eq(TOrder::getGoodsId, goodId)
+                    .eq(TOrder::getDisabled, 0).list();
+            int item;
+            if (!order.isEmpty()) {
+                item = order.stream().map(TOrder::getCount).collect(Collectors.toList())
+                        .stream().mapToInt(Integer::intValue).sum();
+            } else {
+                item = 0;
+            }
+            boolean canBuy = good.getTotal() - item >= number;
+            if (!canBuy) {
+                throw new GlobalException("商品库存不足,兑换失败!");
+            }
+            // 检查用户兑换数量是否超过单用户最大兑换数量
+            List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getUserId, user.getId())
+                    .eq(TOrder::getGoodsId, goodId).list();
+            // 用户已兑换数量
+            int totalNumber;
+            if (orderList.isEmpty()) {
+                totalNumber = 0;
+            } else {
+                // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
+                totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
+                        .stream().mapToInt(Integer::intValue).sum();
+            }
+            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) + "!");
+            }
+            // redisson分布式锁,防止超卖
+            String key = String.format(RedisConstants.GOOD_STOCK, good.getId());
+            RSemaphore semaphore = redissonClient.getSemaphore(key);
+            // 请求超时时间 单位:毫秒
+            semaphore.trySetPermits(1000);
+            boolean tried = semaphore.tryAcquire(number);
+            // 兑换失败,库存不足
+            if (!tried) {
+                semaphore.release(number);
+                return R.exchangeError("当前商品库存不足");
+            }
+            // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
+            boolean result = exchangeGoodParent(goodExchange, recipient, number, goodId, needIntegral);
+            if (!result) {
+                semaphore.release(number);
+                return R.exchangeError("商品兑换失败!");
+            }
+        } else {
+            if (null != good.getUserCount()) {
+                // 检查用户兑换数量是否超过单用户最大兑换数量
+                List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getUserId, user.getId())
+                        .eq(TOrder::getGoodsId, goodId).list();
+                int totalNumber;
+                if (orderList.isEmpty()) {
+                    totalNumber = 0;
+                } else {
+                    // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
+                    totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
+                            .stream().mapToInt(Integer::intValue).sum();
+                }
+                Integer userCount = good.getUserCount();
+                if (userCount > totalNumber) {
+                    int i = userCount - totalNumber;
+                    if (number > i) {
+                        return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
+                                i + "!");
+                    }
+                } else {
+                    return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量!");
+                }
+            }
+            boolean result = exchangeGoodParent(goodExchange, recipient, number, goodId, needIntegral);
+            if (!result) {
+                return R.exchangeError("商品兑换失败!");
+            }
+        }
+        return R.ok();
+    }
     private Boolean exchangeGood(GoodExchangeDTO goodExchange, Recipient recipient, Integer number,
                                  Integer goodId, int needIntegral) {
         // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
@@ -182,68 +331,11 @@
         return result;
     }
 
+
+
     @Override
-    @Transactional(rollbackFor = Exception.class)
-    public R goodExchange1(GoodExchangeDTO goodExchange, Recipient recipient) {
-        Integer number = goodExchange.getNumber();
-        Integer goodId = goodExchange.getGoodId();
-        TGoods good = this.getById(goodId);
-        if (null == good) {
-            return R.exchangeError("商品不存在,请稍后重试!");
-        }
-        LoginUserParent loginUser1 = tokenService.getLoginUser1();
-        if (null == loginUser1) {
-            return R.tokenError("登录失效");
-        }
-        // 校验用户积分是否足够兑换
-        TUser user = studyClient.userInfoParent().getData();
-        if (null == user) {
-            throw new GlobalException("用户信息获取失败!");
-        }
-        int needIntegral = good.getIntegral() * number;
-        if (user.getIntegral() < needIntegral) {
-            return R.exchangeError("兑换失败,当前剩余积分不足!");
-        }
-        if (null != good.getTotal()) {
-            // 检查用户兑换数量是否超过单用户最大兑换数量
-            List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getUserId, user.getId())
-                    .eq(TOrder::getGoodsId, goodId).list();
-            // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
-            int 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();
-            if (!canExchange) {
-                return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
-                        (good.getUserCount() - totalNumber) + "!");
-            }
-            // redisson分布式锁,防止超卖
-            String key = String.format(RedisConstants.GOOD_STOCK, good.getId());
-            RSemaphore semaphore = redissonClient.getSemaphore(key);
-            // 请求超时时间 单位:毫秒
-            semaphore.trySetPermits(1000);
-            boolean tried = semaphore.tryAcquire(number);
-            // 兑换失败,库存不足
-            if (!tried) {
-                semaphore.release(number);
-                return R.exchangeError("当前商品库存不足");
-            }
-            // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
-            boolean result = exchangeGoodParent(goodExchange, recipient, number, goodId, needIntegral);
-            // 扣除库存
-            result = result && this.lambdaUpdate().set(TGoods::getTotal, good.getTotal() - number)
-                    .eq(TGoods::getId, good.getId()).update();
-            if (!result) {
-                semaphore.release(number);
-                return R.exchangeError("商品兑换失败!");
-            }
-        } else {
-            boolean result = exchangeGoodParent(goodExchange, recipient, number, goodId, needIntegral);
-            if (!result) {
-                return R.exchangeError("商品兑换失败!");
-            }
-        }
-        return R.ok();
+    public void updateOne(TGoods dto) {
+        this.baseMapper.updateOne(dto);
     }
 
     private TOrder orderInfo(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, Integer goodId, int needIntegral) {

--
Gitblit v1.7.1