diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/GameRankingInitTask.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/GameRankingInitTask.java new file mode 100644 index 00000000..a7c23088 --- /dev/null +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/GameRankingInitTask.java @@ -0,0 +1,68 @@ +package com.red.circle.other.app.scheduler; + +import com.red.circle.component.redis.annotation.TaskCacheLock; +import com.red.circle.other.domain.game.GameRankingIncrementCmd; +import com.red.circle.other.domain.game.GameRankingPeriodType; +import com.red.circle.other.domain.gateway.game.GameRankingGateway; +import com.red.circle.other.infra.database.rds.entity.sys.GameListConfig; +import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService; +import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 每周初始化游戏排行榜记录 + * + * @author tf + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class GameRankingInitTask { + + private final GameListConfigService gameListConfigService; + private final GameRankingGateway gameRankingGateway; + + /** + * 每周一 0点00分02秒执行. + */ + @Scheduled(cron = "2 0 0 ? * MON", zone = "Asia/Riyadh") + @TaskCacheLock(key = "GAME_RANKING_INIT_TASK", expireSecond = 86400) + public void initGameRanking() { + log.warn("========== 游戏排行榜初始化任务开始 =========="); + try { + List configs = gameListConfigService.query() + .eq(GameListConfig::getShowcase, Boolean.TRUE) + .list(); + if (configs.isEmpty()) { + log.warn("无上架游戏,跳过初始化"); + return; + } + String weekKey = ZonedDateTimeAsiaRiyadhUtils.nowWeekMondayToInt().toString(); + for (GameListConfig config : configs) { + gameRankingGateway.incrementPrizeAmount( + GameRankingIncrementCmd.builder() + .sysOrigin(config.getSysOrigin()) + .gameOrigin(config.getGameOrigin()) + .gameId(config.getId().toString()) + .gameName(config.getName()) + .gameCover(config.getCover()) + .gameUrl(config.getGameCode()) + .gameCreateTime(config.getCreateTime()) + .showcase(config.getShowcase()) + .periodType(GameRankingPeriodType.WEEK.getCode()) + .periodKey(weekKey) + .amount(0L) + .build() + ); + } + log.warn("========== 游戏排行榜初始化任务完成,共初始化 {} 个游戏 ==========", configs.size()); + } catch (Exception e) { + log.error("========== 游戏排行榜初始化任务异常 ==========", e); + } + } +}