From 8059e9b991c15edbac508e6b658a0d9571d11b1c Mon Sep 17 00:00:00 2001
From: huliguo <2023611923@qq.com>
Date: 星期五, 01 八月 2025 12:46:00 +0800
Subject: [PATCH] bug修改

---
 ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/lottery/LotteryEventServiceImpl.java |  422 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 374 insertions(+), 48 deletions(-)

diff --git a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/lottery/LotteryEventServiceImpl.java b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/lottery/LotteryEventServiceImpl.java
index b83e9cb..c85351c 100644
--- a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/lottery/LotteryEventServiceImpl.java
+++ b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/lottery/LotteryEventServiceImpl.java
@@ -1,33 +1,32 @@
 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.UserLotteryEventVo;
+import com.ruoyi.goods.api.domain.*;
+import com.ruoyi.goods.domain.dto.MgtLotteryEventEditDTO;
+import com.ruoyi.goods.domain.dto.MgtLotteryEventPageDto;
+import com.ruoyi.goods.domain.dto.MgtLotteryEventQuestionDTO;
+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.dto.IntegralChangeDto;
 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;
-import com.ruoyi.system.api.service.RemoteCouponService;
-import com.ruoyi.system.api.service.RemoteMemberService;
-import com.ruoyi.system.api.service.RemoteShopService;
+import com.ruoyi.system.api.model.AddIntegralRecordVo;
+import com.ruoyi.system.api.model.AddOrderVo;
+import com.ruoyi.system.api.service.*;
 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 java.text.SimpleDateFormat;
@@ -41,7 +40,7 @@
  * @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;
@@ -63,7 +62,15 @@
 	
 	@Resource
 	private RemoteShopService remoteShopService;
-	
+
+	@Resource
+	private ILotteryEventQuestionsAnswersService lotteryEventQuestionsAnswersService;
+
+	@Resource
+	private ILotteryEventQuestionsService lotteryEventQuestionsService;
+
+	@Resource
+	private RemoteOrderService remoteOrderService;
 	
 	/**
 	 * 根据id查询抽奖活动信息
@@ -72,26 +79,22 @@
 	 * @return
 	 */
 	@Override
-	public LotteryEventVo getLotteryEvent(String id) {
+	public LotteryEventVo getLotteryEvent(String id, String orderId) {
 		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));
-		vo.setLaveTimes(lotteryEvent.getTimes() - userLotteryEvents.size());
-		//查询抽检活动奖品
-		List<LotteryEventPrize> lotteryEventPrizeList = lotteryEventPrizeService.list(new QueryWrapper<LotteryEventPrize>().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));
-			if (null == questionsServiceOne) {
-				vo.setCorrectAnswerRate(questionsServiceOne.getCorrectAnswerRate());
-			}
+		QueryWrapper<TUserLotteryEvent> wrapper = new QueryWrapper<TUserLotteryEvent>().eq("lottery_event_id", id).eq("user_id", userId);
+		//下单抽奖
+		if(2 == lotteryEvent.getActivityType()){
+			wrapper.eq("order_id", orderId);
 		}
