From 928bb1c436d763b3060bda0a6c00bc73d79465b8 Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Mon, 19 Aug 2024 09:45:29 +0800 Subject: [PATCH] =?UTF-8?q?release:=20=E6=B0=B8=E4=B9=85=E4=BC=9A=E5=91=98?= =?UTF-8?q?=E7=A7=AF=E5=88=86=E6=8A=BD=E5=A5=968=E6=8A=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/constants/ErrorConstants.java | 1 + .../src/main/java/enums/PointEnums.java | 1 + .../java/com/luoo/user/config/EnvConfig.java | 3 + .../java/com/luoo/user/dto/point/DrawDTO.java | 39 ++++++++++++ .../com/luoo/user/listener/drawListener.java | 32 ++++++++++ .../com/luoo/user/service/LotteryService.java | 43 ++++++------- .../user/service/UserPointLogService.java | 63 ++++++++++++++++--- 7 files changed, 149 insertions(+), 33 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dto/point/DrawDTO.java create mode 100644 luoo_user/src/main/java/com/luoo/user/listener/drawListener.java diff --git a/luoo_common/src/main/java/constants/ErrorConstants.java b/luoo_common/src/main/java/constants/ErrorConstants.java index 68841d0..80fdb4b 100644 --- a/luoo_common/src/main/java/constants/ErrorConstants.java +++ b/luoo_common/src/main/java/constants/ErrorConstants.java @@ -58,5 +58,6 @@ public class ErrorConstants { public static final String ALREADY_PARTICIPATED_CANNOT_PARTICIPATE_AGAIN = "您已参加,不能再次参与"; public static final String DRAW_TIME_CANNOT_BE_BEFORE_APPLY_END_TIME = "开奖时间不能在报名结束时间之前"; public static final String APPLY_END_TIME_CANNOT_BE_BEFORE_APPLY_START_TIME = "报名结束时间不能在报名开始时间之前"; + public static final String POINT_NOT_ENOUGH = "积分不足"; } diff --git a/luoo_common/src/main/java/enums/PointEnums.java b/luoo_common/src/main/java/enums/PointEnums.java index ef789cb..a7841f9 100644 --- a/luoo_common/src/main/java/enums/PointEnums.java +++ b/luoo_common/src/main/java/enums/PointEnums.java @@ -10,6 +10,7 @@ public enum PointEnums { TASK_TYPE_NEW(1, "新手任务"), TASK_TYPE_DAILY(2, "日常任务"), + TASK_TYPE_SPECIAL(3, "特殊任务"), TASK_POINT_TYPE_ADD(1, "增加"), TASK_POINT_TYPE_REDUCE(2, "减少"), diff --git a/luoo_user/src/main/java/com/luoo/user/config/EnvConfig.java b/luoo_user/src/main/java/com/luoo/user/config/EnvConfig.java index 1431a4e..7bfff33 100644 --- a/luoo_user/src/main/java/com/luoo/user/config/EnvConfig.java +++ b/luoo_user/src/main/java/com/luoo/user/config/EnvConfig.java @@ -23,4 +23,7 @@ public class EnvConfig { @ApiModelProperty(value = "下载地址") private String downloadUrl; + @ApiModelProperty(value = "永久会员积分抽奖折扣") + private Float permanentMemberDiscount; + } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/point/DrawDTO.java b/luoo_user/src/main/java/com/luoo/user/dto/point/DrawDTO.java new file mode 100644 index 0000000..a662b88 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/point/DrawDTO.java @@ -0,0 +1,39 @@ +package com.luoo.user.dto.point; + +import io.swagger.annotations.ApiModelProperty; +import lombok.*; + +import javax.validation.constraints.Size; +import java.io.Serializable; + +/** + * DTO for {@link com.luoo.user.pojo.UserPointLog} + * + * 抽奖rabbitmq对象 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class DrawDTO implements Serializable { + + private static final long serialVersionUID = 1115530233333204283L; + + @ApiModelProperty(value = "积分") + Integer score; + + @ApiModelProperty(value = "任务积分id") + Integer type; + + @ApiModelProperty(value = "用户id") + String userId; + + @ApiModelProperty(value = "任务积分id") + String taskPointId; + + @ApiModelProperty(value = "抽奖区域id") + Integer regionId; + + @ApiModelProperty(value = "抽奖id") + String lotteryId; +} \ No newline at end of file diff --git a/luoo_user/src/main/java/com/luoo/user/listener/drawListener.java b/luoo_user/src/main/java/com/luoo/user/listener/drawListener.java new file mode 100644 index 0000000..fe02473 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/listener/drawListener.java @@ -0,0 +1,32 @@ +package com.luoo.user.listener; + +import com.luoo.user.dto.point.DrawDTO; +import com.luoo.user.pojo.UserPointLog; +import com.luoo.user.service.UserPointLogService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * @program: luoo_parent + * @description: 抽奖队列 + * @author: yawei.huang + * @create: 2024-08-19 09:28 + **/ +@Component +@RabbitListener(queues = "draw") +@Slf4j +public class drawListener { + + @Autowired + private UserPointLogService userPointLogService; + + @RabbitHandler + public void executeDraw(DrawDTO drawDTO) { + log.info("drawDTO:{}", drawDTO); + userPointLogService.executeDraw(drawDTO); + } + +} 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 d468532..8de7f3a 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 @@ -2,10 +2,12 @@ package com.luoo.user.service; import api.PageResult; import com.luoo.user.Scheduler.DrawLotteryScheduler; +import com.luoo.user.config.EnvConfig; import com.luoo.user.dao.LotteryDao; import com.luoo.user.dao.LotteryRegionDao; import com.luoo.user.dao.LotteryUserDao; import com.luoo.user.dao.UserInfoDao; +import com.luoo.user.dto.point.DrawDTO; import com.luoo.user.dto.point.LotterySearchDto; import com.luoo.user.pojo.*; import com.luoo.user.vo.point.*; @@ -54,8 +56,6 @@ public class LotteryService { public static final String POINTS_WILL_BE_RETURNED = "抽奖返积分"; - public static final String LOTTERY_POINTS_TYPE = "积分抽奖"; - private final LotteryDao lotteryDao; private final JwtUtil jwtUtil; @@ -78,7 +78,9 @@ public class LotteryService { private final JPAQueryFactory jpaQueryFactory; - public LotteryService(LotteryDao lotteryDao, JwtUtil jwtUtil, IdWorker idWorker, RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao, UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, DrawLotteryScheduler drawLotteryScheduler, JPAQueryFactory jpaQueryFactory) { + private final EnvConfig envConfig; + + public LotteryService(LotteryDao lotteryDao, JwtUtil jwtUtil, IdWorker idWorker, RedisLockUtil redisLockUtil, RegionService regionService, LotteryRegionDao lotteryRegionDao, UserInfoDao userInfoDao, RabbitTemplate rabbitTemplate, LotteryUserDao lotteryUserDao, DrawLotteryScheduler drawLotteryScheduler, JPAQueryFactory jpaQueryFactory, EnvConfig envConfig) { this.lotteryDao = lotteryDao; this.jwtUtil = jwtUtil; this.idWorker = idWorker; @@ -90,6 +92,7 @@ public class LotteryService { this.lotteryUserDao = lotteryUserDao; this.drawLotteryScheduler = drawLotteryScheduler; this.jpaQueryFactory = jpaQueryFactory; + this.envConfig = envConfig; } @@ -358,33 +361,25 @@ public class LotteryService { } } - LotteryUser checkLotteryUser = lotteryUserDao.findByLotteryIdAndUserId( - id, userLoginDto.getUserId()); - if (checkLotteryUser != null) { - throw new BizException(ErrorConstants.ALREADY_PARTICIPATED); + Integer point = lottery.getPoint(); + // 永久会员 + if (Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) { + // 向下取整 + point = (int) Math.floor(point * envConfig.getPermanentMemberDiscount()); + } + // 积分不足 + if (point > userInfo.getPoint()) { + throw new BizException(ErrorConstants.POINT_NOT_ENOUGH); } - LotteryUser lotteryUser = LotteryUser.builder() - .id(String.valueOf(idWorker.nextId())) + DrawDTO drawDTO = DrawDTO.builder() .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())) + .score(point) .type(PointEnums.TASK_POINT_TYPE_REDUCE.getCode()) - .createUser(lotteryUser.getUserId()) - .taskPointId(null) - .description(LOTTERY_POINTS_TYPE) - .userId(lotteryUser.getUserId()) - .score(lottery.getPoint()) + .userId(userLoginDto.getUserId()) .build(); - rabbitTemplate.convertAndSend("pointLog", userPointLog); + rabbitTemplate.convertAndSend("draw", drawDTO); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java index 783f6ab..d360416 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java @@ -1,9 +1,11 @@ package com.luoo.user.service; import api.PageResult; +import com.luoo.user.dao.LotteryUserDao; import com.luoo.user.dao.TaskPointDao; import com.luoo.user.dao.UserInfoDao; import com.luoo.user.dao.UserPointLogDao; +import com.luoo.user.dto.point.DrawDTO; import com.luoo.user.dto.point.UserPointLogSearchDto; import com.luoo.user.pojo.*; import com.luoo.user.vo.point.UserPointLogVO; @@ -12,28 +14,23 @@ import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.Expressions; import com.querydsl.core.types.dsl.StringExpression; import com.querydsl.jpa.impl.JPAQueryFactory; +import constants.ErrorConstants; import constants.TaskPointIdConstants; import dto.UserLoginDto; import enums.PointEnums; import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; import java.util.List; import java.util.Objects; -import javax.persistence.EntityManager; -import javax.persistence.criteria.*; +import exception.BizException; 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.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.data.domain.Sort.Direction; -import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; @@ -49,6 +46,8 @@ import util.JwtUtil; @Slf4j public class UserPointLogService { + public static final String LOTTERY_POINTS_TYPE = "积分抽奖"; + private final UserPointLogDao userPointLogDao; private final IdWorker idWorker; @@ -63,10 +62,10 @@ public class UserPointLogService { private final JPAQueryFactory jpaQueryFactory; - + private final LotteryUserDao lotteryUserDao; public UserPointLogService(UserPointLogDao userPointLogDao, IdWorker idWorker, JwtUtil jwtUtil, - TaskPointDao taskPointDao, RabbitTemplate rabbitTemplate, UserInfoDao userInfoDao, JPAQueryFactory jpaQueryFactory) { + TaskPointDao taskPointDao, RabbitTemplate rabbitTemplate, UserInfoDao userInfoDao, JPAQueryFactory jpaQueryFactory, LotteryUserDao lotteryUserDao) { this.userPointLogDao = userPointLogDao; this.idWorker = idWorker; this.jwtUtil = jwtUtil; @@ -74,6 +73,7 @@ public class UserPointLogService { this.rabbitTemplate = rabbitTemplate; this.userInfoDao = userInfoDao; this.jpaQueryFactory = jpaQueryFactory; + this.lotteryUserDao = lotteryUserDao; } private TaskPointService taskPointService; @@ -201,6 +201,51 @@ public class UserPointLogService { userInfoDao.save(userInfo); } + @Transactional(rollbackFor = Exception.class) + public void executeDraw(DrawDTO drawDTO) { + + String taskPointId = drawDTO.getTaskPointId(); + if (taskPointId != null) { + TaskPoint taskPoint = taskPointService.getTaskPoint(taskPointId); + if(taskPoint == null) { + return ; + } + + // 调整用户积分 + UserInfo userInfo = userInfoDao.findById(drawDTO.getUserId()).get(); + int point = userInfo.getPoint() == null ? 0 : userInfo.getPoint(); + if(point < taskPoint.getScore()) { + throw new BizException(ErrorConstants.POINT_NOT_ENOUGH); + } + userInfo.setPoint(point - taskPoint.getScore()); + userInfoDao.save(userInfo); + + UserPointLog userPointLog = UserPointLog.builder() + .id(String.valueOf(idWorker.nextId())) + .type(PointEnums.TASK_POINT_TYPE_REDUCE.getCode()) + .createUser(drawDTO.getUserId()) + .taskPointId(null) + .description(LOTTERY_POINTS_TYPE) + .userId(drawDTO.getUserId()) + .score(point) + .build(); + userPointLogDao.save(userPointLog); + + // 确保先扣除积分,再报名成功 + // 抽奖报名成功 + LotteryUser lotteryUser = LotteryUser.builder() + .id(String.valueOf(idWorker.nextId())) + .lotteryId(drawDTO.getLotteryId()) + .regionId(drawDTO.getRegionId()) + .userId(drawDTO.getUserId()) + .createUser(drawDTO.getUserId()) + .updateUser(drawDTO.getUserId()) + .build(); + + lotteryUserDao.save(lotteryUser); + } + } + /** * 每日签到 *