| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Module\Farm\AdminControllers\Metrics;
- use UCore\DcatAdmin\Metrics\Examples\Ranking;
- use Illuminate\Http\Request;
- use App\Module\Farm\Models\FarmDailyStats;
- /**
- * 农场房屋等级排名卡片
- *
- * 参考UCore\DcatAdmin\Metrics\Examples\Ranking实现
- * 显示房屋等级的排名统计
- */
- class FarmHouseRanking extends Ranking
- {
- /**
- * 初始化卡片内容
- *
- * @return void
- */
- protected function init()
- {
- parent::init();
- $this->title('房屋等级排名(昨天)');
- $this->dropdown([
- 'count' => '按数量排序',
- 'level' => '按等级排序',
- ]);
- }
- /**
- * 获取排名数据
- *
- * @param string $option
- *
- * @return array
- */
- protected function getData($option)
- {
- // 获取最新的统计数据
- $latestStats = FarmDailyStats::orderBy('stats_date', 'desc')->first();
- if (!$latestStats) {
- return [];
- }
- $houseData = [];
- // 收集房屋数据
- for ($level = 1; $level <= 10; $level++) {
- $field = "house_level_{$level}";
- $count = $latestStats->$field ?? 0;
- if ($count > 0) {
- $houseData[] = [
- 'level' => $level,
- 'label' => "{$level}级房屋",
- 'value' => $count,
- ];
- }
- }
- // 根据选择的排序方式排序
- if ($option === 'level') {
- // 按等级排序
- usort($houseData, function($a, $b) {
- return $a['level'] - $b['level'];
- });
- } else {
- // 按数量排序(默认)
- usort($houseData, function($a, $b) {
- return $b['value'] - $a['value'];
- });
- }
- // 计算占比
- $total = array_sum(array_column($houseData, 'value'));
- foreach ($houseData as &$item) {
- $item['ratio'] = $total > 0 ? round($item['value'] / $total * 100, 1) : 0;
- }
- // 转换为Ranking组件需要的格式
- $rankingData = [];
- foreach ($houseData as $index => $item) {
- $rankingData[] = [
- 'rank' => ($index + 1),
- 'title' => $item['label'],
- 'number' => $item['value'] . '个 (' . $item['ratio'] . '%)',
- ];
- }
- return $rankingData;
- }
- }
|