| | |
| | | * @Date 2025/5/8 18:05 |
| | | */ |
| | | @Service |
| | | public class LotteryEventServiceImpl extends ServiceImpl<LotteryEventMapper, LotteryEvent> implements ILotteryEventService { |
| | | public class LotteryEventServiceImpl extends ServiceImpl<LotteryEventMapper, TLotteryEvent> implements ILotteryEventService { |
| | | |
| | | @Resource |
| | | private IUserLotteryEventService userLotteryEventService; |
| | |
| | | @Override |
| | | public LotteryEventVo getLotteryEvent(String id) { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | LotteryEvent lotteryEvent = this.getById(id); |
| | | TLotteryEvent lotteryEvent = this.getById(id); |
| | | //构建返回结果 |
| | | LotteryEventVo vo = new LotteryEventVo(); |
| | | vo.setId(lotteryEvent.getId()); |
| | | vo.setName(lotteryEvent.getName()); |
| | | vo.setActivityProfile(lotteryEvent.getActivityProfile()); |
| | | List<UserLotteryEvent> userLotteryEvents = userLotteryEventService.list(new QueryWrapper<UserLotteryEvent>().eq("lottery_event_id", id)); |
| | | List<TUserLotteryEvent> userLotteryEvents = userLotteryEventService.list(new QueryWrapper<TUserLotteryEvent>().eq("lottery_event_id", id)); |
| | | vo.setLaveTimes(lotteryEvent.getTimes() - userLotteryEvents.size()); |
| | | //查询抽检活动奖品 |
| | | List<LotteryEventPrize> lotteryEventPrizeList = lotteryEventPrizeService.list(new QueryWrapper<LotteryEventPrize>().eq("lottery_event_id", id)); |
| | | List<TLotteryEventPrize> lotteryEventPrizeList = lotteryEventPrizeService.list(new QueryWrapper<TLotteryEventPrize>().eq("lottery_event_id", id)); |
| | | vo.setPrizeList(lotteryEventPrizeList); |
| | | //答题正确率 |
| | | if (5 == lotteryEvent.getActivityType()) { |
| | | UserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<UserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId)); |
| | | TUserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<TUserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId)); |
| | | if (null == questionsServiceOne) { |
| | | vo.setCorrectAnswerRate(questionsServiceOne.getCorrectAnswerRate()); |
| | | } |
| | |
| | | public R lotteryDraw(String id) { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | Member member = remoteMemberService.getMember(userId).getData(); |
| | | LotteryEvent lotteryEvent = this.getById(id); |
| | | TLotteryEvent lotteryEvent = this.getById(id); |
| | | //判断答题抽奖是否满足抽奖条件 |
| | | if (lotteryEvent.getActivityType() == 5) { |
| | | UserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<UserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId)); |
| | | TUserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<TUserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId)); |
| | | if (questionsServiceOne.getCorrectAnswerRate().compareTo(lotteryEvent.getAccuracy()) < 0) { |
| | | return R.fail("答题抽奖失败,答题正确率低于活动要求"); |
| | | } |
| | |
| | | boolean tryLock = lock.tryLock(30, TimeUnit.SECONDS); |
| | | if (tryLock) { |
| | | //判断抽奖次数是否用完 |
| | | int count = userLotteryEventService.count(new QueryWrapper<UserLotteryEvent>().eq("lottery_event_id", id).eq("user_id", userId)); |
| | | int count = userLotteryEventService.count(new QueryWrapper<TUserLotteryEvent>().eq("lottery_event_id", id).eq("user_id", userId)); |
| | | if (lotteryEvent.getTimes() <= count) { |
| | | return R.fail("抽奖次数已用完"); |
| | | } |
| | | List<LotteryEventPrize> lotteryEventPrizeList = lotteryEventPrizeService.list(new QueryWrapper<LotteryEventPrize>().eq("lottery_event_id", id)); |
| | | List<TLotteryEventPrize> lotteryEventPrizeList = lotteryEventPrizeService.list(new QueryWrapper<TLotteryEventPrize>().eq("lottery_event_id", id)); |
| | | //开始抽奖,根据中奖概率来抽奖 |
| | | List<LotteryEventPrize> list = new ArrayList<>(); |
| | | for (LotteryEventPrize lotteryEventPrize : lotteryEventPrizeList) { |
| | | List<TLotteryEventPrize> list = new ArrayList<>(); |
| | | for (TLotteryEventPrize lotteryEventPrize : lotteryEventPrizeList) { |
| | | int winRate = lotteryEventPrize.getWinRate().intValue(); |
| | | for (int i = 0; i < winRate; i++) { |
| | | LotteryEventPrize event = new LotteryEventPrize(); |
| | | TLotteryEventPrize event = new TLotteryEventPrize(); |
| | | BeanUtil.copyProperties(lotteryEventPrize, event); |
| | | list.add(event); |
| | | } |
| | |
| | | Collections.shuffle(list); |
| | | //开始获取随机数 |
| | | int random = new Random().nextInt(list.size()); |
| | | LotteryEventPrize lotteryEventPrize = lotteryEventPrizeList.get(random); |
| | | TLotteryEventPrize lotteryEventPrize = lotteryEventPrizeList.get(random); |
| | | //添加中奖商品 |
| | | UserLotteryEvent userLotteryEvent = new UserLotteryEvent(); |
| | | TUserLotteryEvent userLotteryEvent = new TUserLotteryEvent(); |
| | | userLotteryEvent.setId(IdUtils.simpleUUID()); |
| | | userLotteryEvent.setUserId(userId); |
| | | userLotteryEvent.setLotteryEventId(id); |
| | |
| | | @Transactional |
| | | public R editLotteryEvent(MgtLotteryEventEditDTO dto) { |
| | | //先检查开始-结束时间范围内,系统中是否有该活动 |
| | | List<LotteryEvent> lotteryEvents = this.baseMapper.selectList(new LambdaQueryWrapper<LotteryEvent>() |
| | | .eq(LotteryEvent::getDelFlag, 0)//未删除的 |
| | | .eq(LotteryEvent::getActivityType, dto.getActivityType())//类型相同的 |
| | | .between(LotteryEvent::getStartTime, dto.getStartTime(), dto.getEndTime())//开始时间在这个活动的(开始-结束)范围内的 |
| | | List<TLotteryEvent> lotteryEvents = this.baseMapper.selectList(new LambdaQueryWrapper<TLotteryEvent>() |
| | | .eq(TLotteryEvent::getDelFlag, 0)//未删除的 |
| | | .eq(TLotteryEvent::getActivityType, dto.getActivityType())//类型相同的 |
| | | .between(TLotteryEvent::getStartTime, dto.getStartTime(), dto.getEndTime())//开始时间在这个活动的(开始-结束)范围内的 |
| | | .or() |
| | | .between(LotteryEvent::getEndTime, dto.getStartTime(), dto.getEndTime())//结束时间在这个活动的(开始-结束)范围内的 |
| | | .between(TLotteryEvent::getEndTime, dto.getStartTime(), dto.getEndTime())//结束时间在这个活动的(开始-结束)范围内的 |
| | | ); |
| | | if (null != lotteryEvents && !lotteryEvents.isEmpty()) { |
| | | // 新增记录时直接返回错误 |
| | |
| | | return R.fail("编辑抽奖活动与现有其他活动时间范围冲突"); |
| | | } |
| | | } |
| | | LotteryEvent lotteryEvent = new LotteryEvent(); |
| | | TLotteryEvent lotteryEvent = new TLotteryEvent(); |
| | | if (null != dto.getId()) { |
| | | //编辑 |
| | | lotteryEvent=this.baseMapper.selectById(dto.getId()); |
| | | if (null == lotteryEvent||lotteryEvent.getDelFlag() != 0) { |
| | | if (null == lotteryEvent || lotteryEvent.getDelFlag() != 0) { |
| | | return R.fail("该抽奖活动已被删除"); |
| | | } |
| | | if (!lotteryEvent.getActivityType().equals(dto.getActivityType())) { |
| | | return R.fail("编辑抽奖活动不能修改开启方式"); |
| | | } |
| | | if(!lotteryEvent.getName().equals(dto.getName())) { |
| | | if (!lotteryEvent.getName().equals(dto.getName())) { |
| | | return R.fail("编辑抽奖活动不能修改抽奖名称"); |
| | | } |
| | | //答题类型 |
| | | if (lotteryEvent.getActivityType().equals(5)){ |
| | | //将之前的题干数据、答案选项数据删除 |
| | | if (lotteryEvent.getActivityType().equals(5)) { |
| | | //将之前的题干数据、答案选项数据删除 |
| | | //先删除答案选项数据 |
| | | lotteryEventQuestionsAnswersService.remove(new LambdaQueryWrapper<LotteryEventQuestionsAnswers>() |
| | | .eq(LotteryEventQuestionsAnswers::getLotteryEventId,lotteryEvent.getId())); |
| | | lotteryEventQuestionsAnswersService.remove(new LambdaQueryWrapper<TLotteryEventQuestionsAnswers>() |
| | | .eq(TLotteryEventQuestionsAnswers::getLotteryEventId, lotteryEvent.getId())); |
| | | //再删除题干数据 |
| | | lotteryEventQuestionsService.remove(new LambdaQueryWrapper<LotteryEventQuestions>() |
| | | .eq(LotteryEventQuestions::getLotteryEventId,lotteryEvent.getId())); |
| | | lotteryEventQuestionsService.remove(new LambdaQueryWrapper<TLotteryEventQuestions>() |
| | | .eq(TLotteryEventQuestions::getLotteryEventId, lotteryEvent.getId())); |
| | | } |
| | | //奖品数据删除 |
| | | /*lotteryEventPrizeService.remove(new LambdaQueryWrapper<LotteryEventPrize>() |
| | | .eq(LotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));*/ |
| | | /*lotteryEventPrizeService.remove(new LambdaQueryWrapper<TLotteryEventPrize>() |
| | | .eq(TLotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));*/ |
| | | lotteryEventPrizeService.deleteByLotteryEventId(lotteryEvent.getId()); |
| | | } |
| | | //新增 |
| | |
| | | lotteryEvent.setId(IdUtils.simpleUUID()); |
| | | lotteryEvent.setCreateTime(LocalDateTime.now()); |
| | | lotteryEvent.setCreateUserId(dto.getUserId()); |
| | | }else { |
| | | } else { |
| | | lotteryEvent.setUpdateTime(LocalDateTime.now()); |
| | | lotteryEvent.setUpdateUserId(dto.getUserId()); |
| | | } |
| | | //保存抽奖活动 |
| | | this.saveOrUpdate(lotteryEvent); |
| | | // 保存奖品数据 |
| | | LotteryEvent finalLotteryEvent = lotteryEvent;//jdk8及之后 变量在初始化后确实没有被重新赋值,可直接引用,无需显式声明为 final |
| | | List<LotteryEventPrize> prizeList = dto.getMgtLotteryEventPrizeDTOList().stream() |
| | | TLotteryEvent finalLotteryEvent = lotteryEvent;//jdk8及之后 变量在初始化后确实没有被重新赋值,可直接引用,无需显式声明为 final |
| | | List<TLotteryEventPrize> prizeList = dto.getMgtLotteryEventPrizeDTOList().stream() |
| | | .map(prizeDTO -> { |
| | | LotteryEventPrize prize = new LotteryEventPrize(); |
| | | TLotteryEventPrize prize = new TLotteryEventPrize(); |
| | | prize.setId(IdUtils.simpleUUID()); |
| | | prize.setLotteryEventId(finalLotteryEvent.getId()); |
| | | prize.setPrizeType(prizeDTO.getPrizeType()); |
| | | if (null != prizeDTO.getObjectId()){ |
| | | if (null != prizeDTO.getObjectId()) { |
| | | prize.setObjectId(prizeDTO.getObjectId()); |
| | | } |
| | | prize.setObjectName(prizeDTO.getObjectName()); |
| | |
| | | if (dto.getActivityType().equals(5)) { |
| | | //保存题干数据 |
| | | for (MgtLotteryEventQuestionDTO questionDTO : dto.getMgtLotteryQuestionDTOList()) { |
| | | LotteryEventQuestions question = new LotteryEventQuestions(); |
| | | TLotteryEventQuestions question = new TLotteryEventQuestions(); |
| | | question.setId(IdUtils.simpleUUID()); |
| | | question.setLotteryEventId(finalLotteryEvent.getId()); |
| | | question.setName(questionDTO.getName()); |
| | | question.setSort(questionDTO.getSort()); |
| | | lotteryEventQuestionsService.save(question); |
| | | //保存答案选项数据 |
| | | List<LotteryEventQuestionsAnswers> answersList = questionDTO.getAnswersDTOList().stream() |
| | | List<TLotteryEventQuestionsAnswers> answersList = questionDTO.getAnswersDTOList().stream() |
| | | .map(answersDTO -> { |
| | | LotteryEventQuestionsAnswers answer = new LotteryEventQuestionsAnswers(); |
| | | TLotteryEventQuestionsAnswers answer = new TLotteryEventQuestionsAnswers(); |
| | | answer.setId(IdUtils.simpleUUID()); |
| | | answer.setLotteryEventId(finalLotteryEvent.getId()); |
| | | answer.setLotteryEventQuestionsId(question.getId()); |
| | |
| | | |
| | | @Override |
| | | public R<MgtLotteryEventDetailVO> getLotteryEventDetailById(String id) { |
| | | LotteryEvent lotteryEvent = this.getById(id); |
| | | TLotteryEvent lotteryEvent = this.getById(id); |
| | | if (null == lotteryEvent) { |
| | | return R.fail("该抽奖活动不存在"); |
| | | } |
| | |
| | | } |
| | | if(lotteryEvent.getActivityType()==5){ |
| | | //题干数据 |
| | | List<LotteryEventQuestions> questionsList = lotteryEventQuestionsService.getBaseMapper().selectList(new LambdaQueryWrapper<LotteryEventQuestions>().eq(LotteryEventQuestions::getLotteryEventId, lotteryEvent.getId())); |
| | | List<TLotteryEventQuestions> questionsList = lotteryEventQuestionsService.getBaseMapper().selectList(new LambdaQueryWrapper<TLotteryEventQuestions>().eq(TLotteryEventQuestions::getLotteryEventId, lotteryEvent.getId())); |
| | | |
| | | List<MgtLotteryEventQuestionVO> questionsVOList = questionsList.stream().map(question -> { |
| | | MgtLotteryEventQuestionVO questionVO = new MgtLotteryEventQuestionVO(); |
| | | BeanUtils.copyProperties(question, questionVO); |
| | | //答案选项数据 |
| | | List<LotteryEventQuestionsAnswers> answersList = lotteryEventQuestionsAnswersService.getBaseMapper().selectList(new LambdaQueryWrapper<LotteryEventQuestionsAnswers>() |
| | | .eq(LotteryEventQuestionsAnswers::getLotteryEventId, lotteryEvent.getId()) |
| | | .eq(LotteryEventQuestionsAnswers::getLotteryEventQuestionsId, questionVO.getId())); |
| | | List<TLotteryEventQuestionsAnswers> answersList = lotteryEventQuestionsAnswersService.getBaseMapper().selectList(new LambdaQueryWrapper<TLotteryEventQuestionsAnswers>() |
| | | .eq(TLotteryEventQuestionsAnswers::getLotteryEventId, lotteryEvent.getId()) |
| | | .eq(TLotteryEventQuestionsAnswers::getLotteryEventQuestionsId, questionVO.getId())); |
| | | questionVO.setAnswersVOList(answersList); |
| | | return questionVO; |
| | | }).collect(Collectors.toList()); |
| | | //题干数据排序 |
| | | // 自定义 Comparator 实现 |
| | | Comparator<LotteryEventQuestions> sortComparator = new Comparator<LotteryEventQuestions>() { |
| | | Comparator<TLotteryEventQuestions> sortComparator = new Comparator<TLotteryEventQuestions>() { |
| | | @Override |
| | | public int compare(LotteryEventQuestions q1, LotteryEventQuestions q2) { |
| | | public int compare(TLotteryEventQuestions q1, TLotteryEventQuestions q2) { |
| | | return Integer.compare(q1.getSort(), q2.getSort()); |
| | | } |
| | | }; |
| | |
| | | vo.setMgtLotteryQuestionVOList(questionsVOList); |
| | | } |
| | | //奖项信息 |
| | | /*List<LotteryEventPrize> prizeList = lotteryEventPrizeService.getBaseMapper().selectList(new LambdaQueryWrapper<LotteryEventPrize>() |
| | | .eq(LotteryEventPrize::getLotteryEventId, lotteryEvent.getId()));*/ |
| | | List<LotteryEventPrize> prizeList = lotteryEventPrizeService.getPrizeListByLotteryEventId(lotteryEvent.getId()); |
| | | /*List<TLotteryEventPrize> prizeList = lotteryEventPrizeService.getBaseMapper().selectList(new LambdaQueryWrapper<TLotteryEventPrize>() |
| | | .eq(TLotteryEventPrize::getLotteryEventId, lotteryEvent.getId()));*/ |
| | | List<TLotteryEventPrize> prizeList = lotteryEventPrizeService.getPrizeListByLotteryEventId(lotteryEvent.getId()); |
| | | vo.setLotteryEventPrizeList(prizeList); |
| | | |
| | | |
| | | return R.ok(vo); |
| | | } |
| | | |
| | | @Override |
| | | public R deleteMgtLotteryEvent(String id) { |
| | | LotteryEvent lotteryEvent = this.getById(id); |
| | | TLotteryEvent lotteryEvent = this.getById(id); |
| | | if (null == lotteryEvent || lotteryEvent.getDelFlag()!=0) { |
| | | return R.ok(); |
| | | } |
| | | |
| | | //1.奖品数据删除 |
| | | /* lotteryEventPrizeService.remove(new LambdaQueryWrapper<LotteryEventPrize>() |
| | | .eq(LotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));*/ |
| | | /* lotteryEventPrizeService.remove(new LambdaQueryWrapper<TLotteryEventPrize>() |
| | | .eq(TLotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));*/ |
| | | lotteryEventPrizeService.deleteByLotteryEventId(lotteryEvent.getId()); |
| | | //2.答题类型 |
| | | if (lotteryEvent.getActivityType().equals(5)){ |
| | | if (lotteryEvent.getActivityType().equals(5)) { |
| | | //先删除答案选项数据 |
| | | lotteryEventQuestionsAnswersService.remove(new LambdaQueryWrapper<LotteryEventQuestionsAnswers>() |
| | | .eq(LotteryEventQuestionsAnswers::getLotteryEventId,lotteryEvent.getId())); |
| | | lotteryEventQuestionsAnswersService.remove(new LambdaQueryWrapper<TLotteryEventQuestionsAnswers>() |
| | | .eq(TLotteryEventQuestionsAnswers::getLotteryEventId, lotteryEvent.getId())); |
| | | //再删除题干数据 |
| | | lotteryEventQuestionsService.remove(new LambdaQueryWrapper<LotteryEventQuestions>() |
| | | .eq(LotteryEventQuestions::getLotteryEventId,lotteryEvent.getId())); |
| | | lotteryEventQuestionsService.remove(new LambdaQueryWrapper<TLotteryEventQuestions>() |
| | | .eq(TLotteryEventQuestions::getLotteryEventId, lotteryEvent.getId())); |
| | | } |
| | | //3.抽奖活动删除 |
| | | lotteryEvent.setDelFlag(1); |
| | |
| | | |
| | | @Override |
| | | public R endImmediatelyLotteryEvent(String id) { |
| | | LotteryEvent lotteryEvent = this.getById(id); |
| | | TLotteryEvent lotteryEvent = this.getById(id); |
| | | if (null == lotteryEvent || lotteryEvent.getDelFlag()!=0) { |
| | | return R.fail("该抽奖活动不存在"); |
| | | } |