新增历史TOP1

This commit is contained in:
tianfeng 2025-09-19 11:26:04 +08:00
parent 0a6a4ecad0
commit e771d1d034
7 changed files with 257 additions and 0 deletions

View File

@ -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<WeekStarHistoryVO> getHistoryTop1(AppExtCommand cmd) {
return weekStarService.getHistoryTop1(cmd);
}
}

View File

@ -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<WeekStarHistoryVO> execute(AppExtCommand cmd) {
// 获取历史TOP1数据
List<WeekStarGiftCount> historyTop1List = weekStarGiftCountService
.getHistoryTop1(cmd.getReqSysOrigin().getOrigin(), 20);
if (CollectionUtils.isEmpty(historyTop1List)) {
return List.of();
}
// 获取用户信息
Map<Long, UserProfileDTO> userProfileMap = getUserProfileMap(historyTop1List);
// 转换为返回对象
return historyTop1List.stream()
.map(item -> convertToWeekStarHistoryVO(item, userProfileMap))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
private Map<Long, UserProfileDTO> getUserProfileMap(List<WeekStarGiftCount> historyTop1List) {
Set<Long> userIds = historyTop1List.stream()
.map(WeekStarGiftCount::getUserId)
.collect(Collectors.toSet());
return userProfileAppConvertor.toMapUserProfileDTO(
userProfileGateway.mapByUserIds(userIds)
);
}
private WeekStarHistoryVO convertToWeekStarHistoryVO(WeekStarGiftCount weekStarGiftCount,
Map<Long, UserProfileDTO> 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;
}
}

View File

@ -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<WeekStarHistoryVO> getHistoryTop1(AppExtCommand cmd) {
return weekStarHistoryTop1QueryExe.execute(cmd);
}
}

View File

@ -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;
}

View File

@ -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<WeekStarHistoryVO> getHistoryTop1(AppExtCommand cmd);
}

View File

@ -62,6 +62,11 @@ public interface WeekStarGiftCountService {
*/
WeekStarGiftCount getMyLastWeekRank(String sysOrigin, Long userId);
/**
* 获取历史TOP1列表.
*/
List<WeekStarGiftCount> getHistoryTop1(String sysOrigin, Integer limit);
/**
* 矫正数据.
*/

View File

@ -75,6 +75,50 @@ public class WeekStarGiftCountServiceImpl implements WeekStarGiftCountService {
return getMyRankForWeek(sysOrigin, userId, getLastWeekDate());
}
@Override
public List<WeekStarGiftCount> 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<WeekStarGiftCount> results = mongoTemplate
.aggregate(aggregation, WeekStarGiftCount.class, WeekStarGiftCount.class);
List<WeekStarGiftCount> resultList = results.getMappedResults();
// 添加排名字段所有TOP1的rank都是1
resultList.forEach(item -> item.setRank(1));
return resultList;
}
/**
* 获取用户在指定周的排名.
*/