登录改造

This commit is contained in:
777
2025-12-26 12:16:29 +08:00
parent f0d4b595f4
commit 9cd9841f09
17 changed files with 335 additions and 181 deletions

View File

@@ -1,167 +1,284 @@
package com.ruoyi.cai.lottery;
import com.lottery.entity.Prize;
import com.lottery.entity.UserDrawRecord;
import com.lottery.mapper.UserDrawRecordMapper;
import com.ruoyi.cai.domain.PrizeInfo;
import com.ruoyi.cai.domain.Account;
import com.ruoyi.cai.domain.PointChangeLog;
import com.ruoyi.cai.domain.PrizeOnline;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.enums.GenderEnum;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.manager.IdManager;
import com.ruoyi.cai.manager.SystemConfigManager;
import com.ruoyi.cai.service.AccountService;
import com.ruoyi.cai.service.PrizeOnlineService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/**
* 抽奖核心服务(优化版)
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class LotteryService {
private static final Long THANKS_PRIZE_ID = 1L;
// 固定配置
private static final Long THANKS_PRIZE_ID = 0L;
private static final String USER_DRAW_COUNT_KEY = "user:draw:count:%s";
// 每个用户的锁防止并发抽奖单机版用ReentrantLock
private final ReentrantLock lock = new ReentrantLock();
private static final long USER_DRAW_COUNT_EXPIRE = 7 * 24 * 60 * 60; // 用户累计抽数缓存过期时间7天
private static final double RANDOM_MAX = 10000; // 概率放大倍数,提升随机数精度
private static final long LOCK_TIMEOUT = 500; // 锁超时时间500ms非阻塞获取
// 每个用户的独立锁key=userIdvalue=ReentrantLock
private final ConcurrentHashMap<Long, ReentrantLock> userLockMap = new ConcurrentHashMap<>();
@Autowired
private PrizeOnlineService prizeOnlineService;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
private RedissonClient redissonClient;
@Autowired
private UserService userService;
@Autowired
private AccountService accountService;
@Autowired
private SystemConfigManager systemConfigManager;
private Integer getDrawPoint(Integer gender){
if(GenderEnum.WOMEN.getCode().equals(gender)){
Integer womenDrawPoint = systemConfigManager.getSystemConfigOfInt(SystemConfigEnum.WOMEN_DRAW_POINT);
return womenDrawPoint;
}
return systemConfigManager.getSystemConfigOfInt(SystemConfigEnum.MEN_DRAW_POINT);
}
/**
* 用户抽奖
* 用户抽奖(核心方法,优化后)
* @param userId 用户ID
* @return 中奖奖品
*/
public PrizeInfo draw(Long userId) {
// 加锁防止并发抽奖
lock.lock();
public PrizeOnline draw(Long userId) {
boolean openDraw = systemConfigManager.getSystemConfigOfBool(SystemConfigEnum.OPEN_DRAW);
if(!openDraw){
throw new ServiceException("暂未开启积分抽奖,请等待活动通知");
}
User user = userService.getById(userId);
if(user == null){
throw new ServiceException("用户不存在");
}
boolean select = GenderEnum.isSelect(user.getGender());
if(select){
throw new ServiceException("请选择性别后在抽奖");
}
Account account = accountService.getByUserId(user.getId());
Integer drawPoint = getDrawPoint(user.getGender());
if(account.getPoints() < drawPoint){
throw new ServiceException("积分不足");
}
ReentrantLock userLock = userLockMap.computeIfAbsent(userId, k -> new ReentrantLock());
try {
// 1. 获取用户累计抽数
int continuousDraws = getContinuousDraws(userId);
int newContinuousDraws = continuousDraws + 1;
// 2. 获取启用的奖品(排除谢谢惠顾)
List<Prize> prizes = prizeService.getEnablePrizesExcludeThanks();
if (prizes.isEmpty()) {
// 只有谢谢惠顾
saveDrawRecord(userId, THANKS_PRIZE_ID, newContinuousDraws);
return prizeService.getPrizeFromCache(THANKS_PRIZE_ID);
boolean lockAcquired = userLock.tryLock(LOCK_TIMEOUT, TimeUnit.MILLISECONDS);
if (!lockAcquired) {
log.warn("用户{}抽奖请求太频繁,获取锁失败", userId);
throw new ServiceException("您的请求太频繁,请稍后再试");
}
// 3. 处理抽奖规则(保底、最低中奖抽数、中奖率)
Prize winPrize = null;
boolean isGuarantee = false;
// 3.1 检查保底规则
for (Prize prize : prizes) {
if (prize.getGuaranteeDraws() > 0 && newContinuousDraws >= prize.getGuaranteeDraws()) {
// 触发保底,直接中该奖品
if (prizeService.deductStock(prize.getId())) {
winPrize = prize;
isGuarantee = true;
break;
}
try {
Account accountNew = accountService.getByUserId(user.getId());
if(accountNew.getPoints() < drawPoint){
throw new ServiceException("积分不足");
}
PrizeOnline winPrize = doDrawLogic(user,drawPoint);
return winPrize;
} finally {
userLock.unlock();
// 6. 清理未使用的锁(防止内存溢出)
cleanUnusedLock(userId);
}
// 3.2 未触发保底,按中奖率和最低中奖抽数规则抽奖
if (winPrize == null) {
winPrize = randomDraw(prizes, newContinuousDraws);
}
// 4. 处理中奖结果
Long prizeId = winPrize != null ? winPrize.getId() : THANKS_PRIZE_ID;
int finalContinuousDraws = winPrize != null ? 0 : newContinuousDraws; // 中奖后重置累计抽数
// 5. 保存抽奖记录
saveDrawRecord(userId, prizeId, finalContinuousDraws);
// 6. 更新用户累计抽数缓存
updateContinuousDrawsCache(userId, finalContinuousDraws);
return prizeService.getPrizeFromCache(prizeId);
} finally {
lock.unlock();
} catch (InterruptedException e) {
log.error("用户{}抽奖获取锁被中断", userId, e);
Thread.currentThread().interrupt(); // 恢复中断状态
throw new ServiceException("抽奖请求处理中,请稍后再试");
} catch (Exception e) {
log.error("用户{}抽奖失败", userId, e);
throw new ServiceException("抽奖失败,请重新刷新页面后在尝试");
}
}
/**
* 随机抽奖(处理中奖率和最低中奖抽数
* 实际抽奖逻辑(抽离出来,便于维护
*/
private Prize randomDraw(List<Prize> prizes, int continuousDraws) {
// 计算总中奖率
double totalProbability = 0.0;
for (Prize prize : prizes) {
// 检查最低中奖抽数规则未达到则该奖品中奖率为0
if (continuousDraws < prize.getMinWinDraws()) {
continue;
}
totalProbability += prize.getWinProbability();
private PrizeOnline doDrawLogic(User user,Integer drawPoint) {
Long userId = user.getId();
// 步骤1获取用户当前累计抽数缓存+数据库兜底)
int currentContinuousDraws = getContinuousDraws(userId);
int newContinuousDraws = currentContinuousDraws + 1;
log.info("用户{}当前累计抽数:{},本次抽数:{}", userId, currentContinuousDraws, newContinuousDraws);
// 步骤2获取有效奖品列表启用状态排除谢谢惠顾
List<PrizeOnline> validPrizes = prizeOnlineService.selectPrizeOnlineList(user.getGender());
if (validPrizes.isEmpty()) {
throw new ServiceException("无有效奖品,请刷新页面后再次抽奖");
}
if (totalProbability <= 0) {
// 所有奖品都未达到最低中奖抽数返回null谢谢惠顾
return null;
// 步骤3执行抽奖规则保底→最低抽数过滤→概率抽奖
PrizeOnline winPrize = null;
// 3.1 保底规则判断(优先触发
winPrize = checkGuaranteeRule(validPrizes, newContinuousDraws);
// 3.2 未触发保底,执行概率抽奖(含最低中奖抽数过滤)
if (winPrize == null) {
winPrize = executeProbabilityDraw(validPrizes, newContinuousDraws);
}
// 步骤4处理中奖结果确定最终奖品ID和累计抽数重置
if(winPrize == null){
winPrize = new PrizeOnline(); // TODO 谢谢惠顾
}
// 步骤5持久化抽奖记录+更新缓存
winPrizeAfter(winPrize, user, drawPoint, newContinuousDraws);
// 步骤6返回中奖奖品
return winPrize;
}
// 随机数0-总中奖率)
double random = new Random().nextDouble() * totalProbability;
double current = 0.0;
for (Prize prize : prizes) {
if (continuousDraws < prize.getMinWinDraws()) {
continue;
}
current += prize.getWinProbability();
if (random <= current) {
// 抽中该奖品,检查库存
if (prizeService.deductStock(prize.getId())) {
return prize;
} else {
// 库存不足,重新抽奖(递归)
return randomDraw(prizes, continuousDraws);
}
/**
* 保底规则判断
* @param validPrizes 有效奖品列表
* @param currentDraws 当前累计抽数
* @return 保底中奖的奖品无则返回null
*/
private PrizeOnline checkGuaranteeRule(List<PrizeOnline> validPrizes, int currentDraws) {
// 按保底抽数升序排序,优先触发保底抽数小的奖品
validPrizes.sort(Comparator.comparingInt(PrizeOnline::getGuaranteeDraws));
for (PrizeOnline prize : validPrizes) {
int guaranteeDraws = prize.getGuaranteeDraws();
if (guaranteeDraws > 0 && currentDraws >= guaranteeDraws) {
log.info("触发保底规则,用户抽中奖品:{}", prize.getPrizeName());
return prize;
}
}
return null;
}
/**
* 获取用户累计抽数(缓存+数据库
* 执行概率抽奖(含最低中奖抽数过滤
* @param validPrizes 有效奖品列表
* @param currentDraws 当前累计抽数
* @return 中奖奖品无则返回null
*/
private PrizeOnline executeProbabilityDraw(List<PrizeOnline> validPrizes, int currentDraws) {
// 步骤1过滤出满足最低中奖抽数的奖品
List<PrizeOnline> filterPrizes = filterByMinWinDraws(validPrizes, currentDraws);
if (filterPrizes.isEmpty()) {
log.info("无满足最低中奖抽数的奖品,返回谢谢惠顾");
return null;
}
// 步骤2计算奖品的概率总和放大为整数提升精度
double totalProbability = 0.0;
Map<PrizeOnline, Double> prizeProbMap = new LinkedHashMap<>(); // 保留顺序
for (PrizeOnline prize : filterPrizes) {
double prob = prize.getWinProbability().doubleValue();
if (prob < 0 || prob > 1) {
log.warn("奖品{}中奖率{}非法默认设为0", prize.getPrizeName(), prob);
prob = 0.0;
}
prizeProbMap.put(prize, prob);
totalProbability += prob;
}
// 步骤3若总概率为0直接返回null
if (totalProbability <= 0) {
log.info("满足条件的奖品总中奖率为0返回谢谢惠顾");
return null;
}
// 步骤4生成随机数放大10000倍转为整数计算减少浮点误差
int randomNum = new Random().nextInt((int) (totalProbability * RANDOM_MAX));
int currentNum = 0;
// 步骤5匹配中奖奖品
for (Map.Entry<PrizeOnline, Double> entry : prizeProbMap.entrySet()) {
PrizeOnline prize = entry.getKey();
double prob = entry.getValue();
int probInt = (int) (prob * RANDOM_MAX);
currentNum += probInt;
if (randomNum < currentNum) {
log.info("概率抽奖抽中奖品:{}", prize.getPrizeName());
return prize;
}
}
return null;
}
/**
* 过滤出满足最低中奖抽数的奖品
*/
private List<PrizeOnline> filterByMinWinDraws(List<PrizeOnline> prizes, int currentDraws) {
List<PrizeOnline> filterList = new ArrayList<>();
for (PrizeOnline prize : prizes) {
if (currentDraws >= prize.getMinWinDraws()) {
filterList.add(prize);
}
}
return filterList;
}
/**
* 获取用户累计抽数(缓存优先,数据库兜底)
*/
private int getContinuousDraws(Long userId) {
String key = String.format(USER_DRAW_COUNT_KEY, userId);
// 先从缓存获取
Integer count = (Integer) redisTemplate.opsForValue().get(key);
if (count != null) {
return count;
String cacheKey = String.format(USER_DRAW_COUNT_KEY, userId);
// 1. 从Redis缓存获取
RBucket<Integer> bucket = redissonClient.getBucket(cacheKey);
Integer cacheCount = bucket.get();
if (cacheCount != null) {
return cacheCount;
}
// 从数据库查询
count = userDrawRecordMapper.selectLastContinuousDraws(userId);
count = count == null ? 0 : count;
// 存入缓存
redisTemplate.opsForValue().set(key, count);
return count;
// 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;
}
/**
* 更新用户累计抽数缓存
*/
private void updateContinuousDrawsCache(Long userId, int count) {
String key = String.format(USER_DRAW_COUNT_KEY, userId);
redisTemplate.opsForValue().set(key, count);
}
/**
* 保存抽奖记录
* 保存抽奖记录(事务控制)
*/
@Transactional(rollbackFor = Exception.class)
private void saveDrawRecord(Long userId, Long prizeId, int continuousDraws) {
UserDrawRecord record = new UserDrawRecord();
record.setUserId(userId);
record.setPrizeId(prizeId);
record.setDrawTime(LocalDateTime.now());
record.setContinuousDraws(continuousDraws);
userDrawRecordMapper.insert(record);
public void winPrizeAfter(PrizeOnline prizeOnline, User user,Integer drawPoint, int continuousDraws) {
// 扣减积分
String traceId = IdManager.nextIdStr();
PointChangeLog pointChangeLog = accountService.drawPoint(prizeOnline, user, drawPoint, traceId);
// 记录用户抽奖记录
// UserDrawRecord record = new UserDrawRecord();
// record.setUserId(userId);
// record.setPrizeId(prizeId);
// record.setDrawTime(LocalDateTime.now());
// record.setContinuousDraws(continuousDraws);
// userDrawRecordMapper.insert(record);
// 更新缓存
String cacheKey = String.format(USER_DRAW_COUNT_KEY, user.getId());
RBucket<Integer> bucket = redissonClient.getBucket(cacheKey);
bucket.set(continuousDraws, USER_DRAW_COUNT_EXPIRE, java.util.concurrent.TimeUnit.SECONDS);
}
/**
* 清理长时间未使用的用户锁(可选,防止内存溢出)
* 这里简单实现:若锁未被持有,则移除(可根据业务增加时间判断)
*/
private void cleanUnusedLock(Long userId) {
ReentrantLock lock = userLockMap.get(userId);
if (lock != null && !lock.isLocked()) {
userLockMap.remove(userId);
log.debug("清理用户{}的锁", userId);
}
}
}