From 3f14fa244e7dfe6a30d10d5e28b7e01645e0f9d9 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 12 Sep 2025 11:54:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A6=9C=E5=8D=95=E5=AE=8C=E5=96=84=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=92=E5=90=8D=E5=92=8C=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../room/WeekKingQueenController.java | 4 +- .../query/WeekKingQueenRankingQueryExe.java | 35 +++++++++-- .../activity/WeekKingQueenServiceImpl.java | 4 +- .../dto/clientobject/activity/UserRankCO.java | 10 +++ .../WeekKingQueenRankingQueryCmd.java | 5 +- .../activity/WeekKingQueenService.java | 3 +- .../entity/activity/WeekKingQueenCount.java | 7 +++ .../activity/WeekKingQueenCountService.java | 9 +++ .../impl/WeekKingQueenCountServiceImpl.java | 61 ++++++++++++++++++- 9 files changed, 127 insertions(+), 11 deletions(-) diff --git a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekKingQueenController.java b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekKingQueenController.java index 96015d33..425d962f 100644 --- a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekKingQueenController.java +++ b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekKingQueenController.java @@ -1,6 +1,7 @@ package com.red.circle.other.adapter.app.activity.room; import com.red.circle.common.business.dto.cmd.AppExtCommand; +import com.red.circle.framework.dto.PageResult; import com.red.circle.framework.web.controller.BaseController; import com.red.circle.other.app.dto.clientobject.activity.UserCharmWealthContributeCO; import com.red.circle.other.app.dto.clientobject.activity.UserRankCO; @@ -8,6 +9,7 @@ import com.red.circle.other.app.dto.cmd.activity.WeekKingQueenRankingQueryCmd; import com.red.circle.other.app.service.activity.WeekKingQueenService; import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -37,7 +39,7 @@ public class WeekKingQueenController extends BaseController { * @eo.request-type formdata */ @GetMapping("/ranking") - public List listRanking(@Validated WeekKingQueenRankingQueryCmd cmd) { + public PageResult listRanking(@Validated WeekKingQueenRankingQueryCmd cmd) { return weekKingQueenService.listRanking(cmd); } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekKingQueenRankingQueryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekKingQueenRankingQueryExe.java index 16ede92a..acd7a0f0 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekKingQueenRankingQueryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekKingQueenRankingQueryExe.java @@ -1,6 +1,7 @@ package com.red.circle.other.app.command.activity.query; import com.red.circle.common.business.enums.WeekLeaderboardEnum; +import com.red.circle.framework.dto.PageResult; import com.red.circle.other.app.convertor.user.UserProfileAppConvertor; import com.red.circle.other.app.dto.clientobject.activity.UserRankCO; import com.red.circle.other.app.dto.cmd.activity.WeekKingQueenRankingQueryCmd; @@ -17,6 +18,10 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; /** @@ -32,27 +37,44 @@ public class WeekKingQueenRankingQueryExe { private final UserProfileAppConvertor userProfileAppConvertor; private final WeekKingQueenCountService weekKingQueenCountService; - public List execute(WeekKingQueenRankingQueryCmd cmd) { - List weekKingQueens = listLeaderboard(cmd); + public PageResult execute(WeekKingQueenRankingQueryCmd cmd) { + Page kingQueenCountPage = pageWeekTop(cmd.getReqSysOrigin().getOrigin(), + KingQueenType.valueOf(cmd.getKingQueenType()), + Objects.equals(cmd.getType(), WeekLeaderboardEnum.THIS_WEEK) ? 0 : 1, + PageRequest.of(cmd.getCurrent().intValue() - 1, cmd.getSize().intValue())); + + List weekKingQueens = kingQueenCountPage.getContent(); if (CollectionUtils.isEmpty(weekKingQueens)) { - return CollectionUtils.newArrayList(); + return null; } Map userProfileMap = userProfileAppConvertor.toMapUserProfileDTO( userProfileGateway .mapByUserIds(getUserIds(weekKingQueens))); - return weekKingQueens.stream() + List userRankList = weekKingQueens.stream() .map(item -> { UserProfileDTO userProfile = userProfileMap.get(item.getUserId()); return new UserRankCO() .setUserId(userProfile.getId()) + .setAccount(userProfile.getAccount()) .setUserAvatar(userProfile.getUserAvatar()) .setUserNickname(userProfile.getUserNickname()) .setQuantity(NumUtils.formatLong(item.getQuantity())) + .setRank(item.getRank()) .setCountryName(userProfile.getCountryName()) .setCountryCode(userProfile.getCountryCode()); }).toList(); + + + + PageResult result = new PageResult<>(); + result.setTotal(kingQueenCountPage.getTotalElements()); + result.setCurrent(kingQueenCountPage.getNumber()); + result.setSize(kingQueenCountPage.getSize()); + result.setRecords(userRankList); + + return result; } @@ -74,6 +96,11 @@ public class WeekKingQueenRankingQueryExe { return CollectionUtils.newArrayList(); } + private Page pageWeekTop(String sysOrigin, KingQueenType type, + Integer weekType, Pageable pageable) { + return weekKingQueenCountService.pageWeekTop(sysOrigin, type, weekType, pageable); + } + private Set getUserIds(List weekStarGiftCounts) { return weekStarGiftCounts.stream().map(WeekKingQueenCount::getUserId) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenServiceImpl.java index 90e2e1e7..59d61f51 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenServiceImpl.java @@ -1,6 +1,7 @@ package com.red.circle.other.app.service.activity; import com.red.circle.common.business.dto.cmd.AppExtCommand; +import com.red.circle.framework.dto.PageResult; import com.red.circle.other.app.command.activity.query.ActivityRewardQueryExe; import com.red.circle.other.app.command.activity.query.WeekKingQueenCountDownQueryExe; import com.red.circle.other.app.command.activity.query.WeekKingQueenRankingQueryExe; @@ -10,6 +11,7 @@ import com.red.circle.other.app.dto.clientobject.activity.UserRankCO; import com.red.circle.other.app.dto.cmd.activity.WeekKingQueenRankingQueryCmd; import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; /** @@ -27,7 +29,7 @@ public class WeekKingQueenServiceImpl implements WeekKingQueenService { private final WeekKingQueenUserContributeQueryExe weekKingQueenUserContributeQueryExe; @Override - public List listRanking(WeekKingQueenRankingQueryCmd cmd) { + public PageResult listRanking(WeekKingQueenRankingQueryCmd cmd) { return weekKingQueenRankingQueryExe.execute(cmd); } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/UserRankCO.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/UserRankCO.java index 5c3a82ad..4ea02343 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/UserRankCO.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/UserRankCO.java @@ -25,6 +25,11 @@ public class UserRankCO extends ClientObject { @JsonSerialize(using = ToStringSerializer.class) private Long userId; + /** + * 账号 + */ + private String account; + /** * 用户头像. */ @@ -40,6 +45,11 @@ public class UserRankCO extends ClientObject { */ private String quantity; + /** + * 排名 + */ + Integer rank; + /** * 国家名称. */ diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/WeekKingQueenRankingQueryCmd.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/WeekKingQueenRankingQueryCmd.java index d58fe5dd..dd1831ed 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/WeekKingQueenRankingQueryCmd.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/cmd/activity/WeekKingQueenRankingQueryCmd.java @@ -1,5 +1,6 @@ package com.red.circle.other.app.dto.cmd.activity; +import com.red.circle.common.business.dto.PageQueryCmd; import com.red.circle.common.business.dto.cmd.AppExtCommand; import com.red.circle.common.business.enums.WeekLeaderboardEnum; import jakarta.validation.constraints.NotNull; @@ -13,7 +14,7 @@ import lombok.EqualsAndHashCode; */ @Data @EqualsAndHashCode(callSuper = false) -public class WeekKingQueenRankingQueryCmd extends AppExtCommand { +public class WeekKingQueenRankingQueryCmd extends PageQueryCmd { /** * 类型. @@ -30,6 +31,6 @@ public class WeekKingQueenRankingQueryCmd extends AppExtCommand { /** * 获取数量. */ - @NotNull(message = "topSize {not.null}") +// @NotNull(message = "topSize {not.null}") private Integer topSize; } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenService.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenService.java index 94febb1a..882772c0 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenService.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekKingQueenService.java @@ -1,6 +1,7 @@ package com.red.circle.other.app.service.activity; import com.red.circle.common.business.dto.cmd.AppExtCommand; +import com.red.circle.framework.dto.PageResult; import com.red.circle.other.app.dto.clientobject.activity.UserCharmWealthContributeCO; import com.red.circle.other.app.dto.clientobject.activity.UserRankCO; import com.red.circle.other.app.dto.cmd.activity.WeekKingQueenRankingQueryCmd; @@ -13,7 +14,7 @@ import java.util.List; */ public interface WeekKingQueenService { - List listRanking(WeekKingQueenRankingQueryCmd cmd); + PageResult listRanking(WeekKingQueenRankingQueryCmd cmd); String countDown(); diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/WeekKingQueenCount.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/WeekKingQueenCount.java index f9ce81cd..ecdecbff 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/WeekKingQueenCount.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/WeekKingQueenCount.java @@ -10,6 +10,7 @@ import lombok.Data; import lombok.experimental.Accessors; import lombok.experimental.FieldDefaults; import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.mapping.Document; /** @@ -43,6 +44,12 @@ public class WeekKingQueenCount implements Serializable { @JsonSerialize(using = ToStringSerializer.class) Long userId; + /** + * 排名 + */ + @Transient + Integer rank; + /** * 类型. */ diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekKingQueenCountService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekKingQueenCountService.java index 17570043..3a74c42e 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekKingQueenCountService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekKingQueenCountService.java @@ -2,6 +2,9 @@ package com.red.circle.other.infra.database.mongo.service.activity; import com.red.circle.other.infra.database.mongo.entity.activity.WeekKingQueenCount; import com.red.circle.other.inner.enums.activity.KingQueenType; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + import java.util.List; /** @@ -31,6 +34,12 @@ public interface WeekKingQueenCountService { */ List listLastWeekTop(String sysOrigin, KingQueenType type, Integer size); + /** + * 分页本周和上周的top + */ + Page pageWeekTop(String sysOrigin, KingQueenType type, + Integer weekType, Pageable pageable); + /** * 矫正数据. */ diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekKingQueenCountServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekKingQueenCountServiceImpl.java index 029f79e7..402384dc 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekKingQueenCountServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekKingQueenCountServiceImpl.java @@ -13,6 +13,9 @@ import java.util.List; import java.util.Objects; import lombok.RequiredArgsConstructor; import org.springframework.dao.DuplicateKeyException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; @@ -56,23 +59,77 @@ public class WeekKingQueenCountServiceImpl implements WeekKingQueenCountService @Override public List listThisWeekTop(String sysOrigin, KingQueenType type, Integer size) { - return mongoTemplate + List results = mongoTemplate .find(Query.query(Criteria.where("sysOrigin").is(sysOrigin) .and("group").is(getThisWeekDate()) .and("type").is(type)) .with(Sort.by(Sort.Order.desc("quantity"))) .limit(size), WeekKingQueenCount.class); + + // 给结果赋值 rank + for (int i = 0; i < results.size(); i++) { + results.get(i).setRank(i + 1); // rank 从 1 开始 + } + + return results; } @Override public List listLastWeekTop(String sysOrigin, KingQueenType type, Integer size) { - return mongoTemplate + List results = mongoTemplate .find(Query.query(Criteria.where("sysOrigin").is(sysOrigin) .and("group").is(getLastWeekDate()) .and("type").is(type)) .with(Sort.by(Sort.Order.desc("quantity"))) .limit(size), WeekKingQueenCount.class); + + // 给结果赋值 rank + for (int i = 0; i < results.size(); i++) { + results.get(i).setRank(i + 1); // rank 从 1 开始 + } + return results; + } + + @Override + public Page pageWeekTop(String sysOrigin, KingQueenType type, + Integer weekType, Pageable pageable) { + // 根据weekType确定查询的周期 + String weekGroup = getWeekDateByType(weekType); + + // 构建查询条件 + Query query = Query.query(Criteria.where("sysOrigin").is(sysOrigin) + .and("group").is(weekGroup) + .and("type").is(type)) + .with(Sort.by(Sort.Order.desc("quantity"))); + + // 查询总数 + long total = mongoTemplate.count(query, WeekKingQueenCount.class); + + // 分页查询 + query.with(pageable); + List results = mongoTemplate.find(query, WeekKingQueenCount.class); + + // 计算rank(考虑分页偏移量) + int startRank = (int) (pageable.getOffset() + 1); + for (int i = 0; i < results.size(); i++) { + results.get(i).setRank(startRank + i); + } + + return new PageImpl<>(results, pageable, total); + } + + + /** + * 根据周类型获取对应的日期组 + * @param weekType 0-本周,1-上周 + * @return 周期标识 + */ + private String getWeekDateByType(Integer weekType) { + if (weekType != null && weekType == 1) { + return getLastWeekDate(); + } + return getThisWeekDate(); } @Override