release: 永久会员积分抽奖8折

release-2024-04-25
huangyw 3 months ago
parent 2fa061afdf
commit 928bb1c436

@ -58,5 +58,6 @@ public class ErrorConstants {
public static final String ALREADY_PARTICIPATED_CANNOT_PARTICIPATE_AGAIN = "您已参加,不能再次参与"; 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 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 APPLY_END_TIME_CANNOT_BE_BEFORE_APPLY_START_TIME = "报名结束时间不能在报名开始时间之前";
public static final String POINT_NOT_ENOUGH = "积分不足";
} }

@ -10,6 +10,7 @@ public enum PointEnums {
TASK_TYPE_NEW(1, "新手任务"), TASK_TYPE_NEW(1, "新手任务"),
TASK_TYPE_DAILY(2, "日常任务"), TASK_TYPE_DAILY(2, "日常任务"),
TASK_TYPE_SPECIAL(3, "特殊任务"),
TASK_POINT_TYPE_ADD(1, "增加"), TASK_POINT_TYPE_ADD(1, "增加"),
TASK_POINT_TYPE_REDUCE(2, "减少"), TASK_POINT_TYPE_REDUCE(2, "减少"),

@ -23,4 +23,7 @@ public class EnvConfig {
@ApiModelProperty(value = "下载地址") @ApiModelProperty(value = "下载地址")
private String downloadUrl; private String downloadUrl;
@ApiModelProperty(value = "永久会员积分抽奖折扣")
private Float permanentMemberDiscount;
} }

@ -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;
}

@ -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);
}
}

@ -2,10 +2,12 @@ package com.luoo.user.service;
import api.PageResult; import api.PageResult;
import com.luoo.user.Scheduler.DrawLotteryScheduler; import com.luoo.user.Scheduler.DrawLotteryScheduler;
import com.luoo.user.config.EnvConfig;
import com.luoo.user.dao.LotteryDao; import com.luoo.user.dao.LotteryDao;
import com.luoo.user.dao.LotteryRegionDao; import com.luoo.user.dao.LotteryRegionDao;
import com.luoo.user.dao.LotteryUserDao; import com.luoo.user.dao.LotteryUserDao;
import com.luoo.user.dao.UserInfoDao; import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.dto.point.DrawDTO;
import com.luoo.user.dto.point.LotterySearchDto; import com.luoo.user.dto.point.LotterySearchDto;
import com.luoo.user.pojo.*; import com.luoo.user.pojo.*;
import com.luoo.user.vo.point.*; 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 POINTS_WILL_BE_RETURNED = "抽奖返积分";
public static final String LOTTERY_POINTS_TYPE = "积分抽奖";
private final LotteryDao lotteryDao; private final LotteryDao lotteryDao;
private final JwtUtil jwtUtil; private final JwtUtil jwtUtil;
@ -78,7 +78,9 @@ public class LotteryService {
private final JPAQueryFactory jpaQueryFactory; 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.lotteryDao = lotteryDao;
this.jwtUtil = jwtUtil; this.jwtUtil = jwtUtil;
this.idWorker = idWorker; this.idWorker = idWorker;
@ -90,6 +92,7 @@ public class LotteryService {
this.lotteryUserDao = lotteryUserDao; this.lotteryUserDao = lotteryUserDao;
this.drawLotteryScheduler = drawLotteryScheduler; this.drawLotteryScheduler = drawLotteryScheduler;
this.jpaQueryFactory = jpaQueryFactory; this.jpaQueryFactory = jpaQueryFactory;
this.envConfig = envConfig;
} }
@ -358,33 +361,25 @@ public class LotteryService {
} }
} }
LotteryUser checkLotteryUser = lotteryUserDao.findByLotteryIdAndUserId( Integer point = lottery.getPoint();
id, userLoginDto.getUserId()); // 永久会员
if (checkLotteryUser != null) { if (Objects.equals(userInfo.getVipStatus(), UserVipStatusEnum.LIFE.getCode())) {
throw new BizException(ErrorConstants.ALREADY_PARTICIPATED); // 向下取整
point = (int) Math.floor(point * envConfig.getPermanentMemberDiscount());
}
// 积分不足
if (point > userInfo.getPoint()) {
throw new BizException(ErrorConstants.POINT_NOT_ENOUGH);
} }
LotteryUser lotteryUser = LotteryUser.builder() DrawDTO drawDTO = DrawDTO.builder()
.id(String.valueOf(idWorker.nextId()))
.lotteryId(id) .lotteryId(id)
.regionId(regionId) .regionId(regionId)
.userId(userLoginDto.getUserId()) .score(point)
.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()) .type(PointEnums.TASK_POINT_TYPE_REDUCE.getCode())
.createUser(lotteryUser.getUserId()) .userId(userLoginDto.getUserId())
.taskPointId(null)
.description(LOTTERY_POINTS_TYPE)
.userId(lotteryUser.getUserId())
.score(lottery.getPoint())
.build(); .build();
rabbitTemplate.convertAndSend("pointLog", userPointLog); rabbitTemplate.convertAndSend("draw", drawDTO);
} }

