| | |
| | | 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; |
| | | import cn.lili.modules.lmk.service.ScanPrizeService; |
| | | import cn.lili.base.Result; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import cn.lili.modules.lmk.domain.form.ScanPrizeForm; |
| | |
| | | import lombok.RequiredArgsConstructor; |
| | | import cn.lili.utils.PageUtil; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.support.TransactionSynchronization; |
| | | import org.springframework.transaction.support.TransactionSynchronizationManager; |
| | | import org.springframework.util.Assert; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | 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 |
| | | public Result page(ScanPrizeQuery query) { |
| | | |
| | | IPage<ScanPrizeVO> page = PageUtil.getPage(query, ScanPrizeVO.class); |
| | | LambdaQueryWrapper<ScanPrize> wrapper = Wrappers.lambdaQuery(); |
| | | wrapper.eq(Objects.nonNull(query.getStoreId()), ScanPrize::getStoreId, query.getStoreId()); |
| | | wrapper.eq(StringUtils.isNotBlank(query.getStatus()), ScanPrize::getStatus, query.getStatus()); |
| | | wrapper.eq(StringUtils.isNotBlank(query.getGenerateStatus()), ScanPrize::getGenerateStatus, query.getGenerateStatus()); |
| | | baseMapper.getPage(page, query); |
| | | return Result.ok().data(page.getRecords()).total(page.getTotal()); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result generateStorePrize(String id) { |
| | | |
| | | RLock redissonLock = redissonClient.getLock(STORE_PRIZE_GENERATE + id); |
| | |
| | | prizeClaimRecord.setStoreId(scanPrize.getStoreId()); |
| | | prizeClaimRecord.setStoreName(scanPrize.getStoreName()); |
| | | prizeClaimRecord.setPrizeActivityId(scanPrize.getPrizeActivityId()); |
| | | prizeClaimRecord.setPrizeActivityName(scanPrize.getPrizeActivityNmae()); |
| | | prizeClaimRecord.setPrizeActivityName(scanPrize.getPrizeActivityName()); |
| | | prizeClaimRecord.setClaimStatus(ClaimStatusEnum.NOT_CLAIM.name()); |
| | | prizeClaimRecord.setNo(String.format("%08d", i)); |
| | | 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); |
| | | } |
| | | } |