抽奖接口加锁
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package com.sqx.modules.discSpinning.controller;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.sqx.common.annotation.Debounce;
|
||||
import com.sqx.common.exception.CzgException;
|
||||
import com.sqx.common.exception.SqxException;
|
||||
import com.sqx.common.utils.*;
|
||||
import com.sqx.modules.app.annotation.Login;
|
||||
import com.sqx.modules.app.service.UserInfoService;
|
||||
@@ -54,7 +56,7 @@ public class DiscSpinningController {
|
||||
public DiscSpinningController(CommonInfoService commonRepository, DiscSpinningService discSpinningService,
|
||||
OrdersService ordersService, DiscSpinningRecordService recordService,
|
||||
TaskCenterService taskCenterService, UserService userService, RedisUtils redisUtils,
|
||||
TaskCenterRecordService taskRecordService,UserInfoService userInfoService) {
|
||||
TaskCenterRecordService taskRecordService, UserInfoService userInfoService) {
|
||||
this.commonRepository = commonRepository;
|
||||
this.discSpinningService = discSpinningService;
|
||||
this.ordersService = ordersService;
|
||||
@@ -175,76 +177,99 @@ public class DiscSpinningController {
|
||||
@ApiOperation("抽取大转盘")
|
||||
@Debounce(value = "#userId")
|
||||
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam(required = false, defaultValue = "1") Integer source) throws CzgException {
|
||||
double amount = 0;
|
||||
Long sourceId = null;
|
||||
Integer i = recordService.countDraw(userId);
|
||||
if ((i != null && i.equals(2)) || !source.equals(1)) {
|
||||
boolean auth = userInfoService.isAuth(userId);
|
||||
if(!auth) {
|
||||
return Result.error("剩余抽奖活动需要实名认证后进行");
|
||||
}
|
||||
String lockKey = StrUtil.format("user:disc-spinning:limit:user:lock:{}", userId);
|
||||
if (redisUtils.hasKey(lockKey)) {
|
||||
return Result.error("抽奖进行中,请等待!");
|
||||
}
|
||||
if (source == null || source.equals(1)) {
|
||||
source = 1;
|
||||
//订单抽奖
|
||||
int drawCount = Integer.parseInt(commonRepository.findOne(901).getValue());
|
||||
if (i != null && i >= drawCount) {
|
||||
throw new CzgException("当日可抽奖次数已超限");
|
||||
}
|
||||
Orders orders = ordersService.selectOrdersByDay(userId);
|
||||
if (orders == null) {
|
||||
throw new CzgException("无可抽奖机会");
|
||||
}
|
||||
amount = orders.getPayMoney().doubleValue();
|
||||
sourceId = orders.getOrdersId();
|
||||
} else {
|
||||
String redisKey = "";
|
||||
if (source.equals(2)) {
|
||||
redisKey = RedisKeys.getDateKey("spinning:amount:taskW") + userId;
|
||||
} else if (source.equals(3)) {
|
||||
redisKey = RedisKeys.getDateKey("spinning:amount:taskM") + userId;
|
||||
}
|
||||
Map<String, Object> week = redisUtils.get(redisKey, Map.class);
|
||||
for (Map.Entry<String, Object> entry : week.entrySet()) {
|
||||
int value = new BigDecimal(entry.getValue().toString()).intValue();
|
||||
if (value > 1) {
|
||||
value = value - 1;
|
||||
week.put(entry.getKey(), value);
|
||||
sourceId = Long.valueOf(entry.getKey());
|
||||
break;
|
||||
} else {
|
||||
TaskCenterRecord centerRecord = new TaskCenterRecord();
|
||||
centerRecord.setUserId(userId);
|
||||
centerRecord.setTaskId(Long.valueOf(entry.getKey()));
|
||||
if (source.equals(2)) {
|
||||
sourceId = Long.valueOf(entry.getKey());
|
||||
centerRecord.setSourceId(sourceId);
|
||||
}
|
||||
centerRecord.setName(source.equals(2) ? "周任务奖励" : "月任务奖励");
|
||||
centerRecord.setType(9);
|
||||
centerRecord.setNumber(1);
|
||||
centerRecord.setCreateTime(DateUtil.now());
|
||||
centerRecord.setUpdateTime(DateUtil.now());
|
||||
taskRecordService.save(centerRecord);
|
||||
week.remove(entry.getKey());
|
||||
break;
|
||||
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;
|
||||
Long sourceId = null;
|
||||
Integer i = recordService.countDraw(userId);
|
||||
if ((i != null && i.equals(2)) || !source.equals(1)) {
|
||||
boolean auth = userInfoService.isAuth(userId);
|
||||
if (!auth) {
|
||||
return Result.error("剩余抽奖活动需要实名认证后进行");
|
||||
}
|
||||
}
|
||||
redisUtils.set(redisKey, week, DateUtils.todayAfterSecond());
|
||||
}
|
||||
if (source == 1 && sourceId == null) {
|
||||
throw new CzgException("异常请求");
|
||||
}
|
||||
if(sourceId == null){
|
||||
return Result.error("请求失败,请联系管理员");
|
||||
}
|
||||
if (DataLimitUtil.isAllowed(RedisKeys.getDateKey("spinning:draw:") + sourceId, 120)) {
|
||||
DiscSpinningRecord draws = discSpinningService.draws(i == null ? 1 : i + 1, amount, sourceId, userId, source);
|
||||
executor.schedule(() -> discSpinningService.receive1(draws), 4, TimeUnit.SECONDS);
|
||||
return new Result().put("data", draws);
|
||||
} else {
|
||||
throw new CzgException("异常请求");
|
||||
if (source == null || source.equals(1)) {
|
||||
source = 1;
|
||||
//订单抽奖
|
||||
int drawCount = Integer.parseInt(commonRepository.findOne(901).getValue());
|
||||
if (i != null && i >= drawCount) {
|
||||
throw new CzgException("当日可抽奖次数已超限");
|
||||
}
|
||||
Orders orders = ordersService.selectOrdersByDay(userId);
|
||||
if (orders == null) {
|
||||
throw new CzgException("无可抽奖机会");
|
||||
}
|
||||
amount = orders.getPayMoney().doubleValue();
|
||||
sourceId = orders.getOrdersId();
|
||||
} else {
|
||||
String redisKey = "";
|
||||
if (source.equals(2)) {
|
||||
redisKey = RedisKeys.getDateKey("spinning:amount:taskW") + userId;
|
||||
} else if (source.equals(3)) {
|
||||
redisKey = RedisKeys.getDateKey("spinning:amount:taskM") + userId;
|
||||
}
|
||||
Map<String, Object> week = redisUtils.get(redisKey, Map.class);
|
||||
for (Map.Entry<String, Object> entry : week.entrySet()) {
|
||||
int value = new BigDecimal(entry.getValue().toString()).intValue();
|
||||
if (value > 1) {
|
||||
value = value - 1;
|
||||
week.put(entry.getKey(), value);
|
||||
sourceId = Long.valueOf(entry.getKey());
|
||||
break;
|
||||
} else {
|
||||
TaskCenterRecord centerRecord = new TaskCenterRecord();
|
||||
centerRecord.setUserId(userId);
|
||||
centerRecord.setTaskId(Long.valueOf(entry.getKey()));
|
||||
if (source.equals(2)) {
|
||||
sourceId = Long.valueOf(entry.getKey());
|
||||
centerRecord.setSourceId(sourceId);
|
||||
}
|
||||
centerRecord.setName(source.equals(2) ? "周任务奖励" : "月任务奖励");
|
||||
centerRecord.setType(9);
|
||||
centerRecord.setNumber(1);
|
||||
centerRecord.setCreateTime(DateUtil.now());
|
||||
centerRecord.setUpdateTime(DateUtil.now());
|
||||
taskRecordService.save(centerRecord);
|
||||
week.remove(entry.getKey());
|
||||
break;
|
||||
}
|
||||
}
|
||||
redisUtils.set(redisKey, week, DateUtils.todayAfterSecond());
|
||||
}
|
||||
if (source == 1 && sourceId == null) {
|
||||
throw new CzgException("异常请求");
|
||||
}
|
||||
if (sourceId == null) {
|
||||
return Result.error("请求失败,请联系管理员");
|
||||
}
|
||||
if (DataLimitUtil.isAllowed(RedisKeys.getDateKey("spinning:draw:") + sourceId, 120)) {
|
||||
DiscSpinningRecord draws = discSpinningService.draws(i == null ? 1 : i + 1, amount, sourceId, userId, source);
|
||||
executor.schedule(() -> discSpinningService.receive1(draws), 4, TimeUnit.SECONDS);
|
||||
redisUtils.set(intervalKey, true, 5);
|
||||
return new Result().put("data", draws);
|
||||
} else {
|
||||
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("大转盘奖项领取")
|
||||
|
||||
Reference in New Issue
Block a user