|
|
|
@ -12,12 +12,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;
|
|
|
|
@ -28,6 +32,7 @@ import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
|
import javax.persistence.criteria.Root;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
@ -75,9 +80,11 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
private final LotteryUserDao lotteryUserDao;
|
|
|
|
|
|
|
|
|
|
private final UserMessageService userMessageService;
|
|
|
|
|
|
|
|
|
|
public LotteryService(LotteryDao lotteryDao, IdWorker idWorker, JwtUtil jwtUtil,
|
|
|
|
|
RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao,
|
|
|
|
|
UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao) {
|
|
|
|
|
UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, UserMessageService userMessageService) {
|
|
|
|
|
this.lotteryDao = lotteryDao;
|
|
|
|
|
this.idWorker = idWorker;
|
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
@ -87,6 +94,7 @@ public class LotteryService {
|
|
|
|
|
this.userInfoDao = userInfoDao;
|
|
|
|
|
this.rabbitTemplate = rabbitTemplate;
|
|
|
|
|
this.lotteryUserDao = lotteryUserDao;
|
|
|
|
|
this.userMessageService = userMessageService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -103,34 +111,50 @@ public class LotteryService {
|
|
|
|
|
StringBuilder regionCode = new StringBuilder();
|
|
|
|
|
StringBuilder regionName = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
String id = String.valueOf(idWorker.nextId());
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
lottery.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
|
} 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());
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -144,40 +168,61 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
|
|
|
|
|
String id = lottery.getId();
|
|
|
|
|
|
|
|
|
|
StringBuilder regionCode = new StringBuilder();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Integer num = 0;
|
|
|
|
|
|
|
|
|
|
lotteryRegionDao.deleteByLotteryId(id);
|
|
|
|
|
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(String.valueOf(idWorker.nextId()));
|
|
|
|
|
lotteryRegion.setLotteryId(id);
|
|
|
|
|
lotteryRegion.setCreateUser(userLoginDto.getUserId());
|
|
|
|
|
lotteryRegion.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
|
|
lotteryRegionDao.save(lotteryRegion);
|
|
|
|
|
}
|
|
|
|
|
lottery.setRegionCode(regionCode.toString());
|
|
|
|
|
lottery.setRegionName(regionName.toString());
|
|
|
|
|
lottery.setNum(num);
|
|
|
|
|
|
|
|
|
|
String id = lottery.getId();
|
|
|
|
|
Lottery lottery1 = lotteryDao.findById(id).orElse(null);
|
|
|
|
|
if (!Objects.equals(PointEnums.LOTTERY_STATUS_EDITING.getCode(), lottery1.getStatus())) {
|
|
|
|
|
} else {
|
|
|
|
|
// 实物抽奖,数量必填
|
|
|
|
|
if (lottery.getNum() == null) {
|
|
|
|
|
throw new BizException(ErrorConstants.REAL_NUMBER_IS_REQUIRED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Lottery oldLottery = lotteryDao.getOne(id);
|
|
|
|
|
if (!Objects.equals(PointEnums.LOTTERY_STATUS_EDITING.getCode(), oldLottery.getStatus())) {
|
|
|
|
|
// 不是编辑中,不允许修改
|
|
|
|
|
throw new BizException(ErrorConstants.CAN_NOT_EDIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lottery.setRegionCode(regionCode.toString());
|
|
|
|
|
lottery.setRegionName(regionName.toString());
|
|
|
|
|
lottery.setCreateTime(oldLottery.getCreateTime());
|
|
|
|
|
lottery.setCreateUser(oldLottery.getCreateUser());
|
|
|
|
|
lottery.setCreateUserName(oldLottery.getCreateUserName());
|
|
|
|
|
lottery.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
lottery.setUpdateUserName(userLoginDto.getNickName());
|
|
|
|
|
|
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
|
|
|
|
|
|
lotteryRegionDao.deleteByLotteryId(id);
|
|
|
|
|
lotteryRegionList.forEach(lotteryRegion -> {
|
|
|
|
|
lotteryRegion.setId(String.valueOf(idWorker.nextId()));
|
|
|
|
|
lotteryRegion.setLotteryId(id);
|
|
|
|
|
lotteryRegion.setCreateUser(userLoginDto.getUserId());
|
|
|
|
|
lotteryRegion.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
|
|
|
|
|
lotteryRegionDao.save(lotteryRegion);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -286,7 +331,7 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
List<LotteryUser> checkUserList = lotteryUserDao.findByLotteryIdAndUserIdAndRegionId(
|
|
|
|
|
id, userLoginDto.getUserId(), regionId);
|
|
|
|
|
if(!checkUserList.isEmpty()) {
|
|
|
|
|
if (!checkUserList.isEmpty()) {
|
|
|
|
|
throw new BizException(ErrorConstants.ALREADY_PARTICIPATED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -332,6 +377,8 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
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 -> {
|
|
|
|
@ -340,11 +387,37 @@ public class LotteryService {
|
|
|
|
|
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, lotteryRegion.getNum());
|
|
|
|
|
winnerList = drawWinners(lotteryUserList, num);
|
|
|
|
|
} else {
|
|
|
|
|
// 抽奖人数小于设定人数
|
|
|
|
|
// 全部中奖
|
|
|
|
@ -353,30 +426,34 @@ public class LotteryService {
|
|
|
|
|
|
|
|
|
|
winnerList.forEach(lotteryUser -> {
|
|
|
|
|
lotteryUser.setResult(PointEnums.LOTTERY_RESULT_WIN.getCode());
|
|
|
|
|
if(StringUtils.isNotBlank(token)) {
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
// 抽奖状态为报名中,自动抽奖
|
|
|
|
|
lottery.setStatus(PointEnums.LOTTERY_STATUS_LOTTERY.getCode());
|
|
|
|
|
lotteryDao.save(lottery);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从参与者列表中随机抽取指定数量的中奖者。
|
|
|
|
|
*
|
|
|
|
|
* @param participants 参与者列表
|
|
|
|
|
* @param list 参与者列表
|
|
|
|
|
* @param winnersCount 中奖者数量
|
|
|
|
|
* @return 中奖者列表
|
|
|
|
|
*/
|
|
|
|
|
private List<LotteryUser> drawWinners(List<LotteryUser> participants, int winnersCount) {
|
|
|
|
|
List<LotteryUser> winners = new ArrayList<>(participants);
|
|
|
|
|
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()));
|
|
|
|
|
}
|
|
|
|
|