From 381d40d6995e49591de4d8ddb8c30c240b1217ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=AD=E5=87=AF=E5=87=AF?= Date: Mon, 13 Jan 2025 16:57:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=A5=96=E6=8E=A5=E5=8F=A3=E5=8A=A0?= =?UTF-8?q?=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DiscSpinningController.java | 159 ++++++++++-------- 1 file changed, 92 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/sqx/modules/discSpinning/controller/DiscSpinningController.java b/src/main/java/com/sqx/modules/discSpinning/controller/DiscSpinningController.java index 7c0de016..7f0d2a59 100644 --- a/src/main/java/com/sqx/modules/discSpinning/controller/DiscSpinningController.java +++ b/src/main/java/com/sqx/modules/discSpinning/controller/DiscSpinningController.java @@ -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 week = redisUtils.get(redisKey, Map.class); - for (Map.Entry 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 week = redisUtils.get(redisKey, Map.class); + for (Map.Entry 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("大转盘奖项领取")