| | |
| | | import cn.lili.common.security.context.UserContext; |
| | | import cn.lili.common.utils.StringUtils; |
| | | import cn.lili.modules.lmk.domain.entity.*; |
| | | import cn.lili.modules.lmk.domain.form.AddPrizeNumForm; |
| | | import cn.lili.modules.lmk.domain.query.PrizeRecordTimeQuery; |
| | | import cn.lili.modules.lmk.domain.vo.PrizeDetailVO; |
| | | import cn.lili.modules.lmk.domain.vo.PrizeProbabilityVO; |
| | | import cn.lili.modules.lmk.domain.vo.PrizeRecordTimeVO; |
| | | import cn.lili.modules.lmk.enums.general.*; |
| | | import cn.lili.modules.lmk.service.*; |
| | | import cn.lili.mybatis.BaseEntity; |
| | | import cn.lili.rocketmq.RocketmqSendCallbackBuilder; |
| | | import cn.lili.rocketmq.tags.CommentTagsEnum; |
| | | import cn.lili.utils.COSUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | |
| | | import java.time.LocalTime; |
| | | import java.time.ZoneId; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | private final RedissonClient redissonClient; |
| | | private final RocketmqCustomProperties rocketmqCustomProperties; |
| | | private final RocketMQTemplate rocketMQTemplate; |
| | | private final COSUtil cosUtil; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result prize(String prizeId) { |
| | |
| | | refPrizes = activityRefPrizeService.list(prizeRefQuery); |
| | | canPrizeMap = refPrizes.stream().filter(item -> { |
| | | return item.getRemainNum() > 0; |
| | | }).collect(Collectors.toMap(ActivityRefPrize::getPrizeId, Function.identity())); |
| | | }).collect(Collectors.toMap(ActivityRefPrize::getId, Function.identity())); |
| | | prizeRecordTimeQuery = new PrizeRecordTimeQuery(); |
| | | prizeRecordTimeQuery.setStartTime(beginTime); |
| | | prizeRecordTimeQuery.setEndTime(endTime); |
| | | prizeRecordTimeQuery.setRecordActivityId(prizeId); |
| | | prizeRecordListByTime = prizeRecordService.getPrizeRecordListByTime(prizeRecordTimeQuery); |
| | | prizeRecordListByTime.stream().filter(item->{ |
| | | return item.getPrizeId() != null; |
| | | prizeRecordListByTime.stream().filter(item -> { |
| | | return item.getActivityPrizeRefId() != null; |
| | | }).forEach(item -> { |
| | | ActivityRefPrize activityRefPrize = canPrizeMap.get(Long.parseLong(item.getPrizeId())); |
| | | ActivityRefPrize activityRefPrize = canPrizeMap.get(item.getActivityPrizeRefId()); |
| | | if (activityRefPrize != null) { |
| | | //移除当日上限的奖品并且添加到不能抽奖商品中去 |
| | | if (activityRefPrize.getMaxPreDay() <= item.getTotal()) { |
| | | canPrizeMap.remove(Long.parseLong(item.getPrizeId())); |
| | | canPrizeMap.remove(item.getActivityPrizeRefId()); |
| | | } |
| | | } |
| | | }); |
| | |
| | | BigDecimal probabilityBegin = BigDecimal.ZERO; |
| | | for (ActivityRefPrize item : refPrizeList) { |
| | | PrizeProbabilityVO prizeProbabilityVO = new PrizeProbabilityVO(); |
| | | prizeProbabilityVO.setPrizeId(item.getPrizeId()); |
| | | prizeProbabilityVO.setActivityPrizeRefId(item.getId()); |
| | | BigDecimal multiply = item.getPrizeProbability().multiply(BigDecimal.valueOf(100)); |
| | | BigDecimal[][] position = {{probabilityBegin, multiply.add(probabilityBegin)}}; |
| | | prizeProbabilityVO.setProbability(position); |
| | |
| | | BigDecimal minP = prizeProbabilityVO.getProbability()[0][0]; |
| | | BigDecimal maxP = prizeProbabilityVO.getProbability()[0][1]; |
| | | if (bigDecimal.compareTo(minP) > 0 && bigDecimal.compareTo(maxP) <= 0) { |
| | | prizeWon = prizeProbabilityVO.getPrizeId(); |
| | | prizeWon = prizeProbabilityVO.getActivityPrizeRefId(); |
| | | break; |
| | | } |
| | | } |
| | |
| | | if (!update) { |
| | | throw new ServiceException("当前活动太火爆了请稍后再试"); |
| | | } |
| | | PrizeDraw prizeDraw = prizeDrawService.getById(prizeWon); |
| | | PrizeDraw prizeDraw = prizeDrawService.getById(activityRefPrize.getPrizeId()); |
| | | //写入抽奖记录 |
| | | PrizeRecord prizeRecord = new PrizeRecord(); |
| | | prizeRecord.setUserId(Long.parseLong(userId)); |
| | |
| | | prizeRecord.setPrizeNumId(Long.parseLong(waitUserPrize.getId())); |
| | | prizeRecord.setActivityPrizeRefId(Long.parseLong(activityRefPrize.getId())); |
| | | prizeRecord.setDistributeStatus(PrizeDistributeStatusEnum.WAIT.name()); |
| | | prizeRecord.setPrizeId(prizeWon); |
| | | prizeRecord.setPrizeId(activityRefPrize.getPrizeId()); |
| | | prizeRecord.setPrizeName(prizeDraw.getPrizeName()); |
| | | prizeRecord.setPrizeActivityCover(prizeActivity.getActivityCover()); |
| | | prizeRecord.setPrizeImg(prizeDraw.getPrizeImg()); |
| | |
| | | if (activity == null) { |
| | | throw new RuntimeException("当前活动不存在"); |
| | | } |
| | | Date activityEndtime = activity.getEndTime(); |
| | | if (new Date().after(activityEndtime)) { |
| | | throw new ServiceException("当前活动已经结束"); |
| | | } |
| | | if (!PrizeActivityStatusEnum.ON.name().equals(activity.getEnableStatus())) { |
| | | throw new ServiceException("活动还没有启用"); |
| | | } |
| | | //获取用户抽奖次数 |
| | | Integer prizeNum = activity.getPrizeNum(); |
| | | //加锁判断添加系统赠送次数 |
| | |
| | | if (useNum >= maxPrize) { |
| | | return Result.ok().data(0); |
| | | } else { |
| | | return Result.ok().data(userPrizeNum>maxPrize?maxPrize-useNum:notUseNum); |
| | | return Result.ok().data(userPrizeNum > maxPrize ? maxPrize - useNum : notUseNum); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public Result prizeInfo(String prizeActivityId) { |
| | | if (StringUtils.isBlank(prizeActivityId)) { |
| | | return Result.ok().data(0); |
| | | } |
| | | PrizeDetailVO data = prizeActivityService.prizeInfo(prizeActivityId); |
| | | String activityCover = data.getActivityCover(); |
| | | if (StringUtils.isNotBlank(activityCover) && !activityCover.contains("http")) { |
| | | data.setActivityCover(cosUtil.getPreviewUrl(activityCover)); |
| | | } |
| | | data.getPrizeInfoVOS().forEach(info -> { |
| | | String prizeImg = info.getPrizeImg(); |
| | | String prizeCover = info.getPrizeCover(); |
| | | if (StringUtils.isNotBlank(prizeImg) && !prizeImg.contains("http")) { |
| | | info.setPrizeImg(cosUtil.getPreviewUrl(prizeImg)); |
| | | } |
| | | if (StringUtils.isNotBlank(prizeCover) && !prizeCover.contains("http")) { |
| | | info.setPrizeCover(cosUtil.getPreviewUrl(prizeCover)); |
| | | } |
| | | }); |
| | | return Result.ok().data(data); |
| | | } |
| | | |
| | | @Override |
| | | public Result grantRecord(String prizeActivityId) { |
| | | return prizeDrawService.grantRecord(prizeActivityId); |
| | | } |
| | | |
| | | @Override |
| | | public Result addPrizeNum(AddPrizeNumForm addPrizeNumForm) { |
| | | PrizeActivity activity = prizeActivityService.getById(addPrizeNumForm.getPrizeActivityId()); |
| | | if (activity == null) { |
| | | throw new ServiceException("当前活动不存在"); |
| | | } |
| | | if (!PrizeActivityStatusEnum.ON.name().equals(activity.getEnableStatus())) { |
| | | throw new ServiceException("当前活动没开启"); |
| | | } |
| | | Date date = new Date(); |
| | | //活动结束不需要添加 |
| | | if (date.after(activity.getEndTime())) { |
| | | throw new ServiceException("当前活动已结束"); |
| | | } |
| | | Integer maxPrize = activity.getMaxPrize(); |
| | | String userId = addPrizeNumForm.getUserId(); |
| | | List<PrizeNumber> prizeNumberList = getPrizeNumberList(activity.getId(), userId); |
| | | if (prizeNumberList.size() >= maxPrize) { |
| | | throw new ServiceException("抽奖次数已达到上限了"); |
| | | } |
| | | PrizeUserActionEnum actionEnum = PrizeUserActionEnum.select(addPrizeNumForm.getAddType()); |
| | | if (actionEnum == null) { |
| | | throw new ServiceException("当前类型不存在"); |
| | | } |
| | | Boolean check ; |
| | | //todo 进行数据校验 |
| | | switch (actionEnum) { |
| | | case BUY: |
| | | System.err.println("BUY"); |
| | | break; |
| | | case SHARE: |
| | | //直接获得抽奖次数 |
| | | System.err.println("SHARE"); |
| | | break; |
| | | case SEE_SHOP: |
| | | System.err.println("SEE_SHOP"); |
| | | break; |
| | | case SEE_VIDEO: |
| | | System.err.println("SEE_VIDEO"); |
| | | break; |
| | | default: |
| | | |
| | | break; |
| | | } |
| | | return null; |
| | | } |
| | | } |