From 5820d7fc746b384b6d1e6ce388b760ebb8ffdff8 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Mon, 29 Dec 2025 00:01:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EupdatestartPageuser=20?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduler/UpdateStartPageUserTask.java | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/UpdateStartPageUserTask.java diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/UpdateStartPageUserTask.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/UpdateStartPageUserTask.java new file mode 100644 index 00000000..2b1967c7 --- /dev/null +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/UpdateStartPageUserTask.java @@ -0,0 +1,144 @@ +package com.red.circle.other.app.scheduler; + +import com.red.circle.common.business.core.enums.SysOriginPlatformEnum; +import com.red.circle.component.redis.annotation.TaskCacheLock; +import com.red.circle.other.domain.ranking.RankingActivityType; +import com.red.circle.other.domain.ranking.RankingCycleType; +import com.red.circle.other.infra.database.mongo.entity.activity.RankingActivityRecord; +import com.red.circle.other.infra.database.rds.entity.sys.StartPageUser; +import com.red.circle.other.infra.database.rds.service.sys.StartPageUserService; +import com.red.circle.other.infra.gateway.RankingActivityGateway; +import com.red.circle.tool.core.collection.CollectionUtils; +import com.red.circle.tool.core.date.DateFormatConstant; +import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils; +import com.red.circle.tool.core.date.ZonedDateTimeUtils; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.sql.Timestamp; +import java.time.ZonedDateTime; +import java.util.List; + +/** + * 更新启动页用户定时任务 + * 每周一查询上周排名第一的用户并更新到启动页 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class UpdateStartPageUserTask { + + private final RankingActivityGateway rankingActivityGateway; + private final StartPageUserService startPageUserService; + + private static final String START_PAGE_TYPE = "KING_GAMES"; + private static final Integer EXPIRE_DAYS = 7; + + /** + * 每周一 0点5分执行 + */ + @Scheduled(cron = "0 5 0 ? * MON", zone = "Asia/Riyadh") + @TaskCacheLock(key = "UPDATE_START_PAGE_USER_TASK", expireSecond = 86400) + @Transactional(rollbackFor = Exception.class) + public void updateStartPageUser() { + long startTime = System.currentTimeMillis(); + log.warn("========== 更新启动页用户定时任务开始 =========="); + + try { + String sysOrigin = SysOriginPlatformEnum.LIKEI.name(); + String lastWeekDate = getLastWeekDate(); + + Long topUserId = getLastWeekTopUser(sysOrigin, lastWeekDate); + + if (topUserId == null) { + log.warn("未找到上周排名第一的用户,跳过更新"); + return; + } + + updateOrCreateStartPageUser(sysOrigin, topUserId); + + log.warn("========== 更新启动页用户定时任务完成,耗时:{} ms,用户ID:{} ==========", + System.currentTimeMillis() - startTime, topUserId); + + } catch (Exception e) { + log.error("========== 更新启动页用户定时任务异常 ==========", e); + throw e; + } + } + + /** + * 获取上周排名第一的用户 + */ + private Long getLastWeekTopUser(String sysOrigin, String lastWeekDate) { + List records = rankingActivityGateway.getRankingList( + sysOrigin, + RankingActivityType.KING_GAMES, + RankingCycleType.WEEKLY, + lastWeekDate, + null, + 1 + ); + + if (CollectionUtils.isEmpty(records)) { + log.warn("上周排行榜为空,日期:{}", lastWeekDate); + return null; + } + + RankingActivityRecord topRecord = records.get(0); + log.info("上周排名第一用户ID:{},数量:{}", topRecord.getUserId(), topRecord.getQuantity()); + + return topRecord.getUserId(); + } + + /** + * 更新或创建启动页用户记录 + */ + private void updateOrCreateStartPageUser(String sysOrigin, Long userId) { + StartPageUser existingUser = startPageUserService.query() + .eq(StartPageUser::getSysOrigin, sysOrigin) + .eq(StartPageUser::getType, START_PAGE_TYPE) + .last("LIMIT 1") + .getOne(); + + Timestamp expireTime = calculateExpireTime(); + + if (existingUser != null) { + existingUser.setUserId(userId); + existingUser.setExpireTime(expireTime); + startPageUserService.updateSelectiveById(existingUser); + log.info("更新启动页用户成功,ID:{},用户ID:{},过期时间:{}", + existingUser.getId(), userId, expireTime); + } else { + StartPageUser newUser = new StartPageUser(); + newUser.setSysOrigin(sysOrigin); + newUser.setType(START_PAGE_TYPE); + newUser.setUserId(userId); + newUser.setSort(1); + newUser.setExpireTime(expireTime); + startPageUserService.save(newUser); + log.info("创建启动页用户成功,用户ID:{},过期时间:{}", userId, expireTime); + } + } + + /** + * 计算过期时间(当前时间 + 7天) + */ + private Timestamp calculateExpireTime() { + ZonedDateTime now = ZonedDateTimeAsiaRiyadhUtils.now(); + ZonedDateTime expireDateTime = now.plusDays(EXPIRE_DAYS); + return Timestamp.from(expireDateTime.toInstant()); + } + + /** + * 获取上周一的日期(格式:yyyyMMdd) + */ + private String getLastWeekDate() { + return ZonedDateTimeUtils.format( + ZonedDateTimeAsiaRiyadhUtils.getLastWeekMonday(), + DateFormatConstant.yyyyMMdd + ); + } +}