diff --git a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekStarRestController.java b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekStarRestController.java index df09e693..22dface3 100644 --- a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekStarRestController.java +++ b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/activity/room/WeekStarRestController.java @@ -3,6 +3,7 @@ package com.red.circle.other.adapter.app.activity.room; import com.red.circle.common.business.dto.cmd.AppExtCommand; import com.red.circle.framework.web.controller.BaseController; import com.red.circle.other.app.dto.clientobject.activity.LastWeekStarGiftUserRankCO; +import com.red.circle.other.app.dto.clientobject.activity.UserRankCO; import com.red.circle.other.app.dto.clientobject.activity.WeekStarGiftUserRankCO; import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO; import com.red.circle.other.app.dto.cmd.activity.WeekStarGiftQueryCmd; @@ -109,4 +110,17 @@ public class WeekStarRestController extends BaseController { return weekStarService.weekStarStartEndTime(); } + /** + * 获取我的排名. + * + * @eo.name 获取我的排名 + * @eo.url /my-rank + * @eo.method get + * @eo.request-type formdata + */ + @GetMapping("/my-rank") + public UserRankCO getMyRank(@Validated WeekStarRankingQueryCmd cmd) { + return weekStarService.getMyRank(cmd); + } + } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarGiftRankingQueryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarGiftRankingQueryExe.java index f9b68c92..acc294ec 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarGiftRankingQueryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarGiftRankingQueryExe.java @@ -35,9 +35,39 @@ public class WeekStarGiftRankingQueryExe { private final WeekStarGiftCountService weekStarGiftCountService; public WeekStarGiftUserRankCO execute(WeekStarRankingQueryCmd cmd) { - return new WeekStarGiftUserRankCO() + WeekStarGiftUserRankCO result = new WeekStarGiftUserRankCO() .setGiftId(cmd.getGiftId()) .setRankUsers(leaderboard(cmd, cmd.getCurrent(), cmd.getSize())); + return result; + } + + /** + * 将 WeekStarGiftCount 转换为 UserRankCO. + */ + private UserRankCO convertToUserRankCO(WeekStarGiftCount weekStarGiftCount) { + if (Objects.isNull(weekStarGiftCount)) { + return null; + } + + // 获取用户信息 + Map userProfileMap = userProfileAppConvertor.toMapUserProfileDTO( + userProfileGateway.mapByUserIds(Set.of(weekStarGiftCount.getUserId())) + ); + + UserProfileDTO userProfile = userProfileMap.get(weekStarGiftCount.getUserId()); + if (Objects.isNull(userProfile)) { + return null; + } + + return new UserRankCO() + .setUserId(userProfile.getId()) + .setAccount(userProfile.getAccount()) + .setUserAvatar(userProfile.getUserAvatar()) + .setRank(weekStarGiftCount.getRank()) + .setUserNickname(userProfile.getUserNickname()) + .setQuantity(NumUtils.formatLong(weekStarGiftCount.getQuantity())) + .setCountryCode(userProfile.getCountryCode()) + .setCountryName(userProfile.getCountryName()); } @@ -95,6 +125,23 @@ public class WeekStarGiftRankingQueryExe { return PageResult.newPageResult(20); } + public UserRankCO myRank(WeekStarRankingQueryCmd cmd) { + if (Objects.equals(cmd.getType(), WeekLeaderboardEnum.THIS_WEEK)) { + return convertToUserRankCO(weekStarGiftCountService.getMyThisWeekRank( + cmd.getReqSysOrigin().getOrigin(), + cmd.getReqUserId() + )); + } + if (Objects.equals(cmd.getType(), WeekLeaderboardEnum.LAST_WEEK)) { + return convertToUserRankCO(weekStarGiftCountService.getMyLastWeekRank( + cmd.getReqSysOrigin().getOrigin(), + cmd.getReqUserId() + )); + } + return null; + } + + private Set getUserIds(List weekStarGiftCounts) { return weekStarGiftCounts.stream().map(WeekStarGiftCount::getUserId) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekStarServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekStarServiceImpl.java index d7ac0ab8..a53abb97 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekStarServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/activity/WeekStarServiceImpl.java @@ -9,6 +9,7 @@ import com.red.circle.other.app.command.activity.query.WeekStarRewardQueryExe; import com.red.circle.other.app.command.activity.query.WeekStarStartEndTimeQueryExe; import com.red.circle.other.app.command.activity.query.WeekStarThisWeeksGiftQueryExe; import com.red.circle.other.app.dto.clientobject.activity.LastWeekStarGiftUserRankCO; +import com.red.circle.other.app.dto.clientobject.activity.UserRankCO; import com.red.circle.other.app.dto.clientobject.activity.WeekStarGiftUserRankCO; import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO; import com.red.circle.other.app.dto.cmd.activity.WeekStarGiftQueryCmd; @@ -78,4 +79,9 @@ public class WeekStarServiceImpl implements WeekStarService { public List lastWeekFirstPlace(AppExtCommand cmd) { return lastWeekFirstPlaceQueryExe.execute(cmd); } + + @Override + public UserRankCO getMyRank(WeekStarRankingQueryCmd cmd) { + return weekStarGiftRankingQueryExe.myRank(cmd); + } } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarGiftUserRankCO.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarGiftUserRankCO.java index 30c4ceaa..3a10cbb8 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarGiftUserRankCO.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarGiftUserRankCO.java @@ -30,4 +30,9 @@ public class WeekStarGiftUserRankCO extends ClientObject { */ @JsonSerialize(using = ToStringSerializer.class) private Long giftId; + + /** + * 我的排名. + */ + private UserRankCO myRank; } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekStarService.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekStarService.java index fcc5ec91..500ecec0 100644 --- a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekStarService.java +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/service/activity/WeekStarService.java @@ -3,6 +3,7 @@ package com.red.circle.other.app.service.activity; import com.red.circle.common.business.dto.cmd.AppExtCommand; import com.red.circle.other.app.dto.clientobject.activity.LastWeekStarGiftUserRankCO; +import com.red.circle.other.app.dto.clientobject.activity.UserRankCO; import com.red.circle.other.app.dto.clientobject.activity.WeekStarGiftUserRankCO; import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO; import com.red.circle.other.app.dto.cmd.activity.WeekStarGiftQueryCmd; @@ -29,4 +30,9 @@ public interface WeekStarService { List lastWeekFirstPlace(AppExtCommand cmd); + /** + * 获取我的排名. + */ + UserRankCO getMyRank(WeekStarRankingQueryCmd cmd); + } diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekStarGiftCountService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekStarGiftCountService.java index 7bedfd1c..b160463c 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekStarGiftCountService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/WeekStarGiftCountService.java @@ -52,6 +52,16 @@ public interface WeekStarGiftCountService { */ PageResult listLastWeekTopV2(String sysOrigin, Pageable pageable); + /** + * 获取我的本周排名. + */ + WeekStarGiftCount getMyThisWeekRank(String sysOrigin, Long userId); + + /** + * 获取我的上周排名. + */ + WeekStarGiftCount getMyLastWeekRank(String sysOrigin, Long userId); + /** * 矫正数据. */ diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekStarGiftCountServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekStarGiftCountServiceImpl.java index 80502199..89bc11cd 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekStarGiftCountServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/activity/impl/WeekStarGiftCountServiceImpl.java @@ -65,6 +65,52 @@ public class WeekStarGiftCountServiceImpl implements WeekStarGiftCountService { ); } + @Override + public WeekStarGiftCount getMyThisWeekRank(String sysOrigin, Long userId) { + return getMyRankForWeek(sysOrigin, userId, getThisWeekDate()); + } + + @Override + public WeekStarGiftCount getMyLastWeekRank(String sysOrigin, Long userId) { + return getMyRankForWeek(sysOrigin, userId, getLastWeekDate()); + } + + /** + * 获取用户在指定周的排名. + */ + private WeekStarGiftCount getMyRankForWeek(String sysOrigin, Long userId, String weekDate) { + // 先获取所有用户的聚合数据(按quantity倒序) + Aggregation aggregation = Aggregation.newAggregation( + Aggregation.match(Criteria.where("sysOrigin").is(sysOrigin) + .and("group").is(weekDate)), + Aggregation.group("userId") + .first("sysOrigin").as("sysOrigin") + .first("userId").as("userId") + .first("group").as("group") + .first("createTime").as("createTime") + .first("expiredTime").as("expiredTime") + .sum("quantity").as("quantity"), + Aggregation.sort(Sort.by(Sort.Order.desc("quantity"))) + ); + + AggregationResults results = mongoTemplate + .aggregate(aggregation, WeekStarGiftCount.class, WeekStarGiftCount.class); + + List resultList = results.getMappedResults(); + + // 查找指定用户并设置排名 + for (int i = 0; i < resultList.size(); i++) { + WeekStarGiftCount item = resultList.get(i); + if (userId.equals(item.getUserId())) { + item.setRank(i + 1); // 排名从1开始 + return item; + } + } + + // 如果没有找到该用户的记录,返回null或默认值 + return null; + } + @Override public List listThisWeekTop(String sysOrigin, Long giftId, Integer size) { return mongoTemplate