New file |
| | |
| | | package cn.lili.listener; |
| | | |
| | | import cn.lili.cache.Cache; |
| | | import cn.lili.common.enums.ResultCode; |
| | | import cn.lili.common.exception.ServiceException; |
| | | import cn.lili.elasticsearch.EsSuffix; |
| | | import cn.lili.modules.lmk.domain.dto.VideoEsUpdateDTO; |
| | | import cn.lili.modules.lmk.domain.entity.*; |
| | | import cn.lili.modules.lmk.domain.es.VideoIndex; |
| | | import cn.lili.modules.lmk.enums.general.PrizeDistributeStatusEnum; |
| | | import cn.lili.modules.lmk.enums.general.PrizeGrantStatusEnums; |
| | | import cn.lili.modules.lmk.enums.general.PrizeNumberUseEnum; |
| | | import cn.lili.modules.lmk.enums.general.PrizeStatusEnum; |
| | | import cn.lili.modules.lmk.service.*; |
| | | import cn.lili.modules.promotion.service.MemberCouponService; |
| | | import cn.lili.rocketmq.tags.VideoTagsEnum; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.rocketmq.common.message.MessageExt; |
| | | import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; |
| | | import org.apache.rocketmq.spring.core.RocketMQListener; |
| | | import org.redisson.api.RLock; |
| | | import org.redisson.api.RedissonClient; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * 抽奖活动 |
| | | * |
| | | * @author paulG |
| | | * @since 2020/12/9 |
| | | **/ |
| | | @Component |
| | | @Slf4j |
| | | @RocketMQMessageListener(topic = "${lili.data.rocketmq.prize-topic}", consumerGroup = "${lili.data.rocketmq.prize-group}") |
| | | public class PrizeMessageListener implements RocketMQListener<MessageExt> { |
| | | @Autowired |
| | | private PrizeDrawService prizeDrawService; |
| | | @Autowired |
| | | private PrizeRecordService prizeRecordService; |
| | | @Autowired |
| | | private PrizeNumberService prizeNumberService; |
| | | @Autowired |
| | | private PrizeGrantRecordService prizeGrantRecordService; |
| | | @Autowired |
| | | private MemberCouponService memberCouponService; |
| | | @Autowired |
| | | private RedissonClient redissonClient; |
| | | |
| | | private final static String PRIZE_GRANT_KEY = "prize_grant_key:"; |
| | | @Override |
| | | public void onMessage(MessageExt messageExt) { |
| | | try { |
| | | String msg = new String(messageExt.getBody()); |
| | | log.info("收到了抽奖信息{}",msg); |
| | | switch (PrizeStatusEnum.valueOf(messageExt.getTags())) { |
| | | case WIN: |
| | | PrizeRecord prizeRecord = JSON.parseObject(msg, PrizeRecord.class); |
| | | RLock lock = redissonClient.getLock(PRIZE_GRANT_KEY + prizeRecord.getId()); |
| | | try { |
| | | lock.lock(); |
| | | PrizeRecord source = prizeRecordService.getById(prizeRecord.getId()); |
| | | //当前中奖记录不存在直接返回 |
| | | if (source == null) { |
| | | log.info("没有对应的中奖{}", prizeRecord.getId()); |
| | | return; |
| | | } |
| | | //奖品不是待发放状态直接返回 |
| | | if (!PrizeDistributeStatusEnum.WAIT.name().equals(source.getDistributeStatus())) { |
| | | log.info("发放状态不是待发放{}", prizeRecord.getId()); |
| | | return; |
| | | } |
| | | //抽奖卷是否一致 |
| | | if (!prizeRecord.getPrizeNumId().equals(source.getPrizeNumId())) { |
| | | log.info("抽奖卷id不一致{}", prizeRecord.getId()); |
| | | return; |
| | | } |
| | | PrizeDraw prizeDraw = prizeDrawService.getById(prizeRecord.getPrizeId()); |
| | | if (prizeDraw == null) { |
| | | log.info("当前奖品不存在{}", prizeRecord.getId()); |
| | | return; |
| | | } |
| | | PrizeNumber number = prizeNumberService.getById(prizeRecord.getPrizeNumId()); |
| | | if (number == null) { |
| | | log.info("当前抽奖券不存在{}", prizeRecord.getId()); |
| | | return; |
| | | } |
| | | if (!PrizeNumberUseEnum.USED.name().equals(number.getUseStatus())) { |
| | | log.info("当前抽奖券不是已使用状态{}", prizeRecord.getId()); |
| | | return; |
| | | } |
| | | PrizeGrantRecord grantRecord = prizeGrantRecordService.getById(prizeRecord.getId()); |
| | | //当前奖品已经记录不需要发放 |
| | | if (grantRecord != null) { |
| | | log.info("当前中奖记录已经记录{}", prizeRecord.getId()); |
| | | return; |
| | | } |
| | | grantRecord = new PrizeGrantRecord(); |
| | | //设置id相同使用主键防住重复发放 |
| | | grantRecord.setId(prizeRecord.getId()); |
| | | grantRecord.setUserId(prizeRecord.getUserId()); |
| | | grantRecord.setNickName(prizeRecord.getNickName()); |
| | | grantRecord.setActivityId(prizeRecord.getPrizeActivityId()); |
| | | grantRecord.setActivityName(prizeRecord.getPrizeActivityName()); |
| | | grantRecord.setPrizeNumId(prizeRecord.getPrizeNumId()); |
| | | grantRecord.setPrizeName(prizeRecord.getPrizeName()); |
| | | grantRecord.setPrizeContent(prizeRecord.getPrizeContent()); |
| | | grantRecord.setGrantStatus(PrizeGrantStatusEnums.SUCCESS.name()); |
| | | grantRecord.setPrizeId(prizeRecord.getPrizeId()); |
| | | source.setDistributeStatus(PrizeDistributeStatusEnum.SUCCESS.name()); |
| | | prizeGrantRecordService.save(grantRecord); |
| | | try { |
| | | memberCouponService.receiveCoupon(prizeDraw.getCouponId(), prizeRecord.getUserId() + "", prizeRecord.getNickName()); |
| | | } catch (Exception e) { |
| | | log.error("奖品发放失败",e); |
| | | log.error("奖品发放失败失败的id是{}", prizeRecord.getId()); |
| | | //报错就直接发放失败 |
| | | source.setDistributeStatus(PrizeDistributeStatusEnum.FAILED.name()); |
| | | grantRecord.setGrantStatus(PrizeGrantStatusEnums.FAILED.name()); |
| | | if (e instanceof ServiceException) { |
| | | ResultCode resultCode = ((ServiceException) e).getResultCode(); |
| | | if (ResultCode.COUPON_NOT_EXIST.equals(resultCode)) { |
| | | grantRecord.setDes(ResultCode.COUPON_NOT_EXIST.message()); |
| | | } else if (ResultCode.COUPON_RECEIVE_ERROR.equals(resultCode)) { |
| | | grantRecord.setDes(ResultCode.COUPON_RECEIVE_ERROR.message()); |
| | | } else if (ResultCode.COUPON_NUM_INSUFFICIENT_ERROR.equals(resultCode)) { |
| | | grantRecord.setDes(ResultCode.COUPON_NUM_INSUFFICIENT_ERROR.message()); |
| | | } else if (ResultCode.COUPON_LIMIT_ERROR.equals(resultCode)) { |
| | | grantRecord.setDes(ResultCode.COUPON_LIMIT_ERROR.message()); |
| | | } |
| | | } |
| | | } |
| | | prizeGrantRecordService.updateById(grantRecord); |
| | | //更新中奖记录表中的状态 |
| | | prizeRecordService.updateById(source); |
| | | break; |
| | | } finally { |
| | | if (lock.isHeldByCurrentThread()) { |
| | | lock.unlock(); |
| | | } |
| | | } |
| | | default: |
| | | log.error("奖品发放没有匹配的标签"); |
| | | break; |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("奖品发放异常", e); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |