From 5273de9efb900feb07a9a4f1426976e0580c623f Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期二, 02 七月 2024 10:29:37 +0800
Subject: [PATCH] bug修改

---
 ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java |  144 +++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 129 insertions(+), 15 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 f978336..41348e6 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
@@ -68,7 +68,10 @@
             String key = String.format(RedisConstants.GOOD_STOCK, goods.getId());
             List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getGoodsId, goodId)
                     .eq(TOrder::getDisabled, 0).list();
-            int sum = orderList.stream().map(TOrder::getCount).mapToInt(Integer::intValue).sum();
+            int sum = 0;
+            if (!orderList.isEmpty()) {
+                sum = orderList.stream().map(TOrder::getCount).mapToInt(Integer::intValue).sum();
+            }
             // 计算商品剩余可兑换数量
             int number = goods.getTotal() - sum;
             if (number <= 0) {
@@ -76,7 +79,7 @@
             }
             // redisson分布式锁
             RSemaphore semaphore = redissonClient.getSemaphore(key);
-            semaphore.trySetPermits(goods.getSurplus());
+            semaphore.trySetPermits(number);
             semaphore.expire(Constants.SIXTY, TimeUnit.MINUTES);
         }
         return new GoodDetailVO(goods, recipient);
@@ -102,12 +105,31 @@
             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 = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
-                    .stream().mapToInt(Integer::intValue).sum();
+            int totalNumber;
+            if (orderList.isEmpty()) {
+                totalNumber = 0;
+            } else {
+                // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
+                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) {
@@ -127,14 +149,32 @@
             }
             // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
             boolean result = exchangeGood(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 {
+            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 (good.getUserCount() > totalNumber) {
+                    int i = userCount - totalNumber;
+                    if (number > i) {
+                        return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
+                                i + "!");
+                    }
+                }
+            }
             boolean result = exchangeGood(goodExchange, recipient, number, goodId, needIntegral);
             if (!result) {
                 return R.exchangeError("商品兑换失败!");
@@ -147,11 +187,27 @@
                                  Integer goodId, int needIntegral) {
         // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
         TOrder order = orderInfo(goodExchange, recipient, number, goodId, needIntegral);
+        order.setProvince(recipient.getProvince());
+        order.setCity(recipient.getCity());
         boolean result = orderService.save(order);
         // 远程调用,生成积分明细
         result = result && studyClient.addIntegralDetail(Constants.BURDEN + needIntegral, Constants.SHOPPING_CONSUME).getData();
         // 扣除用户积分
         result = result && studyClient.exchangeIntegral(needIntegral, Constants.BURDEN).getData();
+        return result;
+    }
+
+    private Boolean exchangeGoodParent(GoodExchangeDTO goodExchange, Recipient recipient, Integer number,
+                                       Integer goodId, int needIntegral) {
+        // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
+        TOrder order = orderInfoParent(goodExchange, recipient, number, goodId, needIntegral);
+        order.setProvince(recipient.getProvince());
+        order.setCity(recipient.getCity());
+        boolean result = orderService.save(order);
+        // 远程调用,生成积分明细
+        result = result && studyClient.addIntegralDetailParent(Constants.BURDEN + needIntegral, Constants.SHOPPING_CONSUME).getData();
+        // 扣除用户积分
+        result = result && studyClient.exchangeIntegralParent(needIntegral, Constants.BURDEN).getData();
         return result;
     }
 
@@ -165,7 +221,7 @@
             return R.exchangeError("商品不存在,请稍后重试!");
         }
         LoginUserParent loginUser1 = tokenService.getLoginUser1();
-        if (null == loginUser1){
+        if (null == loginUser1) {
             return R.tokenError("登录失效");
         }
         // 校验用户积分是否足够兑换
@@ -178,12 +234,31 @@
             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 = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
-                    .stream().mapToInt(Integer::intValue).sum();
+            int totalNumber;
+            if (orderList.isEmpty()) {
+                totalNumber = 0;
+            } else {
+                // 该商品订单为空、订单数量未超过商品的单个用户兑换上限数量时,可以进行兑换
+                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) {
@@ -202,7 +277,7 @@
                 return R.exchangeError("当前商品库存不足");
             }
             // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
-            boolean result = exchangeGood(goodExchange, recipient, number, goodId, needIntegral);
+            boolean result = exchangeGoodParent(goodExchange, recipient, number, goodId, needIntegral);
             // 扣除库存
             result = result && this.lambdaUpdate().set(TGoods::getTotal, good.getTotal() - number)
                     .eq(TGoods::getId, good.getId()).update();
@@ -211,7 +286,28 @@
                 return R.exchangeError("商品兑换失败!");
             }
         } else {
-            boolean result = exchangeGood(goodExchange, recipient, number, goodId, needIntegral);
+            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 (good.getUserCount() > totalNumber) {
+                    int i = userCount - totalNumber;
+                    if (number > i) {
+                        return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
+                                i + "!");
+                    }
+                }
+            }
+            boolean result = exchangeGoodParent(goodExchange, recipient, number, goodId, needIntegral);
             if (!result) {
                 return R.exchangeError("商品兑换失败!");
             }
@@ -227,10 +323,28 @@
         order.setGoodsId(goodId);
         order.setCount(number);
         order.setState(1);
+        order.setRemark(goodExchange.getRemark());
         order.setIntegral(needIntegral);
         order.setConsigneeName(recipient.getRecipient());
         order.setConsigneePhone(recipient.getRecipientPhone());
-        order.setConsigneeAddress(recipient.getAddress());
+        order.setConsigneeAddress(recipient.getProvince() + recipient.getCity() + recipient.getAddress());
+        order.setDisabled(Boolean.FALSE);
+        return order;
+    }
+
+    private TOrder orderInfoParent(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, Integer goodId, int needIntegral) {
+        TOrder order = new TOrder();
+        order.setOrderNumber(goodExchange.getOrderNumber());
+        order.setUserId(tokenService.getLoginUser1().getUserid());
+        order.setInsertTime(new Date());
+        order.setGoodsId(goodId);
+        order.setCount(number);
+        order.setState(1);
+        order.setRemark(goodExchange.getRemark());
+        order.setIntegral(needIntegral);
+        order.setConsigneeName(recipient.getRecipient());
+        order.setConsigneePhone(recipient.getRecipientPhone());
+        order.setConsigneeAddress(recipient.getProvince() + recipient.getCity() + recipient.getAddress());
         order.setDisabled(Boolean.FALSE);
         return order;
     }

--
Gitblit v1.7.1