排行榜新增 总榜前三名接口
This commit is contained in:
parent
b75aad4c9c
commit
b6de23e28e
@ -59,6 +59,9 @@ public class RankingActivityRestController extends BaseController {
|
||||
return rankingActivityService.getRankingList(cmd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 查询历史Top1列表(周榜)
|
||||
*
|
||||
@ -89,4 +92,17 @@ public class RankingActivityRestController extends BaseController {
|
||||
cmd.setTemplateId("2001207026499137537");
|
||||
return rankingActivityService.getTopFourRankingWithReward(cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询总榜前三名
|
||||
*
|
||||
* 用途:获取指定活动的总榜(跨所有周期,按用户ID分组汇总)前三名用户
|
||||
*
|
||||
* @param cmd 查询命令
|
||||
* @return 总榜前三名
|
||||
*/
|
||||
@PostMapping("/top-three-overall")
|
||||
public List<RankingUserVO> getTopThreeOverall(@RequestBody @Validated RankingListQueryCmd cmd) {
|
||||
return rankingActivityService.getTopThreeOverall(cmd);
|
||||
}
|
||||
}
|
||||
@ -20,12 +20,16 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
|
||||
import com.red.circle.other.domain.model.user.UserProfile;
|
||||
import com.red.circle.other.domain.ranking.RankingActivityType;
|
||||
import com.red.circle.other.domain.ranking.RankingCycleType;
|
||||
import com.red.circle.other.infra.database.mongo.entity.activity.RankingActivityRecord;
|
||||
import com.red.circle.other.infra.gateway.RankingActivityGateway;
|
||||
import com.red.circle.tool.core.collection.CollectionUtils;
|
||||
import com.red.circle.tool.core.num.NumUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -45,6 +49,7 @@ public class RankingActivityServiceImpl implements RankingActivityService {
|
||||
private final RedisService redisService;
|
||||
private final UserMqMessageService userMqMessageService;
|
||||
private final UserProfileGateway userProfileGateway;
|
||||
private final RankingActivityGateway rankingActivityGateway;
|
||||
|
||||
private static final String RANKING_LIST_CACHE_KEY_PREFIX = "ranking_list:";
|
||||
private static final int CACHE_EXPIRE_MINUTES = 10;
|
||||
@ -224,4 +229,46 @@ public class RankingActivityServiceImpl implements RankingActivityService {
|
||||
private String buildRankingListCacheKey(RankingListQueryCmd cmd) {
|
||||
return RANKING_LIST_CACHE_KEY_PREFIX + RankingActivityType.getByCode(cmd.getActivityType()).name() + ":" + cmd.getCycleKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RankingUserVO> getTopThreeOverall(RankingListQueryCmd cmd) {
|
||||
// 查询总榜排行(按用户ID分组汇总)
|
||||
List<RankingActivityRecord> records = rankingActivityGateway.getOverallRankingList(
|
||||
cmd.getReqSysOrigin().getOrigin(),
|
||||
RankingActivityType.getByCode(cmd.getActivityType()),
|
||||
3 // 只获取前三名
|
||||
);
|
||||
|
||||
if (CollectionUtils.isEmpty(records)) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
Set<Long> userIds = records.stream()
|
||||
.map(RankingActivityRecord::getUserId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Map<Long, UserProfile> userProfileMap = userProfileGateway.mapByUserIds(userIds);
|
||||
|
||||
// 转换为VO并设置排名
|
||||
return IntStream.range(0, records.size())
|
||||
.mapToObj(i -> {
|
||||
RankingActivityRecord record = records.get(i);
|
||||
UserProfile profile = userProfileMap.get(record.getUserId());
|
||||
|
||||
RankingUserVO vo = new RankingUserVO();
|
||||
vo.setUserId(record.getUserId());
|
||||
vo.setRank(i + 1); // 排名从1开始
|
||||
vo.setQuantity(NumUtils.formatLong(record.getQuantity()));
|
||||
|
||||
if (profile != null) {
|
||||
vo.setAccount(profile.getAccount());
|
||||
vo.setNickname(profile.getUserNickname());
|
||||
vo.setAvatar(profile.getUserAvatar());
|
||||
}
|
||||
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,4 +55,12 @@ public interface RankingActivityService {
|
||||
* @return 排行榜前四名
|
||||
*/
|
||||
List<RankingUserVO> getTopFourRankingWithReward(RankingListQueryCmd cmd);
|
||||
|
||||
/**
|
||||
* 获取总榜前三名
|
||||
*
|
||||
* @param cmd 查询命令
|
||||
* @return 总榜前三名用户列表
|
||||
*/
|
||||
List<RankingUserVO> getTopThreeOverall(RankingListQueryCmd cmd);
|
||||
}
|
||||
|
||||
@ -108,4 +108,18 @@ public interface RankingActivityGateway {
|
||||
RankingActivityType activityType,
|
||||
String cycleKey
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询总榜排行(按用户ID分组汇总所有周期数据)
|
||||
*
|
||||
* @param sysOrigin 平台
|
||||
* @param activityType 活动类型
|
||||
* @param topN 查询数量
|
||||
* @return 总榜排行列表
|
||||
*/
|
||||
List<RankingActivityRecord> getOverallRankingList(
|
||||
String sysOrigin,
|
||||
RankingActivityType activityType,
|
||||
Integer topN
|
||||
);
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.mongodb.core.FindAndModifyOptions;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
@ -164,4 +165,44 @@ public class RankingActivityGatewayImpl implements RankingActivityGateway {
|
||||
userIds, activityType, cycleKey, NOT_DELETED
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RankingActivityRecord> getOverallRankingList(
|
||||
String sysOrigin,
|
||||
RankingActivityType activityType,
|
||||
Integer topN) {
|
||||
|
||||
// 使用 MongoDB 聚合查询,按 userId 分组汇总所有周期的数据
|
||||
Aggregation aggregation =
|
||||
Aggregation.newAggregation(
|
||||
// 1. 匹配条件:平台、活动类型、未删除
|
||||
Aggregation.match(
|
||||
Criteria.where("sysOrigin").is(sysOrigin)
|
||||
.and("activityType").is(activityType)
|
||||
.and("deleted").is(NOT_DELETED)
|
||||
),
|
||||
// 2. 按 userId 分组,汇总 quantity
|
||||
Aggregation.group("userId")
|
||||
.sum("quantity").as("quantity")
|
||||
.first("userId").as("userId")
|
||||
.first("userSex").as("userSex")
|
||||
.first("activityType").as("activityType")
|
||||
.first("dimension").as("dimension"),
|
||||
// 3. 按总数量降序排序
|
||||
Aggregation.sort(
|
||||
org.springframework.data.domain.Sort.Direction.DESC, "quantity"
|
||||
),
|
||||
// 4. 限制返回数量
|
||||
Aggregation.limit(topN)
|
||||
);
|
||||
|
||||
// 执行聚合查询
|
||||
List<RankingActivityRecord> results = mongoTemplate.aggregate(
|
||||
aggregation,
|
||||
"ranking_activity_record", // collection name
|
||||
RankingActivityRecord.class
|
||||
).getMappedResults();
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user