From 5feb2085f70f764d2a44dff707a3f57fa5fb306f Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期三, 19 六月 2024 15:27:00 +0800 Subject: [PATCH] bug修改 --- ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java | 199 +++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 166 insertions(+), 33 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 b2c3d61..f978336 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 @@ -3,6 +3,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.RedisConstants; +import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.exception.GlobalException; import com.ruoyi.common.security.service.TokenService; import com.ruoyi.goods.domain.Recipient; @@ -12,9 +13,12 @@ import com.ruoyi.goods.mapper.TGoodsMapper; import com.ruoyi.goods.service.ITGoodsService; import com.ruoyi.goods.service.ITOrderService; +import com.ruoyi.goods.vo.GoodDetailVO; import com.ruoyi.goods.vo.TGoodsVO; import com.ruoyi.study.api.domain.TUser; import com.ruoyi.study.api.feignClient.StudyClient; +import com.ruoyi.study.api.vo.UserPersonalCenterVO; +import com.ruoyi.system.api.model.LoginUserParent; import org.redisson.api.RSemaphore; import org.redisson.api.RedissonClient; import org.springframework.stereotype.Service; @@ -22,9 +26,9 @@ import javax.annotation.Resource; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; /** * <p> @@ -52,54 +56,167 @@ } @Override - public Map<String, Object> redeemNow(String goodId, Recipient recipient) { + public GoodDetailVO redeemNow(String goodId, Recipient recipient) { // 商品详情 TGoods goods = lambdaQuery().eq(TGoods::getId, goodId).one(); - Map<String, Object> result = new HashMap<>(8); - result.put("goodDetail", goods); - // 用户收货地址 - result.put("address", recipient); - // 库存预热,redisson分布式锁 - String key = String.format(RedisConstants.GOOD_STOCK, goods.getId()); - RSemaphore semaphore = redissonClient.getSemaphore(key); - semaphore.trySetPermits(goods.getSurplus()); + if (null == goods) { + throw new GlobalException("商品不存在!"); + } + // 商品总数量为 null,可无限兑换,不需要分布式锁进行控制 + if (null != goods.getTotal()) { + // 库存预热,redisson分布式锁 + 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 number = goods.getTotal() - sum; + if (number <= 0) { + throw new GlobalException("商品可兑换数量不足!"); + } + // redisson分布式锁 + RSemaphore semaphore = redissonClient.getSemaphore(key); + semaphore.trySetPermits(goods.getSurplus()); + semaphore.expire(Constants.SIXTY, TimeUnit.MINUTES); + } + return new GoodDetailVO(goods, recipient); + } + + @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(); + if (null == good) { + return R.exchangeError("商品不存在,请稍后重试!"); + } + // 校验用户积分是否足够兑换 + UserPersonalCenterVO userPersonalCenter = studyClient.userInfo().getData(); + if (null == userPersonalCenter) { + throw new GlobalException("用户信息获取失败!"); + } + TUser user = userPersonalCenter.getUser(); + 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 = 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 { + boolean result = exchangeGood(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服务) + TOrder order = orderInfo(goodExchange, recipient, number, goodId, needIntegral); + 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; } @Override @Transactional(rollbackFor = Exception.class) - public Object goodExchange(GoodExchangeDTO goodExchange, Recipient recipient) { + public R goodExchange1(GoodExchangeDTO goodExchange, Recipient recipient) { Integer number = goodExchange.getNumber(); Integer goodId = goodExchange.getGoodId(); TGoods good = this.getById(goodId); if (null == good) { - throw new GlobalException("商品不存在,请稍后重试!"); + return R.exchangeError("商品不存在,请稍后重试!"); + } + LoginUserParent loginUser1 = tokenService.getLoginUser1(); + if (null == loginUser1){ + return R.tokenError("登录失效"); } // 校验用户积分是否足够兑换 - TUser user = studyClient.userInfo().getData(); + TUser user = studyClient.userInfoParent().getData(); + if (null == user) { + throw new GlobalException("用户信息获取失败!"); + } int needIntegral = good.getIntegral() * number; if (user.getIntegral() < needIntegral) { - throw new GlobalException("兑换失败,当前剩余积分不足!"); + return R.exchangeError("兑换失败,当前剩余积分不足!"); } - // redisson分布式锁,防止超卖 - String key = String.format(RedisConstants.GOOD_STOCK, good.getId()); - RSemaphore semaphore = redissonClient.getSemaphore(key); - boolean tried = semaphore.tryAcquire(number); - // 兑换失败,库存不足 - if (!tried) { - throw new GlobalException("当前商品库存不足!"); + 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 = 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 { + boolean result = exchangeGood(goodExchange, recipient, number, goodId, needIntegral); + if (!result) { + return R.exchangeError("商品兑换失败!"); + } } - // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务) - TOrder order = orderInfo(goodExchange, recipient, number, goodId, needIntegral); - if (!orderService.save(order)) { - throw new GlobalException("订单生成失败!"); - } - // 远程调用,生成积分明细 - Boolean boo = studyClient.addIntegralDetail(Constants.BURDEN + needIntegral, Constants.SHOPPING_CONSUME).getData(); - if (!boo) { - throw new GlobalException("生成积分明细失败,请稍后重试!"); - } - return true; + return R.ok(); } private TOrder orderInfo(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, Integer goodId, int needIntegral) { @@ -117,4 +234,20 @@ order.setDisabled(Boolean.FALSE); return order; } + + private TOrder orderInfo1(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.setIntegral(needIntegral); + order.setConsigneeName(recipient.getRecipient()); + order.setConsigneePhone(recipient.getRecipientPhone()); + order.setConsigneeAddress(recipient.getAddress()); + order.setDisabled(Boolean.FALSE); + return order; + } } -- Gitblit v1.7.1