From a57eb45222c6daf38d3e323598eeb56fd9d46726 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Sat, 21 Mar 2026 18:39:48 +0800 Subject: [PATCH] =?UTF-8?q?cp=E8=B5=9B=E5=AD=A3=E6=A6=9C=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activity/query/SeasonCpTop1QueryExe.java | 22 +++++++++++--- .../user/count/WeekCpValueCountService.java | 5 ++++ .../impl/WeekCpValueCountServiceImpl.java | 9 ++++++ .../circle/other/infra/utils/SeasonUtils.java | 29 +++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/SeasonCpTop1QueryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/SeasonCpTop1QueryExe.java index 167acb57..615c6110 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/SeasonCpTop1QueryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/SeasonCpTop1QueryExe.java @@ -8,10 +8,12 @@ import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.infra.common.user.UserCpUtils; import com.red.circle.other.infra.database.mongo.entity.user.count.WeekCpValueCount; import com.red.circle.other.infra.database.mongo.service.user.count.WeekCpValueCountService; +import com.red.circle.other.infra.utils.SeasonUtils; import com.red.circle.other.inner.model.dto.user.UserProfileDTO; import com.red.circle.other.inner.model.dto.user.WeekCpUserIdDTO; import com.red.circle.tool.core.collection.CollectionUtils; import com.red.circle.tool.core.num.NumUtils; +import java.time.LocalDate; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -29,19 +31,31 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class SeasonCpTop1QueryExe { + /** CP赛季功能上线日期,用于计算历史赛季范围 */ + private static final LocalDate SEASON_FEATURE_START_DATE = LocalDate.of(2026, 1, 1); + private final UserProfileGateway userProfileGateway; private final WeekCpValueCountService weekCpValueCountService; private final UserProfileAppConvertor userProfileAppConvertor; public List execute(AppExtCommand cmd) { - List lastSeasonTop = weekCpValueCountService - .listLastSeasonTop(cmd.getReqSysOrigin().getOrigin(), 1); + List historicalSeasonKeys = SeasonUtils.getAllHistoricalSeasonCycleKeys(SEASON_FEATURE_START_DATE); - if (CollectionUtils.isEmpty(lastSeasonTop)) { + if (CollectionUtils.isEmpty(historicalSeasonKeys)) { return Lists.newArrayList(); } - return assemble(lastSeasonTop); + // 从旧到新依次查询每个赛季的 Top1,过滤掉无数据的赛季 + List allSeasonTops = historicalSeasonKeys.stream() + .flatMap(cycleKey -> weekCpValueCountService + .listSeasonTopByCycleKey(cmd.getReqSysOrigin().getOrigin(), cycleKey, 1).stream()) + .collect(Collectors.toList()); + + if (CollectionUtils.isEmpty(allSeasonTops)) { + return Lists.newArrayList(); + } + + return assemble(allSeasonTops); } private List assemble(List weekCpValueCounts) { diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/WeekCpValueCountService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/WeekCpValueCountService.java index eed99daa..7da582c1 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/WeekCpValueCountService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/WeekCpValueCountService.java @@ -77,6 +77,11 @@ public interface WeekCpValueCountService { */ List listLastSeasonTop(String sysOrigin, Integer size); + /** + * 获取指定赛季cycleKey的统计记录top. + */ + List listSeasonTopByCycleKey(String sysOrigin, String cycleKey, Integer size); + /** * 解析id中cp用户id. */ diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/impl/WeekCpValueCountServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/impl/WeekCpValueCountServiceImpl.java index 6670924f..b13ae12d 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/impl/WeekCpValueCountServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/count/impl/WeekCpValueCountServiceImpl.java @@ -102,6 +102,15 @@ public class WeekCpValueCountServiceImpl implements WeekCpValueCountService { .limit(size), WeekCpValueCount.class); } + @Override + public List listSeasonTopByCycleKey(String sysOrigin, String cycleKey, Integer size) { + return mongoTemplate + .find(Query.query(Criteria.where("sysOrigin").is(sysOrigin) + .and("group").is(cycleKey)) + .with(Sort.by(Sort.Order.desc("quantity"))) + .limit(size), WeekCpValueCount.class); + } + private void addThisWeek(String sysOrigin, Long userIdOne, Long userIdTwo, Long quantity) { mongoTemplate.save(new WeekCpValueCount() .setId(getWeekUniqueId(userIdOne, userIdTwo)) diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/SeasonUtils.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/SeasonUtils.java index 33fb234e..7040d971 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/SeasonUtils.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/utils/SeasonUtils.java @@ -8,6 +8,7 @@ import lombok.Data; import java.time.LocalDate; import java.time.ZoneId;import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -44,6 +45,34 @@ public class SeasonUtils { return getSeasonCycleKey(now); } + /** + * 获取从指定日期到现在的所有历史赛季 cycleKey(不含当前赛季),从旧到新排列. + * @param startDate 起始日期(包含该日期所在赛季) + */ + public static List getAllHistoricalSeasonCycleKeys(LocalDate startDate) { + List cycleKeys = new ArrayList<>(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); + String currentSeasonKey = getCurrentSeasonCycleKey(); + ZonedDateTime current = startDate.atStartOfDay(ZonedId.ASIA_RIYADH.getZonedId()); + ZonedDateTime now = ZonedDateTimeAsiaRiyadhUtils.now(); + + while (!current.isAfter(now)) { + String cycleKey = getSeasonCycleKey(current); + // 当前赛季跳过(仍在进行中) + if (cycleKey.equals(currentSeasonKey)) { + break; + } + if (!cycleKeys.contains(cycleKey)) { + cycleKeys.add(cycleKey); + } + // 跳到本赛季结束日 +1 天,进入下一个赛季 + String endPart = cycleKey.split("-")[1]; + LocalDate endDate = LocalDate.parse(endPart, formatter); + current = endDate.plusDays(1).atStartOfDay(ZonedId.ASIA_RIYADH.getZonedId()); + } + return cycleKeys; + } + /** * 获取上一个赛季的cycleKey * 通过获取当前赛季开始日期前一天,来定位到上一个赛季