| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Module\Farm\AdminControllers\Metrics;
- use UCore\DcatAdmin\Metrics\Examples\Ranking;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use App\Module\Farm\Logics\HouseLogic;
- /**
- * 财富排行榜卡片
- *
- * 显示用户钻石财富排行榜前100名
- * 基于RankfundHandler的逻辑实现
- */
- class WealthRankingCard extends Ranking
- {
- /**
- * 初始化卡片内容
- */
- protected function init()
- {
- parent::init();
-
- $this->title('财富排行榜');
- $this->dropdown([
- 'all' => '全部时间',
- '30' => '最近 30 天',
- '7' => '最近 7 天',
- ]);
- }
- /**
- * 处理请求
- *
- * @param Request $request
- * @return mixed|void
- */
- public function handle(Request $request)
- {
- $timeRange = $request->get('option', 'all');
- $data = $this->getWealthRankingData($timeRange);
-
- // 卡片内容
- $this->withContent($data);
- }
- /**
- * 获取财富排行榜数据
- *
- * @param string $timeRange 时间范围
- * @return array
- */
- protected function getWealthRankingData(string $timeRange): array
- {
- try {
- $houseLogic = new HouseLogic();
- // 使用public方法获取财富排行榜数据,传入虚拟用户ID
- $rankDto = $houseLogic->getWealthRankList(0, 1, 20);
- // 转换为Ranking组件需要的格式
- $rankingData = [];
- foreach ($rankDto->list as $index => $item) {
- $rank = $index + 1;
- $userId = $item->userId ?? 0;
- $nickname = $item->nickname ?: "用户{$userId}";
- $balance = $item->wealth ?? 0;
- $houseLevel = $item->houseLevel ?? 0;
- // 格式化钻石数量显示
- $formattedBalance = number_format($balance);
- $rankingData[] = [
- 'rank' => $rank,
- 'title' => "ID:{$userId} {$nickname}",
- 'number' => "{$formattedBalance}钻石 ({$houseLevel}级房屋)",
- ];
- }
- return $rankingData;
- } catch (\Exception $e) {
- Log::error('获取财富排行榜数据失败', [
- 'time_range' => $timeRange,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [];
- }
- }
- }
|