| | |
| | | package cn.lili.modules.lmk.service.impl; |
| | | |
| | | import cn.lili.common.exception.ServiceException; |
| | | import cn.lili.common.security.AuthUser; |
| | | import cn.lili.common.security.context.UserContext; |
| | | import cn.lili.modules.lmk.domain.entity.PrizeClaimRecord; |
| | | import cn.lili.modules.lmk.domain.entity.StoreCoupon; |
| | | import cn.lili.modules.lmk.domain.entity.StoreCouponSingle; |
| | | import cn.lili.modules.lmk.domain.form.AddPrizeNumForm; |
| | | import cn.lili.modules.lmk.domain.vo.StorePrizeVO; |
| | | import cn.lili.modules.lmk.enums.general.*; |
| | | import cn.lili.modules.lmk.service.PrizeClaimRecordService; |
| | | import cn.lili.modules.lmk.service.PrizeService; |
| | | import cn.lili.modules.order.order.entity.enums.ClaimStatusEnum; |
| | | import cn.lili.utils.COSUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import cn.lili.modules.lmk.domain.entity.ScanPrize; |
| | | import cn.lili.modules.lmk.mapper.ScanPrizeMapper; |
| | |
| | | private final RedissonClient redissonClient; |
| | | private static final String STORE_PRIZE_GENERATE = "store_prize_generate:"; |
| | | private final PrizeClaimRecordService prizeClaimRecordService; |
| | | |
| | | private static final String STORE_PRIZE_CLAIM = "store_prize_claim:"; |
| | | private final PrizeService prizeService; |
| | | private final COSUtil cosUtil; |
| | | /** |
| | | * 添加 |
| | | * @param form |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result generateStorePrize(String id) { |
| | | |
| | | RLock redissonLock = redissonClient.getLock(STORE_PRIZE_GENERATE + id); |
| | |
| | | prizeClaimRecord.setMaterial(MaterialStatusEnum.NOT_GENERATE.name()); |
| | | return prizeClaimRecord; |
| | | } |
| | | @Override |
| | | public Result claimPrize(String id) { |
| | | AuthUser currentUser = UserContext.getCurrentUser(); |
| | | if (currentUser == null) { |
| | | throw new ServiceException("当前用户没有登录无法领取"); |
| | | } |
| | | String userId = currentUser.getId(); |
| | | String nickName = currentUser.getNickName(); |
| | | |
| | | //锁住礼品码id |
| | | RLock redissonLock = redissonClient.getLock(STORE_PRIZE_CLAIM + id); |
| | | try { |
| | | redissonLock.lock(); |
| | | LambdaQueryWrapper<PrizeClaimRecord> forUpdate = Wrappers.<PrizeClaimRecord>lambdaQuery() |
| | | .eq(PrizeClaimRecord::getId, id).last("FOR UPDATE"); |
| | | PrizeClaimRecord prizeClaimRecord = prizeClaimRecordService.getOne(forUpdate); |
| | | if (prizeClaimRecord == null) { |
| | | throw new ServiceException("当前领取抽奖机会二维码不存在"); |
| | | } |
| | | if (!ClaimStatusEnum.NOT_CLAIM.name().equals(prizeClaimRecord.getClaimStatus())) { |
| | | throw new ServiceException("当前领取抽奖机会状态异常"); |
| | | } |
| | | //更新被领取的记录 |
| | | prizeClaimRecord.setClaimStatus(ClaimStatusEnum.CLAIM.name()); |
| | | prizeClaimRecord.setUserId(userId); |
| | | prizeClaimRecord.setNickName(nickName); |
| | | LambdaQueryWrapper<ScanPrize> storeCoupQuery = Wrappers.<ScanPrize>lambdaQuery() |
| | | .eq(ScanPrize::getId, prizeClaimRecord.getStorePrizeId()).last("FOR UPDATE"); |
| | | ScanPrize scanPrize = this.getOne(storeCoupQuery); |
| | | AddPrizeNumForm addPrizeNumForm = new AddPrizeNumForm(); |
| | | addPrizeNumForm.setUserId(userId); |
| | | addPrizeNumForm.setPrizeActivityId(prizeClaimRecord.getPrizeActivityId()); |
| | | addPrizeNumForm.setAddType(PrizeUserActionEnum.USER_SCAN_STORE.name()); |
| | | prizeService.addPrizeNum(addPrizeNumForm); |
| | | if (scanPrize == null) { |
| | | throw new ServiceException("当前店铺抽奖卷不存在"); |
| | | } |
| | | if (!StoreScanPrizeStausEnum.ENABLE.name().equals(scanPrize.getStatus())) { |
| | | throw new ServiceException("当前店铺抽奖卷状态异常"); |
| | | } |
| | | //领取对应的优惠卷写入记录 |
| | | prizeClaimRecordService.updateById(prizeClaimRecord); |
| | | LambdaUpdateWrapper<ScanPrize> updateStoreCoupon = Wrappers.<ScanPrize>lambdaUpdate() |
| | | .eq(ScanPrize::getId, scanPrize.getId()) |
| | | .set(ScanPrize::getClaimNum, scanPrize.getClaimNum() + 1) |
| | | .ge(ScanPrize::getGenerateNum, scanPrize.getClaimNum() + 1); |
| | | boolean update = this.update(updateStoreCoupon); |
| | | if (!update) { |
| | | throw new ServiceException("领取失败"); |
| | | } |
| | | return Result.ok().data(0); |
| | | |
| | | } finally { |
| | | TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { |
| | | @Override |
| | | public void afterCommit() { |
| | | if (redissonLock.isHeldByCurrentThread()) { |
| | | redissonLock.unlock(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void afterCompletion(int status) { |
| | | // 确保即使在事务回滚的情况下也能释放锁 |
| | | if (redissonLock.isHeldByCurrentThread()) { |
| | | redissonLock.unlock(); |
| | | } |
| | | } |
| | | |
| | | }); |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Result getStorePrize(String id) { |
| | | StorePrizeVO storePrize = baseMapper.getStorePrize(id); |
| | | if (storePrize == null) { |
| | | throw new ServiceException("当前活动不存在"); |
| | | } |
| | | Boolean popup = storePrize.getPopup(); |
| | | if (!Boolean.TRUE.equals(popup)) { |
| | | throw new ServiceException("当前活动没有开启"); |
| | | } |
| | | if (!PrizeActivityStatusEnum.ON.name().equals(storePrize.getEnableStatus())) { |
| | | throw new ServiceException("当前活动没有开启"); |
| | | } |
| | | String activityCover = storePrize.getActivityCover(); |
| | | if (StringUtils.isNotBlank(activityCover)&&!activityCover.contains("http")) { |
| | | storePrize.setActivityCover(cosUtil.getPreviewUrl(activityCover)); |
| | | } |
| | | return Result.ok().data(storePrize); |
| | | } |
| | | } |