| | |
| | | return R.exchangeError("商品不存在,请稍后重试!"); |
| | | } |
| | | // 校验用户积分是否足够兑换 |
| | | if (tokenService.getLoginUser1() == null) { |
| | | return R.tokenError("登录失效"); |
| | | UserPersonalCenterVO userPersonalCenter = studyClient.userInfo().getData(); |
| | | if (null == userPersonalCenter) { |
| | | throw new GlobalException("用户信息获取失败!"); |
| | | } |
| | | TUser user = studyClient.getUserById(tokenService.getLoginUser1().getUserid()).getData(); |
| | | |
| | | TUser user = userPersonalCenter.getUser(); |
| | | int needIntegral = good.getIntegral() * number; |
| | | if (user.getIntegral() < needIntegral) { |
| | | return R.exchangeError("兑换失败,当前剩余积分不足!"); |
| | | } |
| | | // 检查用户兑换数量是否超过单用户最大兑换数量 |
| | | 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服务) |
| | | TOrder order = orderInfo1(goodExchange, recipient, number, goodId, needIntegral); |
| | | boolean result = orderService.save(order); |
| | | // 远程调用,生成积分明细 |
| | | result = result && studyClient.addIntegralDetail1(Constants.BURDEN + needIntegral, Constants.SHOPPING_CONSUME).getData(); |
| | | // 扣除用户积分 |
| | | result = result && studyClient.exchangeIntegral1(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); |
| | | 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(); |
| | | } |