周星我的排名完善

This commit is contained in:
tianfeng 2025-09-18 22:33:44 +08:00
parent 16084a3b74
commit 0a6a4ecad0
7 changed files with 135 additions and 1 deletions

View File

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

View File

@ -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<Long, UserProfileDTO> 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<Long> getUserIds(List<WeekStarGiftCount> weekStarGiftCounts) {
return weekStarGiftCounts.stream().map(WeekStarGiftCount::getUserId)

View File

@ -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<LastWeekStarGiftUserRankCO> lastWeekFirstPlace(AppExtCommand cmd) {
return lastWeekFirstPlaceQueryExe.execute(cmd);
}
@Override
public UserRankCO getMyRank(WeekStarRankingQueryCmd cmd) {
return weekStarGiftRankingQueryExe.myRank(cmd);
}
}

View File

@ -30,4 +30,9 @@ public class WeekStarGiftUserRankCO extends ClientObject {
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long giftId;
/**
* 我的排名.
*/
private UserRankCO myRank;
}

View File

@ -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<LastWeekStarGiftUserRankCO> lastWeekFirstPlace(AppExtCommand cmd);
/**
* 获取我的排名.
*/
UserRankCO getMyRank(WeekStarRankingQueryCmd cmd);
}

View File

@ -52,6 +52,16 @@ public interface WeekStarGiftCountService {
*/
PageResult<WeekStarGiftCount> listLastWeekTopV2(String sysOrigin, Pageable pageable);
/**
* 获取我的本周排名.
*/
WeekStarGiftCount getMyThisWeekRank(String sysOrigin, Long userId);
/**
* 获取我的上周排名.
*/
WeekStarGiftCount getMyLastWeekRank(String sysOrigin, Long userId);
/**
* 矫正数据.
*/

View File

@ -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<WeekStarGiftCount> results = mongoTemplate
.aggregate(aggregation, WeekStarGiftCount.class, WeekStarGiftCount.class);
List<WeekStarGiftCount> 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<WeekStarGiftCount> listThisWeekTop(String sysOrigin, Long giftId, Integer size) {
return mongoTemplate