抽奖接口加锁
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package com.sqx.modules.discSpinning.controller;
|
package com.sqx.modules.discSpinning.controller;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import com.github.pagehelper.PageInfo;
|
import com.github.pagehelper.PageInfo;
|
||||||
import com.sqx.common.annotation.Debounce;
|
import com.sqx.common.annotation.Debounce;
|
||||||
import com.sqx.common.exception.CzgException;
|
import com.sqx.common.exception.CzgException;
|
||||||
|
import com.sqx.common.exception.SqxException;
|
||||||
import com.sqx.common.utils.*;
|
import com.sqx.common.utils.*;
|
||||||
import com.sqx.modules.app.annotation.Login;
|
import com.sqx.modules.app.annotation.Login;
|
||||||
import com.sqx.modules.app.service.UserInfoService;
|
import com.sqx.modules.app.service.UserInfoService;
|
||||||
@@ -54,7 +56,7 @@ public class DiscSpinningController {
|
|||||||
public DiscSpinningController(CommonInfoService commonRepository, DiscSpinningService discSpinningService,
|
public DiscSpinningController(CommonInfoService commonRepository, DiscSpinningService discSpinningService,
|
||||||
OrdersService ordersService, DiscSpinningRecordService recordService,
|
OrdersService ordersService, DiscSpinningRecordService recordService,
|
||||||
TaskCenterService taskCenterService, UserService userService, RedisUtils redisUtils,
|
TaskCenterService taskCenterService, UserService userService, RedisUtils redisUtils,
|
||||||
TaskCenterRecordService taskRecordService,UserInfoService userInfoService) {
|
TaskCenterRecordService taskRecordService, UserInfoService userInfoService) {
|
||||||
this.commonRepository = commonRepository;
|
this.commonRepository = commonRepository;
|
||||||
this.discSpinningService = discSpinningService;
|
this.discSpinningService = discSpinningService;
|
||||||
this.ordersService = ordersService;
|
this.ordersService = ordersService;
|
||||||
@@ -175,12 +177,23 @@ public class DiscSpinningController {
|
|||||||
@ApiOperation("抽取大转盘")
|
@ApiOperation("抽取大转盘")
|
||||||
@Debounce(value = "#userId")
|
@Debounce(value = "#userId")
|
||||||
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam(required = false, defaultValue = "1") Integer source) throws CzgException {
|
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam(required = false, defaultValue = "1") Integer source) throws CzgException {
|
||||||
|
String lockKey = StrUtil.format("user:disc-spinning:limit:user:lock:{}", userId);
|
||||||
|
if (redisUtils.hasKey(lockKey)) {
|
||||||
|
return Result.error("抽奖进行中,请等待!");
|
||||||
|
}
|
||||||
|
String intervalKey = StrUtil.format("user:disc-spinning:interval:limit:user:{}", userId);
|
||||||
|
if (redisUtils.hasKey(intervalKey)) {
|
||||||
|
return Result.error("抽奖请求过于频繁,请稍后再试!");
|
||||||
|
}
|
||||||
|
redisUtils.set(lockKey, true, 30);
|
||||||
|
Result ret = Result.error();
|
||||||
|
try {
|
||||||
double amount = 0;
|
double amount = 0;
|
||||||
Long sourceId = null;
|
Long sourceId = null;
|
||||||
Integer i = recordService.countDraw(userId);
|
Integer i = recordService.countDraw(userId);
|
||||||
if ((i != null && i.equals(2)) || !source.equals(1)) {
|
if ((i != null && i.equals(2)) || !source.equals(1)) {
|
||||||
boolean auth = userInfoService.isAuth(userId);
|
boolean auth = userInfoService.isAuth(userId);
|
||||||
if(!auth) {
|
if (!auth) {
|
||||||
return Result.error("剩余抽奖活动需要实名认证后进行");
|
return Result.error("剩余抽奖活动需要实名认证后进行");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -235,16 +248,28 @@ public class DiscSpinningController {
|
|||||||
if (source == 1 && sourceId == null) {
|
if (source == 1 && sourceId == null) {
|
||||||
throw new CzgException("异常请求");
|
throw new CzgException("异常请求");
|
||||||
}
|
}
|
||||||
if(sourceId == null){
|
if (sourceId == null) {
|
||||||
return Result.error("请求失败,请联系管理员");
|
return Result.error("请求失败,请联系管理员");
|
||||||
}
|
}
|
||||||
if (DataLimitUtil.isAllowed(RedisKeys.getDateKey("spinning:draw:") + sourceId, 120)) {
|
if (DataLimitUtil.isAllowed(RedisKeys.getDateKey("spinning:draw:") + sourceId, 120)) {
|
||||||
DiscSpinningRecord draws = discSpinningService.draws(i == null ? 1 : i + 1, amount, sourceId, userId, source);
|
DiscSpinningRecord draws = discSpinningService.draws(i == null ? 1 : i + 1, amount, sourceId, userId, source);
|
||||||
executor.schedule(() -> discSpinningService.receive1(draws), 4, TimeUnit.SECONDS);
|
executor.schedule(() -> discSpinningService.receive1(draws), 4, TimeUnit.SECONDS);
|
||||||
|
redisUtils.set(intervalKey, true, 5);
|
||||||
return new Result().put("data", draws);
|
return new Result().put("data", draws);
|
||||||
} else {
|
} else {
|
||||||
throw new CzgException("异常请求");
|
throw new CzgException("异常请求");
|
||||||
}
|
}
|
||||||
|
} catch (CzgException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (SqxException e) {
|
||||||
|
ret.put("msg", e.getMsg());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("抽奖异常", e);
|
||||||
|
ret.put("msg", "请求失败,请联系管理员");
|
||||||
|
} finally {
|
||||||
|
redisUtils.delete(lockKey);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("大转盘奖项领取")
|
@ApiOperation("大转盘奖项领取")
|
||||||
|
|||||||
Reference in New Issue
Block a user