From f138375f15c072d10335c20ecec9b35f1da78ee4 Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期三, 03 七月 2024 16:17:59 +0800 Subject: [PATCH] bug修改 --- ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java | 129 +++++++++++++++++++++++++++++++++++++----- 1 files changed, 113 insertions(+), 16 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 e234a88..87a6610 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("商品兑换失败!"); @@ -158,6 +198,8 @@ 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(); @@ -170,6 +212,8 @@ 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(); @@ -201,14 +245,39 @@ 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(); - 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) + "!"); @@ -234,6 +303,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 (good.getUserCount() > 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("商品兑换失败!"); @@ -242,6 +334,11 @@ return R.ok(); } + @Override + public void updateOne(TGoods dto) { + this.baseMapper.updateOne(dto); + } + private TOrder orderInfo(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, Integer goodId, int needIntegral) { TOrder order = new TOrder(); order.setOrderNumber(goodExchange.getOrderNumber()); -- Gitblit v1.7.1