From f23433b4d7615dd940ecb7d6e6acb03accdd14cb Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Tue, 21 Oct 2025 12:31:08 +0800 Subject: [PATCH] =?UTF-8?q?spins=20task=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 任务已完成 无需增加进度值 领取礼物修改 --- .../app/task/SpinsTaskRestController.java | 2 +- .../SpinsTaskProgressIncrementExe.java | 13 ++- .../command/SpinsTaskProgressUpdateExe.java | 13 ++- .../command/SpinsTaskReceiveRewardExe.java | 7 +- .../gift/strategy/GiftCountStrategy.java | 96 +++++++++++++++++++ .../other/app/listener/task/TaskListener.java | 67 ++++++++++++- .../service/task/SpinsTaskServiceImpl.java | 2 +- .../dto/cmd/SpinsTaskProgressUpdateCmd.java | 2 + .../dto/cmd/SpinsTaskReceiveRewardCmd.java | 12 +-- 9 files changed, 193 insertions(+), 21 deletions(-) diff --git a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/task/SpinsTaskRestController.java b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/task/SpinsTaskRestController.java index 754b7b44..f2ef7756 100644 --- a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/task/SpinsTaskRestController.java +++ b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/task/SpinsTaskRestController.java @@ -48,7 +48,7 @@ public class SpinsTaskRestController { */ @PostMapping("/receive/reward") public SpinsTaskRewardCO receiveReward(@Validated @RequestBody SpinsTaskReceiveRewardCmd cmd) { - log.info("领取任务奖励, userId={}, taskCode={}", cmd.getUserId(), cmd.getTaskCode()); + log.info("领取任务奖励, userId={}, taskCode={}", cmd.getReqUserId(), cmd.getTaskCode()); SpinsTaskRewardCO reward = spinsTaskService.receiveTaskReward(cmd); return reward; } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressIncrementExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressIncrementExe.java index ca75e4bd..0e5a383b 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressIncrementExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressIncrementExe.java @@ -59,15 +59,22 @@ public class SpinsTaskProgressIncrementExe { String cycleKey = getCurrentCycleKey(taskConfig.getCycleType()); SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey); - // 3. 增加进度值 + // 3. 校验任务状态,如果已完成或已领取,则不更新 + if (progress.getTaskStatus() != null && progress.getTaskStatus() >= 1) { + log.info("任务已完成,无需增加进度, userId={}, taskCode={}, taskStatus={}", + userId, taskCode, progress.getTaskStatus()); + return; + } + + // 4. 增加进度值 int newValue = progress.getCurrentValue() + incrementValue; progress.setCurrentValue(newValue); - // 4. 计算进度百分比 + // 5. 计算进度百分比 BigDecimal progressRate = calculateProgressRate(newValue, taskConfig.getTargetValue()); progress.setProgressRate(progressRate); - // 5. 检查是否完成 + // 6. 检查是否完成 if (newValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) { progress.setTaskStatus(1); progress.setCompleteTime(new Timestamp(System.currentTimeMillis())); diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressUpdateExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressUpdateExe.java index 7a103e64..1d2d700a 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressUpdateExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskProgressUpdateExe.java @@ -61,14 +61,21 @@ public class SpinsTaskProgressUpdateExe { String cycleKey = getCurrentCycleKey(taskConfig.getCycleType()); SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey); - // 3. 更新进度值 + // 3. 校验任务状态,如果已完成或已领取,则不更新 + if (progress.getTaskStatus() != null && progress.getTaskStatus() >= 1) { + log.info("任务已完成,无需更新进度, userId={}, taskCode={}, taskStatus={}", + userId, taskCode, progress.getTaskStatus()); + return; + } + + // 4. 更新进度值 progress.setCurrentValue(progressValue); - // 4. 计算进度百分比 + // 5. 计算进度百分比 BigDecimal progressRate = calculateProgressRate(progressValue, taskConfig.getTargetValue()); progress.setProgressRate(progressRate); - // 5. 检查是否完成 + // 6. 检查是否完成 if (progressValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) { progress.setTaskStatus(1); progress.setCompleteTime(new Timestamp(System.currentTimeMillis())); diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskReceiveRewardExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskReceiveRewardExe.java index 3404cad6..25d75729 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskReceiveRewardExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/SpinsTaskReceiveRewardExe.java @@ -45,7 +45,7 @@ public class SpinsTaskReceiveRewardExe { */ @Transactional(rollbackFor = Exception.class) public SpinsTaskRewardCO execute(SpinsTaskReceiveRewardCmd cmd) { - Long userId = cmd.getUserId(); + Long userId = cmd.getReqUserId(); String taskCode = cmd.getTaskCode(); // 1. 查询任务配置 @@ -62,16 +62,17 @@ public class SpinsTaskReceiveRewardExe { // 2. 查询用户任务进度 String cycleKey = getCurrentCycleKey(); - SpinsUserTaskProgress progress = spinsUserTaskProgressDAO.selectById( + List progressList = spinsUserTaskProgressDAO.selectList( new LambdaQueryWrapper() .eq(SpinsUserTaskProgress::getUserId, userId) .eq(SpinsUserTaskProgress::getTaskCode, taskCode) .eq(SpinsUserTaskProgress::getCycleKey, cycleKey) ); - if (progress == null) { + if (progressList.isEmpty()) { throw new RuntimeException("任务未开始"); } + SpinsUserTaskProgress progress = progressList.get(0); // 3. 校验任务状态 if (progress.getTaskStatus() == 0) { diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/GiftCountStrategy.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/GiftCountStrategy.java index a5ab2e8a..1bd13515 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/GiftCountStrategy.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/GiftCountStrategy.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.google.common.collect.Lists; import com.red.circle.common.business.core.enums.GameStateEnum; import com.red.circle.common.business.core.enums.SysOriginPlatformEnum; +import com.red.circle.component.redis.service.RedisService; import com.red.circle.external.inner.endpoint.message.OfficialNoticeClient; import com.red.circle.external.inner.model.cmd.message.notice.official.NoticeExtTemplateTypeCmd; import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum; @@ -14,6 +15,8 @@ import com.red.circle.mq.rocket.business.producer.UserMqMessageService; import com.red.circle.other.app.common.game.GameIndoorTeamPkCommon; import com.red.circle.other.app.common.gift.GameLuckyGiftCommon; import com.red.circle.other.app.dto.clientobject.game.GameRoomPkUserCO; +import com.red.circle.other.app.dto.cmd.SpinsTaskProgressUpdateCmd; +import com.red.circle.other.app.service.SpinsUserTaskProgressService; import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway; import com.red.circle.other.domain.model.user.UserProfile; @@ -77,6 +80,7 @@ import java.math.RoundingMode; import java.sql.Timestamp; import java.time.ZonedDateTime; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -124,6 +128,8 @@ public class GiftCountStrategy implements GiftStrategy { private final UserProfileGateway userProfileRepository; private final OfficialNoticeClient officialService; private final TaskMqMessage taskMqMessage; + private final SpinsUserTaskProgressService spinsUserTaskProgressService; + private final RedisService redisService; private static final List inRegionCodes = List.of("IN", "PK", "BD"); private final UserRegionCacheService userRegionCacheService; @@ -260,6 +266,9 @@ public class GiftCountStrategy implements GiftStrategy { // 累计活动 // consumeActivity(runningWater, isLuckyGift, luckyGiftRatio); + // Spins 送礼物任务 + handleSpinsGiftTask(runningWater); + giftGiveRunningWaterService.addLog(runningWater.getId(), "结束:礼物统计相关"); } @@ -725,7 +734,94 @@ public class GiftCountStrategy implements GiftStrategy { return amount.multiply(luckyGiftRatio).setScale(0, RoundingMode.DOWN).longValue(); } return amount.longValue(); + } + /** + * 处理 Spins 送礼物任务 + */ + private void handleSpinsGiftTask(GiftGiveRunningWater runningWater) { + try { + Long userId = runningWater.getUserId(); + + // 增加用户每日送礼物数量 + int giftCount = incrementDailyGiftCount(userId); + + log.info("Spins送礼物任务, userId={}, giftCount={}", userId, giftCount); + + // 处理送礁1个礼物任务 + if (giftCount >= 1) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_SEND_GIFT_1") + .setProgressValue(giftCount) + ); + } + + // 处理送出3个礼物任务 + if (giftCount >= 3) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_SEND_GIFT_3") + .setProgressValue(giftCount) + ); + } + + // 处理送出10个礼物任务 + if (giftCount >= 10) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_SEND_GIFT_10") + .setProgressValue(giftCount) + ); + } + + // 处理送出15个礼物任务 + if (giftCount >= 15) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_SEND_GIFT_15") + .setProgressValue(giftCount) + ); + } + + log.info("Spins送礼物任务处理成功, userId={}, giftCount={}", userId, giftCount); + } catch (Exception e) { + log.error("处理Spins送礼物任务失败", e); + } + } + + /** + * 增加用户每日送礼物数量 + * @param userId 用户ID + * @return 当前送礼物数量 + */ + private int incrementDailyGiftCount(Long userId) { + String redisKey = "spins:daily:gift:count:" + userId; + String countStr = redisService.getString(redisKey); + + if (StringUtils.isBlank(countStr)) { + // 计算到凌晨的过期时间(秒) + long expireSeconds = getSecondsUntilMidnight(); + redisService.setString(redisKey, "1", expireSeconds, TimeUnit.SECONDS); + return 1; + } else { + Long newCount = redisService.increment(redisKey, 1); + return newCount.intValue(); + } + } + + /** + * 获取当前时间到凌晨的秒数 + * @return 秒数 + */ + private long getSecondsUntilMidnight() { + ZonedDateTime now = ZonedDateTimeAsiaRiyadhUtils.now(); + ZonedDateTime midnight = now.toLocalDate().plusDays(1).atStartOfDay(now.getZone()); + return java.time.Duration.between(now, midnight).getSeconds(); } } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java index e36af160..8d435422 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java @@ -18,6 +18,8 @@ import com.red.circle.external.inner.model.enums.message.GroupMessageTypeEnum; import com.red.circle.mq.business.model.event.task.TaskApprovalEvent; import com.red.circle.mq.rocket.business.streams.TaskSink; import com.red.circle.other.app.dto.clientobject.game.GameRoomMessageCO; +import com.red.circle.other.app.dto.cmd.SpinsTaskProgressUpdateCmd; +import com.red.circle.other.app.service.SpinsUserTaskProgressService; import com.red.circle.other.app.service.room.RoomProfileService; import com.red.circle.other.app.service.task.TaskService; import com.red.circle.other.domain.gateway.user.UserProfileGateway; @@ -84,6 +86,7 @@ public class TaskListener implements MessageListener { private final ImGroupClient imGroupClient; private final GameListConfigService gameListConfigService; private final InviteUserGateway inviteUserGateway; + private final SpinsUserTaskProgressService spinsUserTaskProgressService; @Override public Action consume(ConsumerMessage message) { @@ -184,11 +187,71 @@ public class TaskListener implements MessageListener { if ("10".equals(inc) || Integer.parseInt(inc) >= 10) { // 更新任务状态的逻辑 直接更新 updateTaskStatus(eventBody); - } else { - redisService.increment(redisKey, 1); } + redisService.increment(redisKey, 1); } + // 处理 Spins 上麦任务进度 + handleSpinsMicTask(eventBody.getUserId(), inc); + } + + /** + * 处理 Spins 上麦任务进度 + * @param userId 用户ID + * @param micTimeStr 上麦时长(分钟)字符串 + */ + private void handleSpinsMicTask(Long userId, String micTimeStr) { + if (StringUtils.isEmpty(micTimeStr)) { + return; + } + + try { + int micTime = Integer.parseInt(micTimeStr); + + // 处理上麦15分钟任务 + if (micTime >= 15) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_MIC_15_MIN") + .setProgressValue(micTime) + ); + } + + // 处理上麦30分钟任务 + if (micTime >= 30) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_MIC_30_MIN") + .setProgressValue(micTime) + ); + } + + // 处理上麦1小时任务 + if (micTime >= 60) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_MIC_1_HOUR") + .setProgressValue(micTime) + ); + } + + // 处理上麦2小时任务 + if (micTime >= 120) { + spinsUserTaskProgressService.updateTaskProgress( + new SpinsTaskProgressUpdateCmd() + .setUserId(userId) + .setTaskCode("SPINS_MIC_2_HOUR") + .setProgressValue(micTime) + ); + } + + log.info("Spins上麦任务进度更新成功, userId={}, micTime={}", userId, micTime); + } catch (Exception e) { + log.error("处理Spins上麦任务进度失败, userId={}, micTimeStr={}", userId, micTimeStr, e); + } } private void handleTask99(TaskApprovalEvent eventBody) { diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/task/SpinsTaskServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/task/SpinsTaskServiceImpl.java index 635621da..d10c88a5 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/task/SpinsTaskServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/task/SpinsTaskServiceImpl.java @@ -36,7 +36,7 @@ public class SpinsTaskServiceImpl implements SpinsTaskService { @Override public SpinsTaskRewardCO receiveTaskReward(SpinsTaskReceiveRewardCmd cmd) { - log.info("执行领取任务奖励, userId={}, taskCode={}", cmd.getUserId(), cmd.getTaskCode()); + log.info("执行领取任务奖励, userId={}, taskCode={}", cmd.getReqUserId(), cmd.getTaskCode()); return spinsTaskReceiveRewardExe.execute(cmd); } } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskProgressUpdateCmd.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskProgressUpdateCmd.java index 5dcb8e70..0572529f 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskProgressUpdateCmd.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskProgressUpdateCmd.java @@ -3,6 +3,7 @@ package com.red.circle.other.app.dto.cmd; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Data; +import lombok.experimental.Accessors; import java.io.Serializable; @@ -13,6 +14,7 @@ import java.io.Serializable; * @date 2025-01-21 */ @Data +@Accessors(chain = true) public class SpinsTaskProgressUpdateCmd implements Serializable { private static final long serialVersionUID = 1L; diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskReceiveRewardCmd.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskReceiveRewardCmd.java index 6f6d10b3..cec273cf 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskReceiveRewardCmd.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/SpinsTaskReceiveRewardCmd.java @@ -1,10 +1,10 @@ package com.red.circle.other.app.dto.cmd; +import com.red.circle.common.business.dto.cmd.AppExtCommand; import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotNull; import lombok.Data; +import lombok.EqualsAndHashCode; -import java.io.Serializable; /** * 领取任务奖励命令 @@ -12,13 +12,9 @@ import java.io.Serializable; * @author system * @date 2025-01-21 */ +@EqualsAndHashCode(callSuper = true) @Data -public class SpinsTaskReceiveRewardCmd implements Serializable { - - private static final long serialVersionUID = 1L; - - @NotNull(message = "用户ID不能为空") - private Long userId; +public class SpinsTaskReceiveRewardCmd extends AppExtCommand { @NotBlank(message = "任务编码不能为空") private String taskCode;