|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package com.luoo.user.service;
|
|
|
|
|
|
|
|
|
|
import api.PageResult;
|
|
|
|
|
import com.luoo.user.Scheduler.DrawLotteryScheduler;
|
|
|
|
|
import com.luoo.user.dao.LotteryDao;
|
|
|
|
|
import com.luoo.user.dao.LotteryRegionDao;
|
|
|
|
|
import com.luoo.user.dao.LotteryUserDao;
|
|
|
|
@ -12,22 +13,16 @@ import com.luoo.user.pojo.LotteryUser;
|
|
|
|
|
import com.luoo.user.pojo.Region;
|
|
|
|
|
import com.luoo.user.pojo.UserInfo;
|
|
|
|
|
import com.luoo.user.pojo.UserPointLog;
|
|
|
|
|
import constants.Constants;
|
|
|
|
|
import constants.ErrorConstants;
|
|
|
|
|
import dto.UserLoginDto;
|
|
|
|
|
import dto.UserMessageDto;
|
|
|
|
|
import enums.MessageTypeEnum;
|
|
|
|
|
import enums.PointEnums;
|
|
|
|
|
import enums.UserTypeEnum;
|
|
|
|
|
import enums.UserVipStatusEnum;
|
|
|
|
|
import exception.BizException;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
@ -36,7 +31,6 @@ import javax.persistence.criteria.Root;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
|
@ -80,23 +74,22 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
private final LotteryUserDao lotteryUserDao;
|
|
|
|
|
|
|
|
|
|
private final UserMessageService userMessageService;
|
|
|
|
|
private final DrawLotteryScheduler drawLotteryScheduler;
|
|
|
|
|
|
|
|
|
|
public LotteryService(LotteryDao lotteryDao, IdWorker idWorker, JwtUtil jwtUtil,
|
|
|
|
|
RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao,
|
|
|
|
|
UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, UserMessageService userMessageService) {
|
|
|
|
|
public LotteryService(LotteryDao lotteryDao, JwtUtil jwtUtil, IdWorker idWorker, RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao, UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, DrawLotteryScheduler drawLotteryScheduler) {
|
|
|
|
|
this.lotteryDao = lotteryDao;
|
|
|
|
|
this.idWorker = idWorker;
|
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
|
this.idWorker = idWorker;
|
|
|
|
|
this.redisLockUtil = redisLockUtil;
|
|
|
|
|
this.regionService = regionService;
|
|
|
|
|
this.lotteryRegionDao = lotteryRegionDao;
|
|
|
|
|
this.userInfoDao = userInfoDao;
|
|
|
|
|
this.rabbitTemplate = rabbitTemplate;
|
|
|
|
|
this.lotteryUserDao = lotteryUserDao;
|
|
|
|
|
this.userMessageService = userMessageService;
|
|
|
|
|
this.drawLotteryScheduler = drawLotteryScheduler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 新增一条积分抽奖
|
|
|
|
|
*
|
|
|
|
@ -236,6 +229,10 @@ public class LotteryService {
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
|
|
|
|
|
Lottery lottery = lotteryDao.findById(id).orElse(null);
|
|
|
|
|
|
|
|
|
|
if (lottery.getDrawTime() == null) {
|
|
|
|
|
throw new BizException(ErrorConstants.DRAW_TIME_IS_REQUIRED);
|
|
|
|
|
}
|
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
|
|
|
|
|
@ -243,6 +240,8 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
|
|
|
|
|
|
drawLotteryScheduler.scheduleDraw(lottery.getDrawTime(), id);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -281,6 +280,9 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 取消抽奖
|
|
|
|
|
drawLotteryScheduler.cancelDraw(id);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -359,105 +361,6 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 自动抽奖结果
|
|
|
|
|
*
|
|
|
|
|
* @param id 抽奖id
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void auto(String id, String token) {
|
|
|
|
|
|
|
|
|
|
Lottery lottery = lotteryDao.findById(id).get();
|
|
|
|
|
|
|
|
|
|
LocalDateTime applyEndTime = lottery.getApplyEndTime();
|
|
|
|
|
// 比较当前时间与applyEndTime
|
|
|
|
|
if (LocalDateTime.now().isBefore(applyEndTime)) {
|
|
|
|
|
throw new BizException(ErrorConstants.NOT_END_OF_SIGN_UP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(lottery.getStatus(), PointEnums.LOTTERY_STATUS_SIGN.getCode())) {
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(lottery.getType(), PointEnums.LOTTERY_TICKET.getCode())) {
|
|
|
|
|
// 门票抽奖业务
|
|
|
|
|
List<LotteryRegion> lotteryRegionList = lotteryRegionDao.findByLotteryId(id);
|
|
|
|
|
|
|
|
|
|
lotteryRegionList.forEach(lotteryRegion -> {
|
|
|
|
|
Integer num = lotteryRegion.getNum();
|
|
|
|
|
if (num > 0) {
|
|
|
|
|
List<LotteryUser> lotteryUserList = lotteryUserDao.findByLotteryIdAndRegionId(id,
|
|
|
|
|
lotteryRegion.getRegionId());
|
|
|
|
|
|
|
|
|
|
lotteryFunc(token, lotteryUserList, num);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else if (Objects.equals(lottery.getType(), PointEnums.LOTTERY_REAL.getCode())) {
|
|
|
|
|
// 实物抽奖业务
|
|
|
|
|
List<LotteryUser> byLotteryId = lotteryUserDao.findByLotteryId(id);
|
|
|
|
|
|
|
|
|
|
lotteryFunc(token, byLotteryId, lottery.getNum());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// 抽奖状态为报名中,自动抽奖
|
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_LOTTERY.getCode());
|
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 封装抽奖方法
|
|
|
|
|
*
|
|
|
|
|
* @param token 当前操作人
|
|
|
|
|
* @param lotteryUserList 参与抽奖人员列表
|
|
|
|
|
* @param num 设置的中奖数量
|
|
|
|
|
*/
|
|
|
|
|
private void lotteryFunc(String token, List<LotteryUser> lotteryUserList, Integer num) {
|
|
|
|
|
List<LotteryUser> winnerList = new ArrayList<>();
|
|
|
|
|
if (lotteryUserList.size() > num) {
|
|
|
|
|
// 抽奖人数大于设定人数
|
|
|
|
|
// 随机设定人数的数量中奖
|
|
|
|
|
winnerList = drawWinners(lotteryUserList, num);
|
|
|
|
|
} else {
|
|
|
|
|
// 抽奖人数小于设定人数
|
|
|
|
|
// 全部中奖
|
|
|
|
|
winnerList = lotteryUserList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
winnerList.forEach(lotteryUser -> {
|
|
|
|
|
lotteryUser.setResult(PointEnums.LOTTERY_RESULT_WIN.getCode());
|
|
|
|
|
if (StringUtils.isNotBlank(token)) {
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
lotteryUser.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
}
|
|
|
|
|
lotteryUserDao.save(lotteryUser);
|
|
|
|
|
|
|
|
|
|
// 发送恭喜中奖站内信
|
|
|
|
|
UserMessageDto userMessageDto = new UserMessageDto();
|
|
|
|
|
userMessageDto.setType(MessageTypeEnum.PRIVATE_MESSAGE.getType());
|
|
|
|
|
userMessageDto.setSendUserId(StringUtils.isNotBlank(token) ? jwtUtil.getUserLoginDto(token).getUserId() : null);
|
|
|
|
|
userMessageDto.setTitle("恭喜中奖!");
|
|
|
|
|
userMessageDto.setContent("恭喜中奖!请加微信XXXXXX");
|
|
|
|
|
userMessageDto.setUserId(lotteryUser.getUserId());
|
|
|
|
|
userMessageDto.setSendUserAvatar(Constants.RESOURCE_PREFIX + (StringUtils.isNotBlank(token) ? jwtUtil.getUserLoginDto(token).getAvatar() : null));
|
|
|
|
|
userMessageDto.setSendUserNickName(StringUtils.isNotBlank(token) ? jwtUtil.getUserLoginDto(token).getNickName() : null);
|
|
|
|
|
userMessageService.sendUserMessage(userMessageDto);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从参与者列表中随机抽取指定数量的中奖者。
|
|
|
|
|
*
|
|
|
|
|
* @param list 参与者列表
|
|
|
|
|
* @param winnersCount 中奖者数量
|
|
|
|
|
* @return 中奖者列表
|
|
|
|
|
*/
|
|
|
|
|
private List<LotteryUser> drawWinners(List<LotteryUser> list, int winnersCount) {
|
|
|
|
|
List<LotteryUser> winners = new ArrayList<>();
|
|
|
|
|
Collections.shuffle(winners, new Random());
|
|
|
|
|
return winners.subList(0, Math.min(winnersCount, winners.size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列表页查询
|
|
|
|
|
*
|
|
|
|
|