| | |
| | | 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 javax.annotation.Resource; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | } |
| | | |
| | | @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()); |
| | | return result; |
| | | return new GoodDetailVO(goods,recipient); |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (user.getIntegral() < needIntegral) { |
| | | 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(); |
| | | if (!canExchange) { |
| | | throw new GlobalException("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " + (good.getUserCount() - totalNumber) + "!"); |
| | | } |
| | | // redisson分布式锁,防止超卖 |
| | | String key = String.format(RedisConstants.GOOD_STOCK, good.getId()); |
| | | RSemaphore semaphore = redissonClient.getSemaphore(key); |
| | | boolean tried = semaphore.tryAcquire(number); |
| | | // 兑换失败,库存不足 |
| | | if (!tried) { |
| | | semaphore.release(number); |
| | | throw new GlobalException("当前商品库存不足!"); |
| | | } |
| | | // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务) |
| | | TOrder order = orderInfo(goodExchange, recipient, number, goodId, needIntegral); |
| | | if (!orderService.save(order)) { |
| | | throw new GlobalException("订单生成失败!"); |
| | | } |
| | | boolean result = orderService.save(order); |
| | | // 远程调用,生成积分明细 |
| | | Boolean boo = studyClient.addIntegralDetail(Constants.BURDEN + needIntegral, Constants.SHOPPING_CONSUME).getData(); |
| | | if (!boo) { |
| | | throw new GlobalException("生成积分明细失败,请稍后重试!"); |
| | | result = result && studyClient.addIntegralDetail(Constants.BURDEN + needIntegral, Constants.SHOPPING_CONSUME).getData(); |
| | | // 扣除用户积分 |
| | | result = result && studyClient.exchangeIntegral(needIntegral, Constants.BURDEN).getData(); |
| | | // 扣除库存 |
| | | result = result && this.lambdaUpdate().set(TGoods::getSurplus, good.getSurplus() - number).eq(TGoods::getId, good.getId()).update(); |
| | | if (!result) { |
| | | semaphore.release(number); |
| | | throw new GlobalException("商品兑换失败!"); |
| | | } |
| | | return true; |
| | | return result; |
| | | } |
| | | |
| | | private TOrder orderInfo(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, Integer goodId, int needIntegral) { |