|
|
@@ -269,24 +269,48 @@ class HouseLogic
|
|
|
public function getHouseRankList(int $userId, int $page = 1, int $pageSize = 20): HouseRankDto
|
|
|
{
|
|
|
try {
|
|
|
- // 计算偏移量
|
|
|
+ // 限制只显示前100名
|
|
|
+ $maxRankLimit = 100;
|
|
|
$offset = ($page - 1) * $pageSize;
|
|
|
|
|
|
- // 查询排行榜数据,按房屋等级降序排列
|
|
|
- $rankList = DB::table('farm_users as fu')
|
|
|
- ->join('users as u', 'fu.user_id', '=', 'u.id')
|
|
|
- ->select([
|
|
|
- 'fu.user_id',
|
|
|
- 'fu.house_level',
|
|
|
- 'u.username as nickname', // 使用username作为昵称
|
|
|
- 'fu.last_upgrade_time'
|
|
|
- ])
|
|
|
- ->orderBy('fu.house_level', 'desc')
|
|
|
- ->orderBy('fu.last_upgrade_time', 'asc') // 同等级按升级时间排序
|
|
|
- ->offset($offset)
|
|
|
- ->limit($pageSize)
|
|
|
- ->get()
|
|
|
- ->toArray();
|
|
|
+ // 如果请求的数据超出前100名,返回空结果
|
|
|
+ if ($offset >= $maxRankLimit) {
|
|
|
+ return new HouseRankDto([], 0, 1, [
|
|
|
+ 'page' => $page,
|
|
|
+ 'per_page' => $pageSize,
|
|
|
+ 'total' => min($maxRankLimit, $this->getTotalHouseRankCount())
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 调整查询限制,确保不超过前100名
|
|
|
+ $actualLimit = min($pageSize, $maxRankLimit - $offset);
|
|
|
+
|
|
|
+ // 尝试从缓存获取排行榜数据
|
|
|
+ $cacheKey = $this->cachePrefix . 'house_rank:top100';
|
|
|
+ $cachedRankList = Cache::get($cacheKey);
|
|
|
+
|
|
|
+ if (!$cachedRankList) {
|
|
|
+ // 查询前100名排行榜数据,按房屋等级降序排列
|
|
|
+ $cachedRankList = DB::table('farm_users as fu')
|
|
|
+ ->join('users as u', 'fu.user_id', '=', 'u.id')
|
|
|
+ ->select([
|
|
|
+ 'fu.user_id',
|
|
|
+ 'fu.house_level',
|
|
|
+ 'u.username as nickname', // 使用username作为昵称
|
|
|
+ 'fu.last_upgrade_time'
|
|
|
+ ])
|
|
|
+ ->orderBy('fu.house_level', 'desc')
|
|
|
+ ->orderBy('fu.last_upgrade_time', 'asc') // 同等级按升级时间排序
|
|
|
+ ->limit($maxRankLimit)
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ // 缓存10分钟
|
|
|
+ Cache::put($cacheKey, $cachedRankList, 600);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从缓存数据中获取当前页的数据
|
|
|
+ $rankList = array_slice($cachedRankList, $offset, $actualLimit);
|
|
|
|
|
|
// 转换为DTO对象
|
|
|
$rankItems = [];
|
|
|
@@ -302,7 +326,7 @@ class HouseLogic
|
|
|
$pageInfo = [
|
|
|
'page' => $page,
|
|
|
'per_page' => $pageSize,
|
|
|
- 'total' => $this->getTotalHouseRankCount()
|
|
|
+ 'total' => min($maxRankLimit, $this->getTotalHouseRankCount())
|
|
|
];
|
|
|
|
|
|
return new HouseRankDto($rankItems, $userRank, 1, $pageInfo);
|
|
|
@@ -324,14 +348,23 @@ class HouseLogic
|
|
|
* 获取用户的房屋排名
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
- * @return int 排名,0表示未上榜
|
|
|
+ * @return int 排名,0表示未上榜或超出前100名
|
|
|
*/
|
|
|
private function getUserHouseRank(int $userId): int
|
|
|
{
|
|
|
try {
|
|
|
+ // 尝试从缓存获取用户排名
|
|
|
+ $cacheKey = $this->cachePrefix . 'user_house_rank:' . $userId;
|
|
|
+ $cachedRank = Cache::get($cacheKey);
|
|
|
+
|
|
|
+ if ($cachedRank !== null) {
|
|
|
+ return $cachedRank;
|
|
|
+ }
|
|
|
+
|
|
|
// 获取用户的房屋等级
|
|
|
$userFarm = FarmUser::where('user_id', $userId)->first();
|
|
|
if (!$userFarm) {
|
|
|
+ Cache::put($cacheKey, 0, 600); // 缓存10分钟
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -349,7 +382,17 @@ class HouseLogic
|
|
|
})
|
|
|
->count();
|
|
|
|
|
|
- return $rank + 1;
|
|
|
+ $finalRank = $rank + 1;
|
|
|
+
|
|
|
+ // 如果用户排名超出前100名,返回0表示未上榜
|
|
|
+ if ($finalRank > 100) {
|
|
|
+ $finalRank = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缓存用户排名10分钟
|
|
|
+ Cache::put($cacheKey, $finalRank, 600);
|
|
|
+
|
|
|
+ return $finalRank;
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取用户房屋排名失败', [
|
|
|
@@ -362,14 +405,16 @@ class HouseLogic
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取房屋排行榜总数
|
|
|
+ * 获取房屋排行榜总数(限制前100名)
|
|
|
*
|
|
|
* @return int
|
|
|
*/
|
|
|
private function getTotalHouseRankCount(): int
|
|
|
{
|
|
|
try {
|
|
|
- return FarmUser::count();
|
|
|
+ // 获取实际用户总数,但最多返回100
|
|
|
+ $actualCount = FarmUser::count();
|
|
|
+ return min($actualCount, 100);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取房屋排行榜总数失败', [
|
|
|
'error' => $e->getMessage()
|