From e771d1d03474cdde380f388762075b143d96adc1 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 19 Sep 2025 11:26:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8E=86=E5=8F=B2TOP1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activity/room/WeekStarRestController.java | 14 +++ .../query/WeekStarHistoryTop1QueryExe.java | 97 +++++++++++++++++++ .../service/activity/WeekStarServiceImpl.java | 8 ++ .../activity/WeekStarHistoryVO.java | 83 ++++++++++++++++ .../app/service/activity/WeekStarService.java | 6 ++ .../activity/WeekStarGiftCountService.java | 5 + .../impl/WeekStarGiftCountServiceImpl.java | 44 +++++++++ 7 files changed, 257 insertions(+) create mode 100644 rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarHistoryTop1QueryExe.java create mode 100644 rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarHistoryVO.java 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 22dface3..cf241847 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 @@ -5,6 +5,7 @@ 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.activity.WeekStarHistoryVO; import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO; import com.red.circle.other.app.dto.cmd.activity.WeekStarGiftQueryCmd; import com.red.circle.other.app.dto.cmd.activity.WeekStarRankingQueryCmd; @@ -123,4 +124,17 @@ public class WeekStarRestController extends BaseController { return weekStarService.getMyRank(cmd); } + /** + * 获取历史TOP1. + * + * @eo.name 获取历史TOP1 + * @eo.url /history-top1 + * @eo.method get + * @eo.request-type formdata + */ + @GetMapping("/history-top1") + public List getHistoryTop1(AppExtCommand cmd) { + return weekStarService.getHistoryTop1(cmd); + } + } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarHistoryTop1QueryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarHistoryTop1QueryExe.java new file mode 100644 index 00000000..25add531 --- /dev/null +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/query/WeekStarHistoryTop1QueryExe.java @@ -0,0 +1,97 @@ +package com.red.circle.other.app.command.activity.query; + +import com.red.circle.common.business.dto.cmd.AppExtCommand; +import com.red.circle.other.app.convertor.user.UserProfileAppConvertor; +import com.red.circle.other.app.dto.clientobject.activity.WeekStarHistoryVO; +import com.red.circle.other.domain.gateway.user.UserProfileGateway; +import com.red.circle.other.infra.database.mongo.entity.activity.WeekStarGiftCount; +import com.red.circle.other.infra.database.mongo.service.activity.WeekStarGiftCountService; +import com.red.circle.other.inner.enums.activity.KingQueenType; +import com.red.circle.other.inner.model.dto.user.UserProfileDTO; +import com.red.circle.tool.core.collection.CollectionUtils; +import java.sql.Timestamp; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +/** + * 周星历史TOP1查询执行器. + * + * @author system + */ +@Component +@RequiredArgsConstructor +public class WeekStarHistoryTop1QueryExe { + + private final UserProfileGateway userProfileGateway; + private final UserProfileAppConvertor userProfileAppConvertor; + private final WeekStarGiftCountService weekStarGiftCountService; + + public List execute(AppExtCommand cmd) { + // 获取历史TOP1数据 + List historyTop1List = weekStarGiftCountService + .getHistoryTop1(cmd.getReqSysOrigin().getOrigin(), 20); + + if (CollectionUtils.isEmpty(historyTop1List)) { + return List.of(); + } + + // 获取用户信息 + Map userProfileMap = getUserProfileMap(historyTop1List); + + // 转换为返回对象 + return historyTop1List.stream() + .map(item -> convertToWeekStarHistoryVO(item, userProfileMap)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + private Map getUserProfileMap(List historyTop1List) { + Set userIds = historyTop1List.stream() + .map(WeekStarGiftCount::getUserId) + .collect(Collectors.toSet()); + + return userProfileAppConvertor.toMapUserProfileDTO( + userProfileGateway.mapByUserIds(userIds) + ); + } + + private WeekStarHistoryVO convertToWeekStarHistoryVO(WeekStarGiftCount weekStarGiftCount, + Map userProfileMap) { + if (Objects.isNull(weekStarGiftCount)) { + return null; + } + + UserProfileDTO userProfile = userProfileMap.get(weekStarGiftCount.getUserId()); + if (Objects.isNull(userProfile)) { + return null; + } + + WeekStarHistoryVO historyVO = new WeekStarHistoryVO(); + historyVO.setId(weekStarGiftCount.getUserId() + "_" + weekStarGiftCount.getGroup()); + historyVO.setSysOrigin(weekStarGiftCount.getSysOrigin()); + historyVO.setAccount(userProfile.getAccount()); + historyVO.setUserAvatar(userProfile.getUserAvatar()); + historyVO.setUserNickname(userProfile.getUserNickname()); + historyVO.setUserId(weekStarGiftCount.getUserId()); + historyVO.setUserSex(userProfile.getUserSex()); + historyVO.setRank(weekStarGiftCount.getRank()); + historyVO.setGroup(weekStarGiftCount.getGroup()); + historyVO.setQuantity(weekStarGiftCount.getQuantity()); + + // 转换时间类型 + if (Objects.nonNull(weekStarGiftCount.getCreateTime())) { + historyVO.setCreateTime(weekStarGiftCount.getCreateTime()); + } + if (Objects.nonNull(weekStarGiftCount.getExpiredTime())) { + historyVO.setExpiredTime(weekStarGiftCount.getExpiredTime()); + } + + return historyVO; + } + +} 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 a53abb97..0e4ab9a9 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 @@ -5,12 +5,14 @@ import com.red.circle.common.business.enums.WeekLeaderboardEnum; import com.red.circle.other.app.command.activity.query.LastWeekFirstPlaceQueryExe; import com.red.circle.other.app.command.activity.query.WeekStarCountDownQueryExe; import com.red.circle.other.app.command.activity.query.WeekStarGiftRankingQueryExe; +import com.red.circle.other.app.command.activity.query.WeekStarHistoryTop1QueryExe; 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.activity.WeekStarHistoryVO; import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO; import com.red.circle.other.app.dto.cmd.activity.WeekStarGiftQueryCmd; import com.red.circle.other.app.dto.cmd.activity.WeekStarRankingQueryCmd; @@ -35,6 +37,7 @@ public class WeekStarServiceImpl implements WeekStarService { private final WeekStarCountDownQueryExe weekStarCountDownQueryExe; private final LastWeekFirstPlaceQueryExe lastWeekFirstPlaceQueryExe; private final WeekStarGiftRankingQueryExe weekStarGiftRankingQueryExe; + private final WeekStarHistoryTop1QueryExe weekStarHistoryTop1QueryExe; private final WeekStarStartEndTimeQueryExe weekStarStartEndTimeQueryExe; private final WeekStarThisWeeksGiftQueryExe weekStarThisWeeksGiftQueryExe; @@ -84,4 +87,9 @@ public class WeekStarServiceImpl implements WeekStarService { public UserRankCO getMyRank(WeekStarRankingQueryCmd cmd) { return weekStarGiftRankingQueryExe.myRank(cmd); } + + @Override + public List getHistoryTop1(AppExtCommand cmd) { + return weekStarHistoryTop1QueryExe.execute(cmd); + } } diff --git a/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarHistoryVO.java b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarHistoryVO.java new file mode 100644 index 00000000..aead24bb --- /dev/null +++ b/rc-service/rc-service-other/other-client/src/main/java/com/red/circle/other/app/dto/clientobject/activity/WeekStarHistoryVO.java @@ -0,0 +1,83 @@ +package com.red.circle.other.app.dto.clientobject.activity; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.red.circle.other.inner.enums.activity.KingQueenType; +import lombok.Data; + +import java.io.Serializable; +import java.sql.Timestamp; + +/** + * 周星历史TOP1 + */ +@Data +public class WeekStarHistoryVO implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 记录id. + */ + @JsonSerialize(using = ToStringSerializer.class) + String id; + + /** + * 归属平台. + */ + String sysOrigin; + + + /** + * 账号. + */ + private String account; + + /** + * 头像. + */ + private String userAvatar; + + /** + * 用户昵称. + */ + private String userNickname; + + /** + * 用户id. + */ + @JsonSerialize(using = ToStringSerializer.class) + Long userId; + + /** + * 用户性别:0 女,1 男. + */ + private Integer userSex; + + /** + * 排名 + */ + Integer rank; + + /** + * 分组. + */ + String group; + + /** + * 数量. + */ + @JsonSerialize(using = ToStringSerializer.class) + Long quantity; + + /** + * 创建时间. + */ + Timestamp createTime; + + /** + * 过期时间. + */ + Timestamp expiredTime; + +} 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 500ecec0..fdf8da7d 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 @@ -5,6 +5,7 @@ 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.activity.WeekStarHistoryVO; import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO; import com.red.circle.other.app.dto.cmd.activity.WeekStarGiftQueryCmd; import com.red.circle.other.app.dto.cmd.activity.WeekStarRankingQueryCmd; @@ -35,4 +36,9 @@ public interface WeekStarService { */ UserRankCO getMyRank(WeekStarRankingQueryCmd cmd); + /** + * 获取历史TOP1. + */ + List getHistoryTop1(AppExtCommand 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 b160463c..504929f1 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 @@ -62,6 +62,11 @@ public interface WeekStarGiftCountService { */ WeekStarGiftCount getMyLastWeekRank(String sysOrigin, Long userId); + /** + * 获取历史TOP1列表. + */ + List getHistoryTop1(String sysOrigin, Integer limit); + /** * 矫正数据. */ 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 89bc11cd..ea6c09b7 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 @@ -75,6 +75,50 @@ public class WeekStarGiftCountServiceImpl implements WeekStarGiftCountService { return getMyRankForWeek(sysOrigin, userId, getLastWeekDate()); } + @Override + public List getHistoryTop1(String sysOrigin, Integer limit) { + // 获取所有历史周数据,按周分组,每周只取TOP1 + Aggregation aggregation = Aggregation.newAggregation( + // 筛选指定系统 + Aggregation.match(Criteria.where("sysOrigin").is(sysOrigin)), + // 按group(周)和userId分组,汇总数量 + Aggregation.group("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"), + // 按group和quantity排序 + Aggregation.sort(Sort.by( + Sort.Order.desc("group"), + Sort.Order.desc("quantity") + )), + // 按group分组,只取每组的第一条(TOP1) + Aggregation.group("group") + .first("sysOrigin").as("sysOrigin") + .first("userId").as("userId") + .first("group").as("group") + .first("createTime").as("createTime") + .first("expiredTime").as("expiredTime") + .first("quantity").as("quantity"), + // 按group倒序排列(最新的周在前) + Aggregation.sort(Sort.by(Sort.Order.desc("group"))), + // 限制返回数量 + Aggregation.limit(limit != null ? limit : 20) + ); + + AggregationResults results = mongoTemplate + .aggregate(aggregation, WeekStarGiftCount.class, WeekStarGiftCount.class); + + List resultList = results.getMappedResults(); + + // 添加排名字段(所有TOP1的rank都是1) + resultList.forEach(item -> item.setRank(1)); + + return resultList; + } + /** * 获取用户在指定周的排名. */