From 2e0ab4a5f88bd3f947efd4f34db7f0981ab47351 Mon Sep 17 00:00:00 2001 From: liujie <1793218484@qq.com> Date: 星期五, 05 九月 2025 11:28:15 +0800 Subject: [PATCH] 修改查询 --- ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java | 113 +++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 71 insertions(+), 42 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..9d58a76 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 @@ -25,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; @@ -49,10 +50,14 @@ private ITOrderService orderService; @Resource private TokenService tokenService; - @Override public List<TGoodsVO> goodRecommend(Integer userId) { return baseMapper.goodRecommend(userId); + } + + @Override + public TGoods getById1(Integer goodsId) { + return this.baseMapper.getById1(goodsId); } @Override @@ -66,7 +71,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 +94,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 +129,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 +138,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) + "!"); @@ -167,12 +181,14 @@ .stream().mapToInt(Integer::intValue).sum(); } Integer userCount = good.getUserCount(); - if (good.getUserCount() > totalNumber) { + 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); @@ -182,41 +198,13 @@ } return R.ok(); } - - private Boolean exchangeGood(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, - 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; - } - @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); + TGoods good = lambdaQuery().eq(TGoods::getId, goodId).one(); if (null == good) { return R.exchangeError("商品不存在,请稍后重试!"); } @@ -246,11 +234,13 @@ } boolean canBuy = good.getTotal() - item >= number; if (!canBuy) { - throw new GlobalException("商品库存不足,兑换失败!"); + return R.exchangeError("商品库存不足,兑换失败!"); +// 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; @@ -259,8 +249,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) + "!"); @@ -278,9 +274,6 @@ } // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用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("商品兑换失败!"); @@ -299,12 +292,14 @@ .stream().mapToInt(Integer::intValue).sum(); } Integer userCount = good.getUserCount(); - if (good.getUserCount() > totalNumber) { + 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); @@ -314,6 +309,40 @@ } return R.ok(); } + private Boolean exchangeGood(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, + 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; + } + + + + @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(); -- Gitblit v1.7.1