huliguo
16 小时以前 60e726c81966b042db4f7b108d06bd36109794de
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/lottery/LotteryEventServiceImpl.java
@@ -1,25 +1,19 @@
package com.ruoyi.goods.service.impl.lottery;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.goods.api.domain.LotteryEvent;
import com.ruoyi.goods.api.domain.LotteryEventPrize;
import com.ruoyi.goods.api.domain.UserLotteryEvent;
import com.ruoyi.goods.api.domain.UserLotteryEventQuestions;
import com.ruoyi.goods.domain.vo.AppLotteryEventPageVo;
import com.ruoyi.goods.domain.vo.LotteryEventVo;
import com.ruoyi.goods.domain.vo.ShopLotteryDrawListVo;
import com.ruoyi.goods.domain.vo.UserLotteryEventVo;
import com.ruoyi.goods.api.domain.*;
import com.ruoyi.goods.domain.dto.*;
import com.ruoyi.goods.domain.vo.*;
import com.ruoyi.goods.mapper.lottery.LotteryEventMapper;
import com.ruoyi.goods.service.lottery.ILotteryEventPrizeService;
import com.ruoyi.goods.service.lottery.ILotteryEventService;
import com.ruoyi.goods.service.lottery.IUserLotteryEventQuestionsService;
import com.ruoyi.goods.service.lottery.IUserLotteryEventService;
import com.ruoyi.goods.service.lottery.*;
import com.ruoyi.system.api.domain.poji.member.Member;
import com.ruoyi.system.api.domain.poji.member.MemberGiftRecord;
import com.ruoyi.system.api.domain.poji.shop.Shop;
@@ -29,8 +23,10 @@
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.annotation.Resources;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
@@ -64,7 +60,12 @@
   
   @Resource
   private RemoteShopService remoteShopService;
   @Resource
   private ILotteryEventQuestionsAnswersService lotteryEventQuestionsAnswersService;
   @Resource
   private ILotteryEventQuestionsService lotteryEventQuestionsService;
   
   /**
    * 根据id查询抽奖活动信息
@@ -251,4 +252,231 @@
   public List<ShopLotteryDrawListVo> getShopLotteryDrawList(Page<ShopLotteryDrawListVo> page, Long shopId) {
      return this.baseMapper.getShopLotteryDrawList(page, shopId);
   }
   @Override
   @Transactional
   public R<ShopLotteryDrawVo> 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())//开始时间在这个活动的(开始-结束)范围内的
            .or()
            .between(LotteryEvent::getEndTime, dto.getStartTime(), dto.getEndTime())//结束时间在这个活动的(开始-结束)范围内的
      );
      if (null != lotteryEvents && !lotteryEvents.isEmpty()) {
         // 新增记录时直接返回错误
         if (dto.getId() == null) {
            return R.fail("新增抽奖活动与现有抽奖活动时间范围冲突");
         }
         // 更新记录时,检查是否与其他记录(非自身)冲突
         boolean hasConflict = lotteryEvents.stream()
               .anyMatch(event -> !event.getId().equals(dto.getId()));
         if (hasConflict) {
            return R.fail("编辑抽奖活动与现有其他活动时间范围冲突");
         }
      }
      LotteryEvent lotteryEvent = new LotteryEvent();
      if (null != dto.getId()) {
         //编辑
         lotteryEvent=this.baseMapper.selectById(dto.getId());
         if (null == lotteryEvent||lotteryEvent.getDelFlag() != 0) {
            return R.fail("该抽奖活动已被删除");
         }
         if (!lotteryEvent.getActivityType().equals(dto.getActivityType())) {
            return R.fail("编辑抽奖活动不能修改开启方式");
         }
         if(!lotteryEvent.getName().equals(dto.getName())) {
            return R.fail("编辑抽奖活动不能修改抽奖名称");
         }
         //答题类型
         if (lotteryEvent.getActivityType().equals(5)){
             //将之前的题干数据、答案选项数据删除
            //先删除答案选项数据
            lotteryEventQuestionsAnswersService.remove(new LambdaQueryWrapper<LotteryEventQuestionsAnswers>()
                  .eq(LotteryEventQuestionsAnswers::getLotteryEventId,lotteryEvent.getId()));
            //再删除题干数据
            lotteryEventQuestionsService.remove(new LambdaQueryWrapper<LotteryEventQuestions>()
                  .eq(LotteryEventQuestions::getLotteryEventId,lotteryEvent.getId()));
         }
         //奖品数据删除
         lotteryEventPrizeService.remove(new LambdaQueryWrapper<LotteryEventPrize>()
               .eq(LotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));
      }
      //新增
      BeanUtils.copyProperties(dto, lotteryEvent);
      if (dto.getId() == null) {
         lotteryEvent.setCreateTime(LocalDateTime.now());
         lotteryEvent.setCreateUserId(dto.getUserId());
      }else {
         lotteryEvent.setUpdateTime(LocalDateTime.now());
         lotteryEvent.setUpdateUserId(dto.getUserId());
      }
      //保存抽奖活动
      this.saveOrUpdate(lotteryEvent);
      // 保存奖品数据
      LotteryEvent finalLotteryEvent = lotteryEvent;//jdk8及之后 变量在初始化后确实没有被重新赋值,可直接引用,无需显式声明为 final
      List<LotteryEventPrize> prizeList = dto.getMgtLotteryEventPrizeDTOList().stream()
            .map(prizeDTO -> {
               LotteryEventPrize prize = new LotteryEventPrize();
               prize.setLotteryEventId(finalLotteryEvent.getId());
               prize.setPrizeType(prizeDTO.getPrizeType());
               if (null != prizeDTO.getObjectId()){
                  prize.setObjectId(prizeDTO.getObjectId());
               }
               prize.setObjectName(prizeDTO.getObjectName());
               prize.setNumber(prizeDTO.getNumber());
               prize.setWinRate(prizeDTO.getWinRate());
               return prize;
            })
            .collect(Collectors.toList());
      lotteryEventPrizeService.saveBatch(prizeList);
      if (dto.getActivityType().equals(5)) {
         //保存题干数据
         for (MgtLotteryEventQuestionDTO questionDTO : dto.getMgtLotteryQuestionDTOList()) {
            LotteryEventQuestions question = new LotteryEventQuestions();
            question.setLotteryEventId(finalLotteryEvent.getId());
            question.setName(questionDTO.getName());
            question.setSort(questionDTO.getSort());
            lotteryEventQuestionsService.save(question);
            //保存答案选项数据
            List<LotteryEventQuestionsAnswers> answersList = questionDTO.getAnswersDTOList().stream()
                  .map(answersDTO -> {
                     LotteryEventQuestionsAnswers answer = new LotteryEventQuestionsAnswers();
                     answer.setLotteryEventId(finalLotteryEvent.getId());
                     answer.setLotteryEventQuestionsId(question.getId());
                     answer.setAnswer(answersDTO.getAnswer());
                     answer.setIsRight(answersDTO.getIsRight());
                     return answer;
                  }).collect(Collectors.toList());
            lotteryEventQuestionsAnswersService.saveBatch(answersList);
         }
      }
      return R.ok();
   }
   @Override
   public List<MgtLotteryEventPageVo> pageMgtLotteryEvent(Page<MgtLotteryEventPageVo> page, MgtLotteryEventPageDto dto) {
      Map<Long,Shop> map;
      if (dto.getCreateObject()!=null){
         //根据 条件-创建对象 模糊查找门店集合
         List<Shop> shopList=remoteShopService.getShopListByShopName(dto.getCreateObject());
         if (null != shopList && !shopList.isEmpty()) {
            List<Long> shopIds = shopList.stream().map(Shop::getShopId).collect(Collectors.toList());
            dto.setShopIds(shopIds);
            //转为map 方便后续取商户名称
            map = shopList.stream().collect(Collectors.toMap(Shop::getShopId, shop -> shop));
         } else {
                map = new HashMap<>();
            }
            //判断“平台创建”是否包含了 条件-创建对象
         if (!"平台创建".contains(dto.getCreateObject())){
            dto.setFlag(0);//未包含,只查找门店ids
         }
      } else {
            map = new HashMap<>();
        }
        //获取分页信息
      List<MgtLotteryEventPageVo> voList = this.baseMapper.pageMgtLotteryEvent(page, dto);
      //填充创建对象
      voList.forEach(x->{
         if (x.getActivityType()!=6){
            x.setCreateObject("平台创建");
         }else {
            //线下抽奖,获取门店名称
            x.setCreateObject(map.get(Long.valueOf(x.getShopId())).getShopName());
         }
      });
      return voList;
   }
   @Override
   public R<MgtLotteryEventDetailVO> getLotteryEventDetailById(String id) {
      LotteryEvent lotteryEvent = this.getById(id);
      if (null  == lotteryEvent) {
         return R.fail("该抽奖活动不存在");
      }
      MgtLotteryEventDetailVO vo = new MgtLotteryEventDetailVO();
      BeanUtils.copyProperties(lotteryEvent, vo);
      if (lotteryEvent.getActivityType()==6){
         //线下抽奖 取商户名称
         Shop data = remoteShopService.getShop(Long.valueOf(lotteryEvent.getShopId())).getData();
         if (null != data){
            vo.setShopName(data.getShopName());
         }
         //题干数据
         List<LotteryEventQuestions> questionsList = lotteryEventQuestionsService.getBaseMapper().selectList(new LambdaQueryWrapper<LotteryEventQuestions>().eq(LotteryEventQuestions::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.getLotteryEventId()));
            questionVO.setAnswersVOList(answersList);
            return questionVO;
         }).collect(Collectors.toList());
         //题干数据排序
         // 自定义 Comparator 实现
         Comparator<LotteryEventQuestions> sortComparator = new Comparator<LotteryEventQuestions>() {
            @Override
            public int compare(LotteryEventQuestions q1, LotteryEventQuestions q2) {
               return Integer.compare(q1.getSort(), q2.getSort());
            }
         };
         // 应用排序
         Collections.sort(questionsList, sortComparator);
         vo.setMgtLotteryQuestionVOList(questionsVOList);
      }
      //奖项信息
      List<LotteryEventPrize> prizeList = lotteryEventPrizeService.getBaseMapper().selectList(new LambdaQueryWrapper<LotteryEventPrize>()
            .eq(LotteryEventPrize::getLotteryEventId, lotteryEvent.getId()));
      vo.setLotteryEventPrizeList(prizeList);
      return R.ok(vo);
   }
   @Override
   public R deleteMgtLotteryEvent(String id) {
      LotteryEvent lotteryEvent = this.getById(id);
      if (null == lotteryEvent || lotteryEvent.getDelFlag()!=0) {
         return R.ok();
      }
      //1.奖品数据删除
      lotteryEventPrizeService.remove(new LambdaQueryWrapper<LotteryEventPrize>()
            .eq(LotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));
      //2.答题类型
      if (lotteryEvent.getActivityType().equals(5)){
         //先删除答案选项数据
         lotteryEventQuestionsAnswersService.remove(new LambdaQueryWrapper<LotteryEventQuestionsAnswers>()
               .eq(LotteryEventQuestionsAnswers::getLotteryEventId,lotteryEvent.getId()));
         //再删除题干数据
         lotteryEventQuestionsService.remove(new LambdaQueryWrapper<LotteryEventQuestions>()
               .eq(LotteryEventQuestions::getLotteryEventId,lotteryEvent.getId()));
      }
      //3.抽奖活动删除
      lotteryEvent.setDelFlag(1);
      this.updateById(lotteryEvent);
      return R.ok();
   }
   @Override
   public R endImmediatelyLotteryEvent(String id) {
      LotteryEvent lotteryEvent = this.getById(id);
      if (null == lotteryEvent || lotteryEvent.getDelFlag()!=0) {
         return R.fail("该抽奖活动不存在");
      }
      LocalDateTime now = LocalDateTime.now();
      if (!now.isBefore(lotteryEvent.getStartTime()) && !now.isAfter(lotteryEvent.getEndTime())) {
         return R.fail("该抽奖活动不在活动时间范围内");
      }
      lotteryEvent.setEndTime(now);//将结束时间设置为当前时间
      lotteryEvent.setUpdateUserId(SecurityUtils.getUserId());
      lotteryEvent.setUpdateTime(now);
      this.updateById(lotteryEvent);
      return R.ok();
   }
}