@ -1,9 +1,11 @@
package com.luoo.user.service; package com.luoo.user.service;
import api.PageResult; import api.PageResult;
import com.luoo.user.dao.LotteryUserDao;
import com.luoo.user.dao.TaskPointDao; import com.luoo.user.dao.TaskPointDao;
import com.luoo.user.dao.UserInfoDao; import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.dao.UserPointLogDao; import com.luoo.user.dao.UserPointLogDao;
import com.luoo.user.dto.point.DrawDTO;
import com.luoo.user.dto.point.UserPointLogSearchDto; import com.luoo.user.dto.point.UserPointLogSearchDto;
import com.luoo.user.pojo.*; import com.luoo.user.pojo.*;
import com.luoo.user.vo.point.UserPointLogVO; 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.Expressions;
import com.querydsl.core.types.dsl.StringExpression; import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.jpa.impl.JPAQueryFactory; import com.querydsl.jpa.impl.JPAQueryFactory;
import constants.ErrorConstants;
import constants.TaskPointIdConstants; import constants.TaskPointIdConstants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.PointEnums; import enums.PointEnums;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.persistence.EntityManager;
import javax.persistence.criteria.*;
import exception.BizException;
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;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import util.IdWorker; import util.IdWorker;
@ -49,6 +46,8 @@ import util.JwtUtil;
@Slf4j @Slf4j
public class UserPointLogService { public class UserPointLogService {
public static final String LOTTERY_POINTS_TYPE = "积分抽奖";
private final UserPointLogDao userPointLogDao; private final UserPointLogDao userPointLogDao;
private final IdWorker idWorker; private final IdWorker idWorker;
@ -63,10 +62,10 @@ public class UserPointLogService {
private final JPAQueryFactory jpaQueryFactory; private final JPAQueryFactory jpaQueryFactory;
private final LotteryUserDao lotteryUserDao;
public UserPointLogService(UserPointLogDao userPointLogDao, IdWorker idWorker, JwtUtil jwtUtil, 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.userPointLogDao = userPointLogDao;
this.idWorker = idWorker; this.idWorker = idWorker;
this.jwtUtil = jwtUtil; this.jwtUtil = jwtUtil;
@ -74,6 +73,7 @@ public class UserPointLogService {
this.rabbitTemplate = rabbitTemplate; this.rabbitTemplate = rabbitTemplate;
this.userInfoDao = userInfoDao; this.userInfoDao = userInfoDao;
this.jpaQueryFactory = jpaQueryFactory; this.jpaQueryFactory = jpaQueryFactory;
this.lotteryUserDao = lotteryUserDao;
} }
private TaskPointService taskPointService; private TaskPointService taskPointService;
@ -201,6 +201,51 @@ public class UserPointLogService {
userInfoDao.save(userInfo); 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);
}
}
/** /**
* *
* *

Loading…
Cancel
Save