123
This commit is contained in:
@@ -34,8 +34,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
@Slf4j
|
||||
public class LotteryService {
|
||||
|
||||
// 固定配置
|
||||
private static final Long THANKS_PRIZE_ID = 0L;
|
||||
private static final String USER_DRAW_COUNT_KEY = "user:draw:count:%s";
|
||||
private static final long USER_DRAW_COUNT_EXPIRE = 7 * 24 * 60 * 60; // 用户累计抽数缓存过期时间:7天
|
||||
private static final double RANDOM_MAX = 10000; // 概率放大倍数,提升随机数精度
|
||||
@@ -69,14 +67,14 @@ public class LotteryService {
|
||||
if(user == null){
|
||||
throw new ServiceException("用户不存在");
|
||||
}
|
||||
boolean openDraw = drawService.getOpenDraw(user.getGender());
|
||||
if(!openDraw){
|
||||
throw new ServiceException("暂未开启积分抽奖,请等待活动通知");
|
||||
}
|
||||
boolean select = GenderEnum.isSelect(user.getGender());
|
||||
if(select){
|
||||
throw new ServiceException("请选择性别后在抽奖");
|
||||
}
|
||||
boolean openDraw = drawService.getOpenDraw(user.getGender());
|
||||
if(!openDraw){
|
||||
throw new ServiceException("暂未开启积分抽奖,请等待活动通知");
|
||||
}
|
||||
Account account = accountService.getByUserId(user.getId());
|
||||
Integer drawPoint = drawService.getDrawPoint(user.getGender());
|
||||
if(account.getPoints() < drawPoint){
|
||||
@@ -120,10 +118,23 @@ public class LotteryService {
|
||||
int currentContinuousDraws = getContinuousDraws(userId);
|
||||
int newContinuousDraws = currentContinuousDraws + 1;
|
||||
log.info("用户{}当前累计抽数:{},本次抽数:{}", userId, currentContinuousDraws, newContinuousDraws);
|
||||
// 步骤2:获取有效奖品列表(启用状态,排除谢谢惠顾)
|
||||
// 步骤2:获取所有奖品列表
|
||||
List<PrizeOnline> validPrizes = prizeOnlineService.selectPrizeOnlineList(user.getGender());
|
||||
if (validPrizes.isEmpty()) {
|
||||
throw new ServiceException("无有效奖品,请刷新页面后再次抽奖");
|
||||
throw new ServiceException("无有效奖品,请刷新后再次抽奖");
|
||||
}
|
||||
// 找出系统内置谢谢惠顾
|
||||
PrizeOnline thankPrize = null;
|
||||
for (int i = 0; i < validPrizes.size(); i++) {
|
||||
PrizeOnline validPrize = validPrizes.get(i);
|
||||
if(validPrize.getPrizeId() == 1){
|
||||
thankPrize = validPrize;
|
||||
validPrizes.remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(thankPrize == null){
|
||||
throw new ServiceException("奖品库异常,请刷新后再次抽奖");
|
||||
}
|
||||
// 步骤3:执行抽奖规则(保底→最低抽数过滤→概率抽奖)
|
||||
PrizeOnline winPrize = null;
|
||||
@@ -236,13 +247,10 @@ public class LotteryService {
|
||||
if (cacheCount != null) {
|
||||
return cacheCount;
|
||||
}
|
||||
// 2. 缓存未命中,从数据库查询最后一次累计抽数
|
||||
// Integer dbCount = userDrawRecordMapper.selectLastContinuousDraws(userId);
|
||||
Integer dbCount = 0;
|
||||
int finalCount = dbCount == null ? 0 : dbCount;
|
||||
// 3. 存入缓存(设置过期时间)
|
||||
bucket.set(finalCount, USER_DRAW_COUNT_EXPIRE, java.util.concurrent.TimeUnit.SECONDS);
|
||||
return finalCount;
|
||||
// 3. 存入缓存
|
||||
// bucket.set(0, USER_DRAW_COUNT_EXPIRE, java.util.concurrent.TimeUnit.SECONDS);
|
||||
bucket.set(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
|
||||
Reference in New Issue
Block a user