+		List<TUserLotteryEvent> userLotteryEvents = userLotteryEventService.list(wrapper);
+		vo.setLaveTimes(lotteryEvent.getTimes() - userLotteryEvents.size());
+
 		//构建我的奖品明细
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
 		List<UserLotteryEventVo> collect = userLotteryEvents.stream().map(s -> {
@@ -99,10 +102,12 @@
 			userLotteryEventVo.setName(s.getObjectName());
 			userLotteryEventVo.setPrizeType(s.getPrizeType());
 			userLotteryEventVo.setNumber(s.getNumber());
-			MemberGiftRecord memberGiftRecord = remoteMemberService.getVerifyPrizeByGiftId(s.getId()).getData();
-			userLotteryEventVo.setVerifyCode("3-" + memberGiftRecord.getPrizeId());
-			userLotteryEventVo.setIsVerify(1 == memberGiftRecord.getVerifyStatus() ? 0 : 1);
-			userLotteryEventVo.setVerifyTime(null == memberGiftRecord.getVerifyTime() ? "" : sdf.format(memberGiftRecord.getVerifyTime()));
+			if (Arrays.asList(2, 3).contains(s.getPrizeType())) {
+				MemberGiftRecord memberGiftRecord = remoteMemberService.getVerifyPrizeByGiftId(s.getId()).getData();
+				userLotteryEventVo.setVerifyCode("3-" + memberGiftRecord.getPrizeId());
+				userLotteryEventVo.setIsVerify(1 == memberGiftRecord.getVerifyStatus() ? 0 : 1);
+				userLotteryEventVo.setVerifyTime(null == memberGiftRecord.getVerifyTime() ? "" : sdf.format(memberGiftRecord.getVerifyTime()));
+			}
 			Member member = remoteMemberService.getMember(userId).getData();
 			userLotteryEventVo.setVerifyShop("全部门店");
 			if (null != member && null != member.getRelationShopId()) {
@@ -112,6 +117,17 @@
 			return userLotteryEventVo;
 		}).collect(Collectors.toList());
 		vo.setYourPrizeList(collect);
+
+		//查询抽奖活动奖品
+		List<TLotteryEventPrize> lotteryEventPrizeList = lotteryEventPrizeService.list(new QueryWrapper<TLotteryEventPrize>().eq("lottery_event_id", id));
+		vo.setPrizeList(lotteryEventPrizeList);
+		//答题正确率
+		if (5 == lotteryEvent.getActivityType()) {
+			TUserLotteryEventQuestions questionsServiceOne = userLotteryEventQuestionsAnswersService.getOne(new QueryWrapper<TUserLotteryEventQuestions>().eq("lottery_event_id", id).eq("user_id", userId));
+			if (null != questionsServiceOne) {
+				vo.setCorrectAnswerRate(questionsServiceOne.getCorrectAnswerRate());
+			}
+		}
 		return vo;
 	}
 	
@@ -123,13 +139,13 @@
 	 * @return
 	 */
 	@Override
-	public R lotteryDraw(String id) {
+	public R<TLotteryEventPrize> lotteryDraw(String id, String orderId) {
 		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("答题抽奖失败,答题正确率低于活动要求");
 			}
@@ -141,17 +157,17 @@
 			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).eq("order_id", orderId));
 				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);
 					}
@@ -160,9 +176,9 @@
 				Collections.shuffle(list);
 				//开始获取随机数
 				int random = new Random().nextInt(list.size());
-				LotteryEventPrize lotteryEventPrize = lotteryEventPrizeList.get(random);
+				TLotteryEventPrize lotteryEventPrize = list.get(random);
 				//添加中奖商品
-				UserLotteryEvent userLotteryEvent = new UserLotteryEvent();
+				TUserLotteryEvent userLotteryEvent = new TUserLotteryEvent();
 				userLotteryEvent.setId(IdUtils.simpleUUID());
 				userLotteryEvent.setUserId(userId);
 				userLotteryEvent.setLotteryEventId(id);
@@ -171,13 +187,22 @@
 				userLotteryEvent.setObjectId(lotteryEventPrize.getObjectId());
 				userLotteryEvent.setObjectName(lotteryEventPrize.getObjectName());
 				userLotteryEvent.setNumber(lotteryEventPrize.getNumber());
+				userLotteryEvent.setOrderId(orderId);
 				//中奖优惠券
 				if (1 == lotteryEventPrize.getPrizeType()) {
-					remoteCouponService.addMemberCoupon(lotteryEventPrize.getObjectId(), lotteryEventPrize.getNumber(), userId);
+					AddMemberCoupon addMemberCoupon = new AddMemberCoupon();
+					addMemberCoupon.setCouponId(lotteryEventPrize.getObjectId());
+					addMemberCoupon.setNumber(lotteryEventPrize.getNumber());
+					addMemberCoupon.setUserId(userId);
+					remoteCouponService.addMemberCoupon(addMemberCoupon);
 				}
 				//中奖积分
 				if (4 == lotteryEventPrize.getPrizeType()) {
-					remoteMemberService.addIntegralRecord(lotteryEventPrize.getNumber(), userId, id);
+					AddIntegralRecordVo addIntegralRecordVo = new AddIntegralRecordVo();
+					addIntegralRecordVo.setIntegral(lotteryEventPrize.getNumber());
+					addIntegralRecordVo.setUserId(userId);
+					addIntegralRecordVo.setOrderId(id);
+					remoteMemberService.addIntegralRecord(addIntegralRecordVo);
 				}
 				userLotteryEvent.setCreateTime(LocalDateTime.now());
 				userLotteryEventService.save(userLotteryEvent);
