|
|
@ -12,12 +12,16 @@ import com.luoo.user.pojo.LotteryUser;
|
|
|
|
import com.luoo.user.pojo.Region;
|
|
|
|
import com.luoo.user.pojo.Region;
|
|
|
|
import com.luoo.user.pojo.UserInfo;
|
|
|
|
import com.luoo.user.pojo.UserInfo;
|
|
|
|
import com.luoo.user.pojo.UserPointLog;
|
|
|
|
import com.luoo.user.pojo.UserPointLog;
|
|
|
|
|
|
|
|
import constants.Constants;
|
|
|
|
import constants.ErrorConstants;
|
|
|
|
import constants.ErrorConstants;
|
|
|
|
import dto.UserLoginDto;
|
|
|
|
import dto.UserLoginDto;
|
|
|
|
|
|
|
|
import dto.UserMessageDto;
|
|
|
|
|
|
|
|
import enums.MessageTypeEnum;
|
|
|
|
import enums.PointEnums;
|
|
|
|
import enums.PointEnums;
|
|
|
|
import enums.UserTypeEnum;
|
|
|
|
import enums.UserTypeEnum;
|
|
|
|
import enums.UserVipStatusEnum;
|
|
|
|
import enums.UserVipStatusEnum;
|
|
|
|
import exception.BizException;
|
|
|
|
import exception.BizException;
|
|
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
@ -28,6 +32,7 @@ import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
import javax.persistence.criteria.Root;
|
|
|
|
import javax.persistence.criteria.Root;
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
@ -53,377 +58,449 @@ import util.RedisLockUtil;
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
public class LotteryService {
|
|
|
|
public class LotteryService {
|
|
|
|
|
|
|
|
|
|
|
|
public static final String POINTS_WILL_BE_RETURNED = "抽奖返积分";
|
|
|
|
public static final String POINTS_WILL_BE_RETURNED = "抽奖返积分";
|
|
|
|
|
|
|
|
|
|
|
|
public static final String LOTTERY_POINTS_TYPE = "积分抽奖";
|
|
|
|
public static final String LOTTERY_POINTS_TYPE = "积分抽奖";
|
|
|
|
|
|
|
|
|
|
|
|
private final LotteryDao lotteryDao;
|
|
|
|
private final LotteryDao lotteryDao;
|
|
|
|
|
|
|
|
|
|
|
|
private final JwtUtil jwtUtil;
|
|
|
|
private final JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
|
|
private final IdWorker idWorker;
|
|
|
|
private final IdWorker idWorker;
|
|
|
|
|
|
|
|
|
|
|
|
private final RedisLockUtil redisLockUtil;
|
|
|
|
private final RedisLockUtil redisLockUtil;
|
|
|
|
|
|
|
|
|
|
|
|
private final RegionService regionService;
|
|
|
|
private final RegionService regionService;
|
|
|
|
|
|
|
|
|
|
|
|
private final LotteryRegionDao lotteryRegionDao;
|
|
|
|
private final LotteryRegionDao lotteryRegionDao;
|
|
|
|
|
|
|
|
|
|
|
|
private final UserInfoDao userInfoDao;
|
|
|
|
private final UserInfoDao userInfoDao;
|
|
|
|
|
|
|
|
|
|
|
|
private final RabbitTemplate rabbitTemplate;
|
|
|
|
private final RabbitTemplate rabbitTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
private final LotteryUserDao lotteryUserDao;
|
|
|
|
private final LotteryUserDao lotteryUserDao;
|
|
|
|
|
|
|
|
|
|
|
|
public LotteryService(LotteryDao lotteryDao, IdWorker idWorker, JwtUtil jwtUtil,
|
|
|
|
private final UserMessageService userMessageService;
|
|
|
|
RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao,
|
|
|
|
|
|
|
|
UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao) {
|
|
|
|
|
|
|
|
this.lotteryDao = lotteryDao;
|
|
|
|
|
|
|
|
this.idWorker = idWorker;
|
|
|
|
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
|
|
|
|
this.redisLockUtil = redisLockUtil;
|
|
|
|
|
|
|
|
this.regionService = regionService;
|
|
|
|
|
|
|
|
this.lotteryRegionDao = lotteryRegionDao;
|
|
|
|
|
|
|
|
this.userInfoDao = userInfoDao;
|
|
|
|
|
|
|
|
this.rabbitTemplate = rabbitTemplate;
|
|
|
|
|
|
|
|
this.lotteryUserDao = lotteryUserDao;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
public LotteryService(LotteryDao lotteryDao, IdWorker idWorker, JwtUtil jwtUtil,
|
|
|
|
* 新增一条积分抽奖
|
|
|
|
RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao,
|
|
|
|
*
|
|
|
|
UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, UserMessageService userMessageService) {
|
|
|
|
* @param lottery 抽奖对象
|
|
|
|
this.lotteryDao = lotteryDao;
|
|
|
|
* @param token 登录token
|
|
|
|
this.idWorker = idWorker;
|
|
|
|
*/
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
this.redisLockUtil = redisLockUtil;
|
|
|
|
public void add(Lottery lottery, String token) {
|
|
|
|
this.regionService = regionService;
|
|
|
|
|
|
|
|
this.lotteryRegionDao = lotteryRegionDao;
|
|
|
|
|
|
|
|
this.userInfoDao = userInfoDao;
|
|
|
|
|
|
|
|
this.rabbitTemplate = rabbitTemplate;
|
|
|
|
|
|
|
|
this.lotteryUserDao = lotteryUserDao;
|
|
|
|
|
|
|
|
this.userMessageService = userMessageService;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 新增一条积分抽奖
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param lottery 抽奖对象
|
|
|
|
|
|
|
|
* @param token 登录token
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void add(Lottery lottery, String token) {
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder regionCode = new StringBuilder();
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
StringBuilder regionName = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<LotteryRegion> lotteryRegionList = lottery.getLotteryRegionList();
|
|
|
|
StringBuilder regionCode = new StringBuilder();
|
|
|
|
|
|
|
|
StringBuilder regionName = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
for (LotteryRegion lotteryRegion : lotteryRegionList) {
|
|
|
|
String id = String.valueOf(idWorker.nextId());
|
|
|
|
Region regionById = regionService.getRegionById(lotteryRegion.getRegionId());
|
|
|
|
|
|
|
|
regionCode.append(regionById.getCode()).append(",");
|
|
|
|
|
|
|
|
regionName.append(regionById.getName()).append(",");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lottery.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
if (Objects.equals(lottery.getType(), PointEnums.LOTTERY_TICKET.getCode())) {
|
|
|
|
lottery.setCreateUser(userLoginDto.getUserId());
|
|
|
|
// 门票抽奖,城市必填
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
List<LotteryRegion> lotteryRegionList = lottery.getLotteryRegionList();
|
|
|
|
lottery.setCreateUserName(userLoginDto.getNickName());
|
|
|
|
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
|
|
|
|
lottery.setCode(redisLockUtil.lotteryGenerateSerialNumber());
|
|
|
|
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_EDITING.getCode());
|
|
|
|
|
|
|
|
lottery.setRegionCode(regionCode.toString());
|
|
|
|
|
|
|
|
lottery.setRegionName(regionName.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryRegionList.forEach(lotteryRegion -> {
|
|
|
|
|
|
|
|
lotteryRegion.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
|
|
|
|
lotteryRegion.setLotteryId(lottery.getId());
|
|
|
|
|
|
|
|
lotteryRegion.setCreateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
lotteryRegion.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryRegionDao.save(lotteryRegion);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 编辑抽奖
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param lottery 抽奖内容
|
|
|
|
|
|
|
|
* @param token 登录人
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void edit(Lottery lottery, String token) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder regionCode = new StringBuilder();
|
|
|
|
|
|
|
|
StringBuilder regionName = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<LotteryRegion> lotteryRegionList = lottery.getLotteryRegionList();
|
|
|
|
|
|
|
|
for (LotteryRegion lotteryRegion : lotteryRegionList) {
|
|
|
|
|
|
|
|
Region regionById = regionService.getRegionById(lotteryRegion.getRegionId());
|
|
|
|
|
|
|
|
regionCode.append(regionById.getCode()).append(",");
|
|
|
|
|
|
|
|
regionName.append(regionById.getName()).append(",");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String id = lottery.getId();
|
|
|
|
if (lotteryRegionList.isEmpty()) {
|
|
|
|
Lottery lottery1 = lotteryDao.findById(id).orElse(null);
|
|
|
|
throw new BizException(ErrorConstants.TICKET_CITY_IS_REQUIRED);
|
|
|
|
if (!Objects.equals(PointEnums.LOTTERY_STATUS_EDITING.getCode(), lottery1.getStatus())) {
|
|
|
|
}
|
|
|
|
// 不是编辑中,不允许修改
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.CAN_NOT_EDIT);
|
|
|
|
Integer num = 0;
|
|
|
|
|
|
|
|
for (LotteryRegion lotteryRegion : lotteryRegionList) {
|
|
|
|
|
|
|
|
Region regionById = regionService.getRegionById(lotteryRegion.getRegionId());
|
|
|
|
|
|
|
|
regionCode.append(regionById.getCode()).append(",");
|
|
|
|
|
|
|
|
regionName.append(regionById.getName()).append(",");
|
|
|
|
|
|
|
|
num += lotteryRegion.getNum();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryRegion.setId(id);
|
|
|
|
|
|
|
|
lotteryRegion.setLotteryId(lottery.getId());
|
|
|
|
|
|
|
|
lotteryRegion.setCreateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
lotteryRegion.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryRegionDao.save(lotteryRegion);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lottery.setRegionCode(regionCode.toString());
|
|
|
|
|
|
|
|
lottery.setRegionName(regionName.toString());
|
|
|
|
|
|
|
|
lottery.setNum(num);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 实物抽奖,数量必填
|
|
|
|
|
|
|
|
if (lottery.getNum() == null) {
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.REAL_NUMBER_IS_REQUIRED);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lottery.setId(id);
|
|
|
|
|
|
|
|
lottery.setCreateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
lottery.setCreateUserName(userLoginDto.getNickName());
|
|
|
|
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
|
|
|
|
lottery.setCode(redisLockUtil.lotteryGenerateSerialNumber());
|
|
|
|
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_EDITING.getCode());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lottery.setRegionCode(regionCode.toString());
|
|
|
|
/**
|
|
|
|
lottery.setRegionName(regionName.toString());
|
|
|
|
* 编辑抽奖
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
*
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
* @param lottery 抽奖内容
|
|
|
|
|
|
|
|
* @param token 登录人
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
lotteryRegionDao.deleteByLotteryId(id);
|
|
|
|
public void edit(Lottery lottery, String token) {
|
|
|
|
lotteryRegionList.forEach(lotteryRegion -> {
|
|
|
|
|
|
|
|
lotteryRegion.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
lotteryRegion.setLotteryId(id);
|
|
|
|
|
|
|
|
lotteryRegion.setCreateUser(userLoginDto.getUserId());
|
|
|
|
String id = lottery.getId();
|
|
|
|
lotteryRegion.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder regionCode = new StringBuilder();
|
|
|
|
lotteryRegionDao.save(lotteryRegion);
|
|
|
|
StringBuilder regionName = new StringBuilder();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(lottery.getType(), PointEnums.LOTTERY_TICKET.getCode())) {
|
|
|
|
}
|
|
|
|
// 门票抽奖,城市必填
|
|
|
|
|
|
|
|
List<LotteryRegion> lotteryRegionList = lottery.getLotteryRegionList();
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 发布抽奖
|
|
|
|
if (lotteryRegionList.isEmpty()) {
|
|
|
|
*
|
|
|
|
throw new BizException(ErrorConstants.TICKET_CITY_IS_REQUIRED);
|
|
|
|
* @param id 抽奖id
|
|
|
|
}
|
|
|
|
* @param token 登录人token
|
|
|
|
|
|
|
|
*/
|
|
|
|
Integer num = 0;
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void publish(String id, String token) {
|
|
|
|
lotteryRegionDao.deleteByLotteryId(id);
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
for (LotteryRegion lotteryRegion : lotteryRegionList) {
|
|
|
|
|
|
|
|
Region regionById = regionService.getRegionById(lotteryRegion.getRegionId());
|
|
|
|
Lottery lottery = lotteryDao.findById(id).orElse(null);
|
|
|
|
regionCode.append(regionById.getCode()).append(",");
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
regionName.append(regionById.getName()).append(",");
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
num += lotteryRegion.getNum();
|
|
|
|
|
|
|
|
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_SIGN.getCode());
|
|
|
|
|
|
|
|
|
|
|
|
lotteryRegion.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
lotteryRegion.setLotteryId(id);
|
|
|
|
|
|
|
|
lotteryRegion.setCreateUser(userLoginDto.getUserId());
|
|
|
|
}
|
|
|
|
lotteryRegion.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
lotteryRegionDao.save(lotteryRegion);
|
|
|
|
* 停止抽奖
|
|
|
|
}
|
|
|
|
*
|
|
|
|
lottery.setRegionCode(regionCode.toString());
|
|
|
|
* @param id 抽奖id
|
|
|
|
lottery.setRegionName(regionName.toString());
|
|
|
|
* @param token 登录人token
|
|
|
|
lottery.setNum(num);
|
|
|
|
* @param stopReason 停止原因
|
|
|
|
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
// 实物抽奖,数量必填
|
|
|
|
public void stop(String id, String token, String stopReason) {
|
|
|
|
if (lottery.getNum() == null) {
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
throw new BizException(ErrorConstants.REAL_NUMBER_IS_REQUIRED);
|
|
|
|
|
|
|
|
}
|
|
|
|
Lottery lottery = lotteryDao.findById(id).orElse(null);
|
|
|
|
}
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
Lottery oldLottery = lotteryDao.getOne(id);
|
|
|
|
lottery.setStopReason(stopReason);
|
|
|
|
if (!Objects.equals(PointEnums.LOTTERY_STATUS_EDITING.getCode(), oldLottery.getStatus())) {
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_SIGN.getCode());
|
|
|
|
// 不是编辑中,不允许修改
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.CAN_NOT_EDIT);
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返还用户积分 放到MQ队列
|
|
|
|
lottery.setCreateTime(oldLottery.getCreateTime());
|
|
|
|
lotteryUserDao.findByLotteryId(id).forEach(lotteryUser -> {
|
|
|
|
lottery.setCreateUser(oldLottery.getCreateUser());
|
|
|
|
|
|
|
|
lottery.setCreateUserName(oldLottery.getCreateUserName());
|
|
|
|
UserPointLog userPointLog = UserPointLog.builder()
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
|
|
|
|
|
|
|
|
.createUser(lotteryUser.getUserId())
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
.taskPointId(null)
|
|
|
|
|
|
|
|
.description(POINTS_WILL_BE_RETURNED)
|
|
|
|
|
|
|
|
.userId(lotteryUser.getUserId())
|
|
|
|
|
|
|
|
.score(lottery.getPoint())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend("pointLog", userPointLog);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 参与抽奖
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param id 抽奖id
|
|
|
|
|
|
|
|
* @param token 登录人token
|
|
|
|
|
|
|
|
* @param regionId 地区id
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void participate(String id, String token, Integer regionId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
|
|
|
Lottery lottery = lotteryDao.findById(id).get();
|
|
|
|
|
|
|
|
if (!Objects.equals(PointEnums.LOTTERY_STATUS_SIGN.getCode(), lottery.getStatus())) {
|
|
|
|
|
|
|
|
// 不是报名中,不允许参与
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.NOT_IN_THE_SIGN_UP_PERIOD);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 校验用户是否可参与
|
|
|
|
/**
|
|
|
|
UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get();
|
|
|
|
* 发布抽奖
|
|
|
|
|
|
|
|
*
|
|
|
|
Integer participant = lottery.getParticipant();
|
|
|
|
* @param id 抽奖id
|
|
|
|
if (Objects.equals(participant, PointEnums.LOTTERY_ALL_MEMBER.getCode())) {
|
|
|
|
* @param token 登录人token
|
|
|
|
// 会员可参与
|
|
|
|
*/
|
|
|
|
if (!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.INITIAL.getCode()) &&
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) {
|
|
|
|
public void publish(String id, String token) {
|
|
|
|
throw new BizException(ErrorConstants.NOT_MEMBER);
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) {
|
|
|
|
Lottery lottery = lotteryDao.findById(id).orElse(null);
|
|
|
|
// 永久会员可参与
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
if (!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) {
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
throw new BizException(ErrorConstants.NOT_PERMANENT_MEMBER);
|
|
|
|
|
|
|
|
}
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_SIGN.getCode());
|
|
|
|
} else if (Objects.equals(participant, PointEnums.LOTTERY_CONTRIBUTORS.getCode())) {
|
|
|
|
|
|
|
|
// 贡献者可参与
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
if (StringUtils.isBlank(userInfo.getBadges()) || !userInfo.getBadges().startsWith("1")) {
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.NOT_CONTRIBUTOR);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (Objects.equals(participant, PointEnums.LOTTERY_ARTIST.getCode())) {
|
|
|
|
|
|
|
|
// 音乐人可参与
|
|
|
|
|
|
|
|
if (!Objects.equals(String.valueOf(userInfo.getType()), UserTypeEnum.ARTIST_PERSON.getCode())
|
|
|
|
|
|
|
|
&& !Objects.equals(String.valueOf(userInfo.getType()), UserTypeEnum.ARTIST_BAND.getCode())
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.NOT_ARTIST);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<LotteryUser> checkUserList = lotteryUserDao.findByLotteryIdAndUserIdAndRegionId(
|
|
|
|
/**
|
|
|
|
id, userLoginDto.getUserId(), regionId);
|
|
|
|
* 停止抽奖
|
|
|
|
if(!checkUserList.isEmpty()) {
|
|
|
|
*
|
|
|
|
throw new BizException(ErrorConstants.ALREADY_PARTICIPATED);
|
|
|
|
* @param id 抽奖id
|
|
|
|
|
|
|
|
* @param token 登录人token
|
|
|
|
|
|
|
|
* @param stopReason 停止原因
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void stop(String id, String token, String stopReason) {
|
|
|
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Lottery lottery = lotteryDao.findById(id).orElse(null);
|
|
|
|
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
|
|
|
|
lottery.setStopReason(stopReason);
|
|
|
|
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_SIGN.getCode());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 返还用户积分 放到MQ队列
|
|
|
|
|
|
|
|
lotteryUserDao.findByLotteryId(id).forEach(lotteryUser -> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserPointLog userPointLog = UserPointLog.builder()
|
|
|
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
|
|
|
|
|
|
|
|
.createUser(lotteryUser.getUserId())
|
|
|
|
|
|
|
|
.taskPointId(null)
|
|
|
|
|
|
|
|
.description(POINTS_WILL_BE_RETURNED)
|
|
|
|
|
|
|
|
.userId(lotteryUser.getUserId())
|
|
|
|
|
|
|
|
.score(lottery.getPoint())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend("pointLog", userPointLog);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LotteryUser lotteryUser = LotteryUser.builder()
|
|
|
|
/**
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
* 参与抽奖
|
|
|
|
.lotteryId(id)
|
|
|
|
*
|
|
|
|
.regionId(regionId)
|
|
|
|
* @param id 抽奖id
|
|
|
|
.userId(userLoginDto.getUserId())
|
|
|
|
* @param token 登录人token
|
|
|
|
.createUser(userLoginDto.getUserId())
|
|
|
|
* @param regionId 地区id
|
|
|
|
.updateUser(userLoginDto.getUserId())
|
|
|
|
*/
|
|
|
|
.build();
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void participate(String id, String token, Integer regionId) {
|
|
|
|
lotteryUserDao.save(lotteryUser);
|
|
|
|
|
|
|
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
UserPointLog userPointLog = UserPointLog.builder()
|
|
|
|
Lottery lottery = lotteryDao.findById(id).get();
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
if (!Objects.equals(PointEnums.LOTTERY_STATUS_SIGN.getCode(), lottery.getStatus())) {
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_REDUCE.getCode())
|
|
|
|
// 不是报名中,不允许参与
|
|
|
|
.createUser(lotteryUser.getUserId())
|
|
|
|
throw new BizException(ErrorConstants.NOT_IN_THE_SIGN_UP_PERIOD);
|
|
|
|
.taskPointId(null)
|
|
|
|
}
|
|
|
|
.description(LOTTERY_POINTS_TYPE)
|
|
|
|
|
|
|
|
.userId(lotteryUser.getUserId())
|
|
|
|
// 校验用户是否可参与
|
|
|
|
.score(lottery.getPoint())
|
|
|
|
UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get();
|
|
|
|
.build();
|
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend("pointLog", userPointLog);
|
|
|
|
Integer participant = lottery.getParticipant();
|
|
|
|
|
|
|
|
if (Objects.equals(participant, PointEnums.LOTTERY_ALL_MEMBER.getCode())) {
|
|
|
|
}
|
|
|
|
// 会员可参与
|
|
|
|
|
|
|
|
if (!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.INITIAL.getCode()) &&
|
|
|
|
/**
|
|
|
|
!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) {
|
|
|
|
* 自动抽奖结果
|
|
|
|
throw new BizException(ErrorConstants.NOT_MEMBER);
|
|
|
|
*
|
|
|
|
}
|
|
|
|
* @param id 抽奖id
|
|
|
|
} else if (Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) {
|
|
|
|
*/
|
|
|
|
// 永久会员可参与
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
if (!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) {
|
|
|
|
public void auto(String id, String token) {
|
|
|
|
throw new BizException(ErrorConstants.NOT_PERMANENT_MEMBER);
|
|
|
|
|
|
|
|
}
|
|
|
|
Lottery lottery = lotteryDao.findById(id).get();
|
|
|
|
} else if (Objects.equals(participant, PointEnums.LOTTERY_CONTRIBUTORS.getCode())) {
|
|
|
|
|
|
|
|
// 贡献者可参与
|
|
|
|
LocalDateTime applyEndTime = lottery.getApplyEndTime();
|
|
|
|
if (StringUtils.isBlank(userInfo.getBadges()) || !userInfo.getBadges().startsWith("1")) {
|
|
|
|
// 比较当前时间与applyEndTime
|
|
|
|
throw new BizException(ErrorConstants.NOT_CONTRIBUTOR);
|
|
|
|
if (LocalDateTime.now().isBefore(applyEndTime)) {
|
|
|
|
}
|
|
|
|
throw new BizException(ErrorConstants.NOT_END_OF_SIGN_UP);
|
|
|
|
} else if (Objects.equals(participant, PointEnums.LOTTERY_ARTIST.getCode())) {
|
|
|
|
|
|
|
|
// 音乐人可参与
|
|
|
|
|
|
|
|
if (!Objects.equals(String.valueOf(userInfo.getType()), UserTypeEnum.ARTIST_PERSON.getCode())
|
|
|
|
|
|
|
|
&& !Objects.equals(String.valueOf(userInfo.getType()), UserTypeEnum.ARTIST_BAND.getCode())
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.NOT_ARTIST);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<LotteryUser> checkUserList = lotteryUserDao.findByLotteryIdAndUserIdAndRegionId(
|
|
|
|
|
|
|
|
id, userLoginDto.getUserId(), regionId);
|
|
|
|
|
|
|
|
if (!checkUserList.isEmpty()) {
|
|
|
|
|
|
|
|
throw new BizException(ErrorConstants.ALREADY_PARTICIPATED);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LotteryUser lotteryUser = LotteryUser.builder()
|
|
|
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
|
|
|
.lotteryId(id)
|
|
|
|
|
|
|
|
.regionId(regionId)
|
|
|
|
|
|
|
|
.userId(userLoginDto.getUserId())
|
|
|
|
|
|
|
|
.createUser(userLoginDto.getUserId())
|
|
|
|
|
|
|
|
.updateUser(userLoginDto.getUserId())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lotteryUserDao.save(lotteryUser);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserPointLog userPointLog = UserPointLog.builder()
|
|
|
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_REDUCE.getCode())
|
|
|
|
|
|
|
|
.createUser(lotteryUser.getUserId())
|
|
|
|
|
|
|
|
.taskPointId(null)
|
|
|
|
|
|
|
|
.description(LOTTERY_POINTS_TYPE)
|
|
|
|
|
|
|
|
.userId(lotteryUser.getUserId())
|
|
|
|
|
|
|
|
.score(lottery.getPoint())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend("pointLog", userPointLog);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(lottery.getStatus(), PointEnums.LOTTERY_STATUS_SIGN.getCode())) {
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 自动抽奖结果
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @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());
|
|
|
|
|
|
|
|
|
|
|
|
List<LotteryRegion> lotteryRegionList = lotteryRegionDao.findByLotteryId(id);
|
|
|
|
lotteryFunc(token, lotteryUserList, num);
|
|
|
|
|
|
|
|
|
|
|
|
lotteryRegionList.forEach(lotteryRegion -> {
|
|
|
|
}
|
|
|
|
Integer num = lotteryRegion.getNum();
|
|
|
|
});
|
|
|
|
if (num > 0) {
|
|
|
|
|
|
|
|
List<LotteryUser> lotteryUserList = lotteryUserDao.findByLotteryIdAndRegionId(id,
|
|
|
|
|
|
|
|
lotteryRegion.getRegionId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<LotteryUser> winnerList = new ArrayList<>();
|
|
|
|
} else if (Objects.equals(lottery.getType(), PointEnums.LOTTERY_REAL.getCode())) {
|
|
|
|
if (lotteryUserList.size() > num) {
|
|
|
|
// 实物抽奖业务
|
|
|
|
|
|
|
|
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, lotteryRegion.getNum());
|
|
|
|
winnerList = drawWinners(lotteryUserList, num);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// 抽奖人数小于设定人数
|
|
|
|
// 抽奖人数小于设定人数
|
|
|
|
// 全部中奖
|
|
|
|
// 全部中奖
|
|
|
|
winnerList = lotteryUserList;
|
|
|
|
winnerList = lotteryUserList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
winnerList.forEach(lotteryUser -> {
|
|
|
|
winnerList.forEach(lotteryUser -> {
|
|
|
|
lotteryUser.setResult(PointEnums.LOTTERY_RESULT_WIN.getCode());
|
|
|
|
lotteryUser.setResult(PointEnums.LOTTERY_RESULT_WIN.getCode());
|
|
|
|
if(StringUtils.isNotBlank(token)) {
|
|
|
|
if (StringUtils.isNotBlank(token)) {
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
lotteryUser.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
lotteryUser.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lotteryUserDao.save(lotteryUser);
|
|
|
|
lotteryUserDao.save(lotteryUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
// 发送恭喜中奖站内信
|
|
|
|
});
|
|
|
|
UserMessageDto userMessageDto = new UserMessageDto();
|
|
|
|
// 抽奖状态为报名中,自动抽奖
|
|
|
|
userMessageDto.setType(MessageTypeEnum.PRIVATE_MESSAGE.getType());
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_LOTTERY.getCode());
|
|
|
|
userMessageDto.setSendUserId(StringUtils.isNotBlank(token) ? jwtUtil.getUserLoginDto(token).getUserId() : null);
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
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()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 列表页查询
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param page 页码
|
|
|
|
|
|
|
|
* @param size 每页数量
|
|
|
|
|
|
|
|
* @param lotterySearchDto 查询参数
|
|
|
|
|
|
|
|
* @return 列表页
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public PageResult<Lottery> lotteryPageResult(Integer page, Integer size,
|
|
|
|
|
|
|
|
LotterySearchDto lotterySearchDto) {
|
|
|
|
|
|
|
|
Sort sort = new Sort(Direction.DESC, "createTime");
|
|
|
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Specification<Lottery> specification = getSpecification(lotterySearchDto);
|
|
|
|
|
|
|
|
Page<Lottery> lotteryPage = lotteryDao.findAll(specification, pageRequest);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long totalElements = lotteryPage.getTotalElements();
|
|
|
|
|
|
|
|
return new PageResult<>(totalElements, lotteryPage.getContent());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Specification<Lottery> getSpecification(LotterySearchDto lotterySearchDto) {
|
|
|
|
|
|
|
|
return (Root<Lottery> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) -> {
|
|
|
|
|
|
|
|
List<Predicate> predicateList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(lotterySearchDto.getSearchStr())) {
|
|
|
|
|
|
|
|
predicateList.add(
|
|
|
|
|
|
|
|
criteriaBuilder.or(
|
|
|
|
|
|
|
|
criteriaBuilder.like(root.get("title"),
|
|
|
|
|
|
|
|
"%" + lotterySearchDto.getSearchStr() + "%"),
|
|
|
|
|
|
|
|
criteriaBuilder.like(root.get("code"), "%" + lotterySearchDto.getSearchStr() + "%")
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lotterySearchDto.getStatus() != null) {
|
|
|
|
|
|
|
|
predicateList.add(criteriaBuilder.equal(root.get("status"), lotterySearchDto.getStatus()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(lotterySearchDto.getCreateUser())) {
|
|
|
|
|
|
|
|
predicateList.add(
|
|
|
|
|
|
|
|
criteriaBuilder.equal(root.get("createUser"), lotterySearchDto.getCreateUser()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 从参与者列表中随机抽取指定数量的中奖者。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param participants 参与者列表
|
|
|
|
|
|
|
|
* @param winnersCount 中奖者数量
|
|
|
|
|
|
|
|
* @return 中奖者列表
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private List<LotteryUser> drawWinners(List<LotteryUser> participants, int winnersCount) {
|
|
|
|
|
|
|
|
List<LotteryUser> winners = new ArrayList<>(participants);
|
|
|
|
|
|
|
|
Collections.shuffle(winners, new Random());
|
|
|
|
|
|
|
|
return winners.subList(0, Math.min(winnersCount, winners.size()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 列表页查询
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param page 页码
|
|
|
|
|
|
|
|
* @param size 每页数量
|
|
|
|
|
|
|
|
* @param lotterySearchDto 查询参数
|
|
|
|
|
|
|
|
* @return 列表页
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public PageResult<Lottery> lotteryPageResult(Integer page, Integer size,
|
|
|
|
|
|
|
|
LotterySearchDto lotterySearchDto) {
|
|
|
|
|
|
|
|
Sort sort = new Sort(Direction.DESC, "createTime");
|
|
|
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Specification<Lottery> specification = getSpecification(lotterySearchDto);
|
|
|
|
|
|
|
|
Page<Lottery> lotteryPage = lotteryDao.findAll(specification, pageRequest);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long totalElements = lotteryPage.getTotalElements();
|
|
|
|
|
|
|
|
return new PageResult<>(totalElements, lotteryPage.getContent());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Specification<Lottery> getSpecification(LotterySearchDto lotterySearchDto) {
|
|
|
|
|
|
|
|
return (Root<Lottery> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) -> {
|
|
|
|
|
|
|
|
List<Predicate> predicateList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(lotterySearchDto.getSearchStr())) {
|
|
|
|
|
|
|
|
predicateList.add(
|
|
|
|
|
|
|
|
criteriaBuilder.or(
|
|
|
|
|
|
|
|
criteriaBuilder.like(root.get("title"),
|
|
|
|
|
|
|
|
"%" + lotterySearchDto.getSearchStr() + "%"),
|
|
|
|
|
|
|
|
criteriaBuilder.like(root.get("code"), "%" + lotterySearchDto.getSearchStr() + "%")
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lotterySearchDto.getStatus() != null) {
|
|
|
|
|
|
|
|
predicateList.add(criteriaBuilder.equal(root.get("status"), lotterySearchDto.getStatus()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(lotterySearchDto.getCreateUser())) {
|
|
|
|
|
|
|
|
predicateList.add(
|
|
|
|
|
|
|
|
criteriaBuilder.equal(root.get("createUser"), lotterySearchDto.getCreateUser()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|