cp赛季榜历史接口修改

This commit is contained in:
tianfeng 2026-03-21 18:39:48 +08:00
parent b2619e1327
commit a57eb45222
4 changed files with 61 additions and 4 deletions

View File

@ -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<ActivityCpUserRankingCO> execute(AppExtCommand cmd) {
List<WeekCpValueCount> lastSeasonTop = weekCpValueCountService
.listLastSeasonTop(cmd.getReqSysOrigin().getOrigin(), 1);
List<String> historicalSeasonKeys = SeasonUtils.getAllHistoricalSeasonCycleKeys(SEASON_FEATURE_START_DATE);
if (CollectionUtils.isEmpty(lastSeasonTop)) {
if (CollectionUtils.isEmpty(historicalSeasonKeys)) {
return Lists.newArrayList();
}
return assemble(lastSeasonTop);
// 从旧到新依次查询每个赛季的 Top1过滤掉无数据的赛季
List<WeekCpValueCount> 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<ActivityCpUserRankingCO> assemble(List<WeekCpValueCount> weekCpValueCounts) {

View File

@ -77,6 +77,11 @@ public interface WeekCpValueCountService {
*/
List<WeekCpValueCount> listLastSeasonTop(String sysOrigin, Integer size);
/**
* 获取指定赛季cycleKey的统计记录top.
*/
List<WeekCpValueCount> listSeasonTopByCycleKey(String sysOrigin, String cycleKey, Integer size);
/**
* 解析id中cp用户id.
*/

View File

@ -102,6 +102,15 @@ public class WeekCpValueCountServiceImpl implements WeekCpValueCountService {
.limit(size), WeekCpValueCount.class);
}
@Override
public List<WeekCpValueCount> 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))

View File

@ -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<String> getAllHistoricalSeasonCycleKeys(LocalDate startDate) {
List<String> 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
* 通过获取当前赛季开始日期前一天来定位到上一个赛季