@@ -189,32 +214,56 @@
 				memberGiftRecord.setGiftId(userLotteryEvent.getId());
 				memberGiftRecord.setGiftFrom(1);
 				memberGiftRecord.setShopId(member.getRelationShopId());
+				memberGiftRecord.setVerifyStatus(1);
 				switch (lotteryEventPrize.getPrizeType()) {
 					case 1:
 						memberGiftRecord.setGiftType(1);
 						memberGiftRecord.setCouponId(userLotteryEvent.getObjectId());
 						memberGiftRecord.setCouponNumber(userLotteryEvent.getNumber());
+						memberGiftRecord.setCouponName(userLotteryEvent.getObjectName());
+						memberGiftRecord.setVerifyStatus(2);
+						memberGiftRecord.setVerifyTime(new Date());
 						break;
 					case 2:
 						memberGiftRecord.setGiftType(2);
 						memberGiftRecord.setGoodsId(userLotteryEvent.getObjectId());
 						memberGiftRecord.setGoodsNumber(userLotteryEvent.getNumber());
+						memberGiftRecord.setGoodsName(userLotteryEvent.getObjectName());
+						//添加待核销订单
+						AddOrderVo orderVo = new AddOrderVo();
+						orderVo.setShopId(member.getRelationShopId());
+						orderVo.setUserId(member.getUserId());
+						orderVo.setId(userLotteryEvent.getId());
+						orderVo.setGoodsId(userLotteryEvent.getObjectId());
+						orderVo.setGoodsNum(userLotteryEvent.getNumber());
+						remoteOrderService.addOrder(orderVo);
 						break;
 					case 3:
 						memberGiftRecord.setGiftType(4);
 						memberGiftRecord.setGiftName(userLotteryEvent.getObjectName());
 						memberGiftRecord.setGiftNumber(userLotteryEvent.getNumber());
+						//添加待核销订单
+						AddOrderVo orderVo1 = new AddOrderVo();
+						orderVo1.setShopId(member.getRelationShopId());
+						orderVo1.setUserId(member.getUserId());
+						orderVo1.setId(userLotteryEvent.getId());
+						orderVo1.setGoodsName(userLotteryEvent.getObjectName());
+						orderVo1.setGoodsNum(userLotteryEvent.getNumber());
+						remoteOrderService.addOrder(orderVo1);
 						break;
 					case 4:
 						memberGiftRecord.setGiftType(5);
 						memberGiftRecord.setIntegralNumber(userLotteryEvent.getNumber());
+						memberGiftRecord.setVerifyStatus(2);
+						memberGiftRecord.setVerifyTime(new Date());
 						break;
 				}
 				memberGiftRecord.setPrizeFrom(2);
 				memberGiftRecord.setCreateTime(new Date());
