HouseRankingCard.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Module\Farm\AdminControllers\Metrics;
  3. use UCore\DcatAdmin\Metrics\Examples\Ranking;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Log;
  6. use App\Module\Farm\Logics\HouseLogic;
  7. /**
  8. * 房屋等级排行榜卡片
  9. *
  10. * 显示用户房屋等级排行榜前100名
  11. * 基于RankHandler的逻辑实现
  12. */
  13. class HouseRankingCard extends Ranking
  14. {
  15. /**
  16. * 初始化卡片内容
  17. */
  18. protected function init()
  19. {
  20. parent::init();
  21. $this->title('房屋等级排行榜');
  22. $this->dropdown([
  23. 'all' => '全部时间',
  24. '30' => '最近 30 天',
  25. '7' => '最近 7 天',
  26. ]);
  27. }
  28. /**
  29. * 处理请求
  30. *
  31. * @param Request $request
  32. * @return mixed|void
  33. */
  34. public function handle(Request $request)
  35. {
  36. $timeRange = $request->get('option', 'all');
  37. $data = $this->getHouseRankingData($timeRange);
  38. // 卡片内容
  39. $this->withContent($data);
  40. }
  41. /**
  42. * 获取房屋排行榜数据
  43. *
  44. * @param string $timeRange 时间范围
  45. * @return array
  46. */
  47. protected function getHouseRankingData(string $timeRange): array
  48. {
  49. try {
  50. $houseLogic = new HouseLogic();
  51. // 使用public方法获取排行榜数据,传入虚拟用户ID
  52. $rankDto = $houseLogic->getHouseRankList(0, 1, 20);
  53. // 转换为Ranking组件需要的格式
  54. $rankingData = [];
  55. foreach ($rankDto->list as $index => $item) {
  56. $rank = $index + 1;
  57. $userId = $item->userId ?? 0;
  58. $nickname = $item->nickname ?: "用户{$userId}";
  59. $houseLevel = $item->level ?? 0; // 使用正确的字段名
  60. $balance = $item->wealth ?? 0; // 使用正确的字段名
  61. $rankingData[] = [
  62. 'rank' => $rank,
  63. 'title' => "ID:{$userId} {$nickname}",
  64. 'number' => "{$houseLevel}级房屋 (钻石:{$balance})",
  65. ];
  66. }
  67. return $rankingData;
  68. } catch (\Exception $e) {
  69. Log::error('获取房屋排行榜数据失败', [
  70. 'time_range' => $timeRange,
  71. 'error' => $e->getMessage(),
  72. 'trace' => $e->getTraceAsString()
  73. ]);
  74. return [];
  75. }
  76. }
  77. }