WealthRankingCard.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * 基于RankfundHandler的逻辑实现
  12. */
  13. class WealthRankingCard 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->getWealthRankingData($timeRange);
  38. // 卡片内容
  39. $this->withContent($data);
  40. }
  41. /**
  42. * 获取财富排行榜数据
  43. *
  44. * @param string $timeRange 时间范围
  45. * @return array
  46. */
  47. protected function getWealthRankingData(string $timeRange): array
  48. {
  49. try {
  50. $houseLogic = new HouseLogic();
  51. // 使用public方法获取财富排行榜数据,传入虚拟用户ID
  52. $rankDto = $houseLogic->getWealthRankList(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. $balance = $item->wealth ?? 0;
  60. $houseLevel = $item->houseLevel ?? 0;
  61. // 格式化钻石数量显示
  62. $formattedBalance = number_format($balance);
  63. $rankingData[] = [
  64. 'rank' => $rank,
  65. 'title' => "ID:{$userId} {$nickname}",
  66. 'number' => "{$formattedBalance}钻石 ({$houseLevel}级房屋)",
  67. ];
  68. }
  69. return $rankingData;
  70. } catch (\Exception $e) {
  71. Log::error('获取财富排行榜数据失败', [
  72. 'time_range' => $timeRange,
  73. 'error' => $e->getMessage(),
  74. 'trace' => $e->getTraceAsString()
  75. ]);
  76. return [];
  77. }
  78. }
  79. }