From 61c9b1eb9243ba6e3d81fdb696fed092a3c0ab04 Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Thu, 1 Aug 2024 14:46:17 +0800 Subject: [PATCH] =?UTF-8?q?release:=20=E6=96=B0=E5=A2=9E=E5=AE=9E=E7=89=A9?= =?UTF-8?q?=E6=8A=BD=E5=A5=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/constants/ErrorConstants.java | 2 + .../src/main/java/enums/PointEnums.java | 3 + .../luoo/user/controller/PointController.java | 5 +- .../com/luoo/user/dto/point/LotteryDto.java | 72 ++ .../main/java/com/luoo/user/pojo/Lottery.java | 8 + .../com/luoo/user/service/LotteryService.java | 737 ++++++++++-------- luoo_user/src/main/resources/sql/20240730.sql | 2 + 7 files changed, 498 insertions(+), 331 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dto/point/LotteryDto.java diff --git a/luoo_common/src/main/java/constants/ErrorConstants.java b/luoo_common/src/main/java/constants/ErrorConstants.java index 5afd99a..f8d7b7d 100644 --- a/luoo_common/src/main/java/constants/ErrorConstants.java +++ b/luoo_common/src/main/java/constants/ErrorConstants.java @@ -52,5 +52,7 @@ public class ErrorConstants { public static final String NOT_ARTIST = "非音乐人不能参与"; public static final String ALREADY_PARTICIPATED = "已参与抽奖"; public static final String NOT_END_OF_SIGN_UP = "报名未结束,不允许抽奖"; + public static final String TICKET_CITY_IS_REQUIRED = "门票抽奖城市必填"; + public static final String REAL_NUMBER_IS_REQUIRED = "实物抽奖数量必填"; } diff --git a/luoo_common/src/main/java/enums/PointEnums.java b/luoo_common/src/main/java/enums/PointEnums.java index 5a11378..2b3c26a 100644 --- a/luoo_common/src/main/java/enums/PointEnums.java +++ b/luoo_common/src/main/java/enums/PointEnums.java @@ -14,6 +14,9 @@ public enum PointEnums { TASK_POINT_TYPE_ADD(1, "增加"), TASK_POINT_TYPE_REDUCE(2, "减少"), + LOTTERY_TICKET(1, "门票抽奖"), + LOTTERY_REAL(2, "实物抽奖"), + LOTTERY_ALL(1, "全部"), LOTTERY_ALL_MEMBER(2, "全部会员"), LOTTERY_LIFE_MEMBER(3, "永久会员"), diff --git a/luoo_user/src/main/java/com/luoo/user/controller/PointController.java b/luoo_user/src/main/java/com/luoo/user/controller/PointController.java index a3c2152..5431c70 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/PointController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/PointController.java @@ -3,6 +3,7 @@ package com.luoo.user.controller; import annotation.GlobalInterceptor; import api.PageResult; import api.Result; +import com.luoo.user.dto.point.LotteryDto; import com.luoo.user.dto.point.LotterySearchDto; import com.luoo.user.dto.point.TaskPointDto; import com.luoo.user.dto.point.UserPointLogSearchDto; @@ -242,7 +243,9 @@ public class PointController { @ApiImplicitParam(name = "lottery", value = "抽奖信息", required = true, dataType = "Lottery", paramType = "body") }) public Result editLottery(@RequestHeader("Authorization") String token, - @RequestBody Lottery lottery) { + @RequestBody LotteryDto lotteryDto) { + Lottery lottery = new Lottery(); + BeanUtils.copyProperties(lotteryDto, lottery); lotteryService.edit(lottery, token); return Result.success(); } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/point/LotteryDto.java b/luoo_user/src/main/java/com/luoo/user/dto/point/LotteryDto.java new file mode 100644 index 0000000..a9beaac --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/point/LotteryDto.java @@ -0,0 +1,72 @@ +package com.luoo.user.dto.point; + +import com.luoo.user.pojo.LotteryRegion; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import javax.persistence.Transient; +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.List; + +/** + * DTO for {@link com.luoo.user.pojo.Lottery} + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class LotteryDto implements Serializable { + + @ApiModelProperty(value = "主键") + private String id; + + @ApiModelProperty(value = "抽奖编码") + private String code; + + @ApiModelProperty(value = "标题") + private String title; + + @ApiModelProperty(value = "抽奖类型 1-门票抽奖 2-实物抽奖") + private Integer type; + + @ApiModelProperty(value = "奖品总数量") + private Integer num; + + @ApiModelProperty(value = "可参加人员 1-全部 2-全部会员 3-永久会员 4-贡献者") + private Integer participant; + + @ApiModelProperty(value = "积分") + private Integer point; + + @ApiModelProperty(value = "地区编码") + private String regionCode; + + @ApiModelProperty(value = "地区名称") + private String regionName; + + @ApiModelProperty(value = "报名开始时间") + private LocalDateTime applyStartTime; + + @ApiModelProperty(value = "报名结束时间") + private LocalDateTime applyEndTime; + + @ApiModelProperty(value = "抽奖方式 1-自动抽奖 2-手动抽奖") + private Integer way; + + @ApiModelProperty(value = "抽奖封面url") + private String image; + + @ApiModelProperty(value = "抽奖描述") + private String description; + + @ApiModelProperty(value = "抽奖状态 0-编辑中 1-报名中 2-已抽奖 3-已停止") + private Integer status; + + @ApiModelProperty(value = "停止原因") + private String stopReason; + + @Transient + @ApiModelProperty(value = "抽奖地区列表") + private List lotteryRegionList; +} \ No newline at end of file diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/Lottery.java b/luoo_user/src/main/java/com/luoo/user/pojo/Lottery.java index 253e476..5f8d80f 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/Lottery.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/Lottery.java @@ -53,6 +53,14 @@ public class Lottery { @ApiModelProperty(value = "标题") private String title; + @Column(name = "type") + @ApiModelProperty(value = "抽奖类型 1-门票抽奖 2-实物抽奖") + private Integer type; + + @Column(name = "num") + @ApiModelProperty(value = "奖品总数量") + private Integer num; + @Column(name = "participant") @ApiModelProperty(value = "可参加人员 1-全部 2-全部会员 3-永久会员 4-贡献者") private Integer participant; diff --git a/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java b/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java index 6f259fc..e668f2e 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/LotteryService.java @@ -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; @@ -53,377 +58,449 @@ import util.RedisLockUtil; @Slf4j 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, - 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; - } + private final UserMessageService userMessageService; - /** - * 新增一条积分抽奖 - * - * @param lottery 抽奖对象 - * @param token 登录token - */ - @Transactional(rollbackFor = Exception.class) - public void add(Lottery lottery, String token) { + public LotteryService(LotteryDao lotteryDao, IdWorker idWorker, JwtUtil jwtUtil, + RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao, + UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, UserMessageService userMessageService) { + 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; + 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(); - StringBuilder regionName = new StringBuilder(); + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); - List lotteryRegionList = lottery.getLotteryRegionList(); + StringBuilder regionCode = new StringBuilder(); + StringBuilder regionName = new StringBuilder(); - for (LotteryRegion lotteryRegion : lotteryRegionList) { - Region regionById = regionService.getRegionById(lotteryRegion.getRegionId()); - regionCode.append(regionById.getCode()).append(","); - regionName.append(regionById.getName()).append(","); - } + String id = String.valueOf(idWorker.nextId()); - lottery.setId(String.valueOf(idWorker.nextId())); - 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); - }); - } - - /** - * 编辑抽奖 - * - * @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 lotteryRegionList = lottery.getLotteryRegionList(); - for (LotteryRegion lotteryRegion : lotteryRegionList) { - Region regionById = regionService.getRegionById(lotteryRegion.getRegionId()); - regionCode.append(regionById.getCode()).append(","); - regionName.append(regionById.getName()).append(","); - } + if (Objects.equals(lottery.getType(), PointEnums.LOTTERY_TICKET.getCode())) { + // 门票抽奖,城市必填 + List lotteryRegionList = lottery.getLotteryRegionList(); - String id = lottery.getId(); - Lottery lottery1 = lotteryDao.findById(id).orElse(null); - if (!Objects.equals(PointEnums.LOTTERY_STATUS_EDITING.getCode(), lottery1.getStatus())) { - // 不是编辑中,不允许修改 - throw new BizException(ErrorConstants.CAN_NOT_EDIT); + 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); + + } 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()); - - 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); - }); - - } - - /** - * 发布抽奖 - * - * @param id 抽奖id - * @param token 登录人token - */ - @Transactional(rollbackFor = Exception.class) - public void publish(String id, String token) { - UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); - - Lottery lottery = lotteryDao.findById(id).orElse(null); - lottery.setUpdateUser(userLoginDto.getUserId()); - lottery.setUpdateUserName(userLoginDto.getNickName()); - - lottery.setStatus(PointEnums.LOTTERY_STATUS_SIGN.getCode()); - - lotteryDao.save(lottery); - - } - - /** - * 停止抽奖 - * - * @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); - - }); - - } - - /** - * 参与抽奖 - * - * @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); + /** + * 编辑抽奖 + * + * @param lottery 抽奖内容 + * @param token 登录人 + */ + @Transactional(rollbackFor = Exception.class) + public void edit(Lottery lottery, String token) { + + 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 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); + + } 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.setCreateTime(oldLottery.getCreateTime()); + lottery.setCreateUser(oldLottery.getCreateUser()); + lottery.setCreateUserName(oldLottery.getCreateUserName()); + lottery.setUpdateUser(userLoginDto.getUserId()); + lottery.setUpdateUserName(userLoginDto.getNickName()); + + lotteryDao.save(lottery); + } - // 校验用户是否可参与 - UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get(); - - 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); - } - } else if (Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) { - // 永久会员可参与 - if (!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) { - throw new BizException(ErrorConstants.NOT_PERMANENT_MEMBER); - } - } else if (Objects.equals(participant, PointEnums.LOTTERY_CONTRIBUTORS.getCode())) { - // 贡献者可参与 - 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); - } + /** + * 发布抽奖 + * + * @param id 抽奖id + * @param token 登录人token + */ + @Transactional(rollbackFor = Exception.class) + public void publish(String id, String token) { + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); + + Lottery lottery = lotteryDao.findById(id).orElse(null); + lottery.setUpdateUser(userLoginDto.getUserId()); + lottery.setUpdateUserName(userLoginDto.getNickName()); + + lottery.setStatus(PointEnums.LOTTERY_STATUS_SIGN.getCode()); + + lotteryDao.save(lottery); + } - List 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) - .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); - - } - - /** - * 自动抽奖结果 - * - * @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); + /** + * 参与抽奖 + * + * @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(); + 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); + } + } else if (Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) { + // 永久会员可参与 + if (!Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) { + throw new BizException(ErrorConstants.NOT_PERMANENT_MEMBER); + } + } else if (Objects.equals(participant, PointEnums.LOTTERY_CONTRIBUTORS.getCode())) { + // 贡献者可参与 + 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 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 lotteryRegionList = lotteryRegionDao.findByLotteryId(id); + + lotteryRegionList.forEach(lotteryRegion -> { + Integer num = lotteryRegion.getNum(); + if (num > 0) { + List lotteryUserList = lotteryUserDao.findByLotteryIdAndRegionId(id, + lotteryRegion.getRegionId()); - List lotteryRegionList = lotteryRegionDao.findByLotteryId(id); + lotteryFunc(token, lotteryUserList, num); - lotteryRegionList.forEach(lotteryRegion -> { - Integer num = lotteryRegion.getNum(); - if (num > 0) { - List lotteryUserList = lotteryUserDao.findByLotteryIdAndRegionId(id, - lotteryRegion.getRegionId()); + } + }); - List winnerList = new ArrayList<>(); - if (lotteryUserList.size() > num) { + } else if (Objects.equals(lottery.getType(), PointEnums.LOTTERY_REAL.getCode())) { + // 实物抽奖业务 + List 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 lotteryUserList, Integer num) { + List winnerList = new ArrayList<>(); + if (lotteryUserList.size() > num) { // 抽奖人数大于设定人数 // 随机设定人数的数量中奖 - winnerList = drawWinners(lotteryUserList, lotteryRegion.getNum()); - } else { + winnerList = drawWinners(lotteryUserList, num); + } else { // 抽奖人数小于设定人数 // 全部中奖 winnerList = lotteryUserList; - } + } - winnerList.forEach(lotteryUser -> { + winnerList.forEach(lotteryUser -> { lotteryUser.setResult(PointEnums.LOTTERY_RESULT_WIN.getCode()); - if(StringUtils.isNotBlank(token)) { - UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); - lotteryUser.setUpdateUser(userLoginDto.getUserId()); + if (StringUtils.isNotBlank(token)) { + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); + lotteryUser.setUpdateUser(userLoginDto.getUserId()); } lotteryUserDao.save(lotteryUser); - }); - } - }); - // 抽奖状态为报名中,自动抽奖 - lottery.setStatus(PointEnums.LOTTERY_STATUS_LOTTERY.getCode()); - lotteryDao.save(lottery); + // 发送恭喜中奖站内信 + 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 drawWinners(List list, int winnersCount) { + List 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 lotteryPageResult(Integer page, Integer size, + LotterySearchDto lotterySearchDto) { + Sort sort = new Sort(Direction.DESC, "createTime"); + PageRequest pageRequest = PageRequest.of(page - 1, size, sort); + + Specification specification = getSpecification(lotterySearchDto); + Page lotteryPage = lotteryDao.findAll(specification, pageRequest); + + long totalElements = lotteryPage.getTotalElements(); + return new PageResult<>(totalElements, lotteryPage.getContent()); + } + + private Specification getSpecification(LotterySearchDto lotterySearchDto) { + return (Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) -> { + List 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 drawWinners(List participants, int winnersCount) { - List 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 lotteryPageResult(Integer page, Integer size, - LotterySearchDto lotterySearchDto) { - Sort sort = new Sort(Direction.DESC, "createTime"); - PageRequest pageRequest = PageRequest.of(page - 1, size, sort); - - Specification specification = getSpecification(lotterySearchDto); - Page lotteryPage = lotteryDao.findAll(specification, pageRequest); - - long totalElements = lotteryPage.getTotalElements(); - return new PageResult<>(totalElements, lotteryPage.getContent()); - } - - private Specification getSpecification(LotterySearchDto lotterySearchDto) { - return (Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) -> { - List 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()])); - }; - } } diff --git a/luoo_user/src/main/resources/sql/20240730.sql b/luoo_user/src/main/resources/sql/20240730.sql index 0f4bc21..8335606 100644 --- a/luoo_user/src/main/resources/sql/20240730.sql +++ b/luoo_user/src/main/resources/sql/20240730.sql @@ -3,6 +3,8 @@ create table tb_lottery id varchar(20) not null comment 'id' primary key, code varchar(50) null comment '抽奖码', + type tinyint null comment '抽奖类型 1-门票抽奖 2-实物抽奖', + num int null comment '奖品总数量', title varchar(255) null comment '抽奖标题', participant int null comment '可参加人员 1-全部 2-全部会员 3-永久会员 4-贡献者 5-音乐人', point int null comment '消耗积分',