-				memberGiftRecord.setVerifyStatus(1);
-				remoteMemberService.saveMemberGiftRecord(memberGiftRecord);
-				return R.ok();
+				if(5 != lotteryEventPrize.getPrizeType()){
+					remoteMemberService.saveMemberGiftRecord(memberGiftRecord);
+				}
+				return R.ok(lotteryEventPrize);
 			}
 		} catch (Exception e) {
 			e.printStackTrace();
@@ -236,4 +285,281 @@
 	public List<AppLotteryEventPageVo> pageAppLotteryEvent(Page<AppLotteryEventPageVo> page, Long userId) {
 		return this.baseMapper.pageAppLotteryEvent(page, userId);
 	}
+	
+	
+	/**
+	 * 获取门店抽奖列表
+	 *
+	 * @param page
+	 * @param shopId
+	 * @return
+	 */
+	@Override
+	public List<ShopLotteryDrawListVo> getShopLotteryDrawList(Page<ShopLotteryDrawListVo> page, Long shopId) {
+		return this.baseMapper.getShopLotteryDrawList(page, shopId);
+	}
+
+	@Override
+	@Transactional
+	public R editLotteryEvent(MgtLotteryEventEditDTO dto) {
+		//先检查开始-结束时间范围内,系统中是否有该活动
+		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()
+				.eq(TLotteryEvent::getDelFlag, 0)//未删除的
+				.eq(TLotteryEvent::getActivityType, dto.getActivityType())//类型相同的
+				.between(TLotteryEvent::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("编辑抽奖活动与现有其他活动时间范围冲突");
+			}
+		}
+		TLotteryEvent lotteryEvent = new TLotteryEvent();
+		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<TLotteryEventQuestionsAnswers>()
+						.eq(TLotteryEventQuestionsAnswers::getLotteryEventId, lotteryEvent.getId()));
+				//再删除题干数据
+				lotteryEventQuestionsService.remove(new LambdaQueryWrapper<TLotteryEventQuestions>()
+						.eq(TLotteryEventQuestions::getLotteryEventId, lotteryEvent.getId()));
+			}
+			//奖品数据删除
+			/*lotteryEventPrizeService.remove(new LambdaQueryWrapper<TLotteryEventPrize>()
+					.eq(TLotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));*/
+			lotteryEventPrizeService.deleteByLotteryEventId(lotteryEvent.getId());
+		}
+		//新增
+		BeanUtils.copyProperties(dto, lotteryEvent);
+		lotteryEvent.setDelFlag(0);
+		if (dto.getId() == null) {
+			lotteryEvent.setId(IdUtils.simpleUUID());
+			lotteryEvent.setCreateTime(LocalDateTime.now());
+			lotteryEvent.setCreateUserId(dto.getUserId());
+		} else {
+			lotteryEvent.setUpdateTime(LocalDateTime.now());
+			lotteryEvent.setUpdateUserId(dto.getUserId());
+		}
+		//保存抽奖活动
+		this.saveOrUpdate(lotteryEvent);
+		// 保存奖品数据
+		TLotteryEvent finalLotteryEvent = lotteryEvent;//jdk8及之后 变量在初始化后确实没有被重新赋值,可直接引用,无需显式声明为 final
+		List<TLotteryEventPrize> prizeList = dto.getMgtLotteryEventPrizeDTOList().stream()
+				.map(prizeDTO -> {
+					TLotteryEventPrize prize = new TLotteryEventPrize();
+					prize.setId(IdUtils.simpleUUID());
+					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);
+		lotteryEventPrizeService.savePrizeList(prizeList);
+		if (dto.getActivityType().equals(5)) {
+			//保存题干数据
+			for (MgtLotteryEventQuestionDTO questionDTO : dto.getMgtLotteryQuestionDTOList()) {
+				TLotteryEventQuestions question = new TLotteryEventQuestions();
+				question.setId(IdUtils.simpleUUID());
+				question.setLotteryEventId(finalLotteryEvent.getId());
+				question.setName(questionDTO.getName());
+				question.setSort(questionDTO.getSort());
+				lotteryEventQuestionsService.save(question);
+				//保存答案选项数据
+				List<TLotteryEventQuestionsAnswers> answersList = questionDTO.getAnswersDTOList().stream()
+						.map(answersDTO -> {
+							TLotteryEventQuestionsAnswers answer = new TLotteryEventQuestionsAnswers();
+							answer.setId(IdUtils.simpleUUID());
+							answer.setLotteryEventId(finalLotteryEvent.getId());
+							answer.setLotteryEventQuestionsId(question.getId());
+							answer.setAnswers(answersDTO.getAnswers());
+							answer.setIsRight(answersDTO.getIsRight());
+							return answer;
+						}).collect(Collectors.toList());
+				lotteryEventQuestionsAnswersService.saveBatch(answersList);
+			}
+		}
+		return R.ok();
+	}
+
+	@Override
+	public Page<MgtLotteryEventPageVo> pageMgtLotteryEvent( MgtLotteryEventPageDto dto) {
+		Map<Long,Shop> map;
+		List<Shop> shopList;
+		Page<MgtLotteryEventPageVo> page = new Page<>();
+		page.setSize(dto.getPageSize());
+		page.setCurrent(dto.getPageNum());
+		page.setOptimizeCountSql(false);
+
+		if (dto.getCreateObject()!=null){
+			//根据 条件-创建对象 模糊查找门店集合
+			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<>();
+        }
+		//获取分页数据
+		Long total=this.baseMapper.pageMgtLotteryEventCount( dto);
+		page.setTotal(total);
+		page.setCurrent(dto.getPageNum());
+		page.setPages(total/dto.getPageSize());
+        //获取分页信息
+		dto.setOffset((dto.getPageNum() - 1) * dto.getPageSize());
+		List<MgtLotteryEventPageVo> voList = this.baseMapper.pageMgtLotteryEvent(dto);
+		//填充创建对象
+		voList.forEach(x->{
+			if (null == x.getShopId()){
+				x.setCreateObject("平台创建");
+
+			}else {
+				//线下抽奖,获取门店名称
+				x.setActivityType(6);
+				if(map.containsKey(Long.valueOf(x.getShopId()))){
+					x.setCreateObject(map.get(Long.valueOf(x.getShopId())).getShopName());
+				}else {
+					Shop shop = remoteShopService.getShop(Long.valueOf(x.getShopId())).getData();
+					if (null != shop){
+						x.setCreateObject(shop.getShopName());
+					}
+				}
+
+			}
+
+		});
+		page.setRecords(voList);
+		return page;
+	}
+
+	@Override
+	public R<MgtLotteryEventDetailVO> getLotteryEventDetailById(String id) {
+		TLotteryEvent lotteryEvent = this.getById(id);
+		if (null  == lotteryEvent) {
+			return R.fail("该抽奖活动不存在");
+		}
+		MgtLotteryEventDetailVO vo = new MgtLotteryEventDetailVO();
+		BeanUtils.copyProperties(lotteryEvent, vo);
+
+		if(lotteryEvent.getActivityType()==5){
+			//题干数据
+			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<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<TLotteryEventQuestions> sortComparator = new Comparator<TLotteryEventQuestions>() {
+				@Override
+				public int compare(TLotteryEventQuestions q1, TLotteryEventQuestions q2) {
+					return Integer.compare(q1.getSort(), q2.getSort());
+				}
+			};
+			// 应用排序
+			Collections.sort(questionsList, sortComparator);
+			vo.setMgtLotteryQuestionVOList(questionsVOList);
+		}
+		//奖项信息
+		/*List<TLotteryEventPrize> prizeList = lotteryEventPrizeService.getBaseMapper().selectList(new LambdaQueryWrapper<TLotteryEventPrize>()
+				.eq(TLotteryEventPrize::getLotteryEventId, lotteryEvent.getId()));*/
+		List<TLotteryEventPrize> prizeList = lotteryEventPrizeService.getPrizeListByLotteryEventId(lotteryEvent.getId());
+		vo.setLotteryEventPrizeList(prizeList);
+		if(null!=lotteryEvent.getShopId()){
+			//线下抽奖 取商户名称
+			Shop data = remoteShopService.getShop(Long.valueOf(lotteryEvent.getShopId())).getData();
+			if (null != data) {
+				vo.setShopName(data.getShopName());
+			}
+		}
+		return R.ok(vo);
+	}
+
+	@Override
+	public R deleteMgtLotteryEvent(String id) {
+		TLotteryEvent lotteryEvent = this.getById(id);
+		if (null == lotteryEvent || lotteryEvent.getDelFlag()!=0) {
+			return R.ok();
+		}
+
+		//1.奖品数据删除
+/*		lotteryEventPrizeService.remove(new LambdaQueryWrapper<TLotteryEventPrize>()
+				.eq(TLotteryEventPrize::getLotteryEventId,lotteryEvent.getId()));*/
+		
+		lotteryEventPrizeService.deleteByLotteryEventId(lotteryEvent.getId());
+		//2.答题类型
+		if (lotteryEvent.getActivityType().equals(5)) {
+			//先删除答案选项数据
+			lotteryEventQuestionsAnswersService.remove(new LambdaQueryWrapper<TLotteryEventQuestionsAnswers>()
+					.eq(TLotteryEventQuestionsAnswers::getLotteryEventId, lotteryEvent.getId()));
+			//再删除题干数据
+			lotteryEventQuestionsService.remove(new LambdaQueryWrapper<TLotteryEventQuestions>()
+					.eq(TLotteryEventQuestions::getLotteryEventId, lotteryEvent.getId()));
+		}
+		//3.抽奖活动删除
+		lotteryEvent.setDelFlag(1);
+		this.updateById(lotteryEvent);
+		return R.ok();
+	}
+
+	@Override
+	public R endImmediatelyLotteryEvent(String id) {
+		TLotteryEvent 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();
+	}
 }

--
Gitblit v1.7.1