| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?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名
- * 基于RankHandler的逻辑实现
- */
- class HouseRankingCard 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->getHouseRankingData($timeRange);
-
- // 卡片内容
- $this->withContent($data);
- }
- /**
- * 获取房屋排行榜数据
- *
- * @param string $timeRange 时间范围
- * @return array
- */
- protected function getHouseRankingData(string $timeRange): array
- {
- try {
- $houseLogic = new HouseLogic();
- // 使用public方法获取排行榜数据,传入虚拟用户ID
- $rankDto = $houseLogic->getHouseRankList(0, 1, 20);
- // 转换为Ranking组件需要的格式
- $rankingData = [];
- foreach ($rankDto->list as $index => $item) {
- $rank = $index + 1;
- $userId = $item->userId ?? 0;
- $nickname = $item->nickname ?: "用户{$userId}";
- $houseLevel = $item->level ?? 0; // 使用正确的字段名
- $balance = $item->wealth ?? 0; // 使用正确的字段名
- $rankingData[] = [
- 'rank' => $rank,
- 'title' => "ID:{$userId} {$nickname}",
- 'number' => "{$houseLevel}级房屋 (钻石:{$balance})",
- ];
- }
- return $rankingData;
- } catch (\Exception $e) {
- Log::error('获取房屋排行榜数据失败', [
- 'time_range' => $timeRange,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [];
- }
- }
- }
|