Browse Source

修复财富排行榜分页问题

- 修复用户排名计算错误,使用绝对位置而非相对位置
- 使用缓存数据行数作为总数,确保分页信息准确
- 修复超出范围时的总数计算
- 删除不再使用的getTotalWealthRankCount方法
- 与房屋排行榜保持一致的修复方案
AI Assistant 6 tháng trước cách đây
mục cha
commit
39bf6f10c7
1 tập tin đã thay đổi với 6 bổ sung25 xóa
  1. 6 25
      app/Module/Farm/Logics/HouseLogic.php

+ 6 - 25
app/Module/Farm/Logics/HouseLogic.php

@@ -383,10 +383,12 @@ class HouseLogic
 
             // 如果请求的数据超出前100名,返回空结果
             if ($offset >= $maxRankLimit) {
+                // 先获取缓存数据来计算实际总数
+                $cachedRankList = $this->getWealthRankListCache();
                 return new WealthRankDto([], 0, 1, [
                     'page'     => $page,
                     'per_page' => $pageSize,
-                    'total'    => min($maxRankLimit, $this->getTotalWealthRankCount())
+                    'total'    => count($cachedRankList)
                 ]);
             }
             // 从缓存获取前100名数据
@@ -409,15 +411,15 @@ class HouseLogic
             $userRank = 0;
             foreach ($cachedRankList as $index => $item) {
                 if ($item->user_id == $userId) {
-                    $rank     = $offset + $index + 1;
-                    $userRank = $rank;
+                    $userRank = $index + 1; // 用户在整个排行榜中的排名
+                    break; // 找到后立即退出循环
                 }
             }
             // 构建分页信息
             $pageInfo = [
                 'page'     => $page,
                 'per_page' => $pageSize,
-                'total'    => min($maxRankLimit, $this->getTotalWealthRankCount())
+                'total'    => count($cachedRankList) // 直接使用缓存数据的行数
             ];
 //            dd($rankItems);
 
@@ -477,28 +479,7 @@ class HouseLogic
     }
 
 
-    /**
-     * 获取财富排行榜总数(限制前100名)
-     *
-     * @return int
-     */
-    private function getTotalWealthRankCount(): int
-    {
-        try {
-            // 获取实际用户总数,但最多返回100
-            $actualCount = DB::table('fund')
-                ->where('fund_id', 2) // 钻石资金类型
-                ->count();
 
-            return min($actualCount, 100);
-        } catch (\Exception $e) {
-            Log::error('获取财富排行榜总数失败', [
-                'error' => $e->getMessage()
-            ]);
-
-            return 0;
-        }
-    }
 
     /**
      * 清除房屋配置缓存