| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace App\Module\Farm\AdminControllers;
- use App\Module\Farm\Models\FarmDailyStats;
- use App\Module\Farm\AdminControllers\Metrics\FarmLandLevelStatsCard;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Widgets\Card;
- use Dcat\Admin\Admin;
- use Carbon\Carbon;
- /**
- * 农场统计图表控制器
- *
- * @AdminController(
- * title="农场统计图表",
- * permission="farm.metrics",
- * menu_title="统计图表",
- * menu_parent="农场管理",
- * menu_order=101
- * )
- */
- class FarmMetricsController extends AdminController
- {
- /**
- * 首页
- */
- public function index(Content $content)
- {
- return $content
- ->title('农场统计图表')
- ->description('农场模块数据统计分析')
- ->row(function ($row) {
- $row->column(6, new FarmLandLevelStatsCard());
- $row->column(6, $this->getLandStatsCards());
- })
- ->row($this->getLandTrendsChart())
- ->row($this->getHouseRankingCard());
- }
- /**
- * 获取土地等级统计卡片
- */
- protected function getLandStatsCards()
- {
- $latestStats = FarmDailyStats::orderBy('stats_date', 'desc')->first();
-
- if (!$latestStats) {
- return new Card('土地统计', '<div class="alert alert-warning">暂无统计数据</div>');
- }
- $landTypeNames = [
- 1 => '普通土地',
- 2 => '红土地',
- 3 => '黑土地',
- 4 => '金色特殊土地',
- 5 => '蓝色特殊土地',
- 6 => '紫色特殊土地',
- ];
- $cards = '<div class="row">';
-
- for ($type = 1; $type <= 6; $type++) {
- $field = "land_type_{$type}";
- $count = $latestStats->$field ?? 0;
- $typeName = $landTypeNames[$type];
-
- // 根据土地类型设置不同的颜色
- $colors = [
- 1 => 'bg-secondary', // 普通土地 - 灰色
- 2 => 'bg-danger', // 红土地 - 红色
- 3 => 'bg-dark', // 黑土地 - 黑色
- 4 => 'bg-warning', // 金色特殊土地 - 黄色
- 5 => 'bg-primary', // 蓝色特殊土地 - 蓝色
- 6 => 'bg-info', // 紫色特殊土地 - 紫色
- ];
-
- $color = $colors[$type];
-
- $cards .= "
- <div class='col-md-2'>
- <div class='info-box'>
- <span class='info-box-icon {$color}'><i class='fa fa-map'></i></span>
- <div class='info-box-content'>
- <span class='info-box-text'>{$typeName}</span>
- <span class='info-box-number'>{$count}块</span>
- </div>
- </div>
- </div>
- ";
- }
-
- $cards .= '</div>';
-
- return new Card('土地类型统计', $cards);
- }
- /**
- * 获取土地趋势图表
- */
- protected function getLandTrendsChart()
- {
- // 获取最近30天的统计数据
- $stats = FarmDailyStats::where('stats_date', '>=', Carbon::now()->subDays(30))
- ->orderBy('stats_date', 'asc')
- ->get();
- if ($stats->isEmpty()) {
- return new Card('土地趋势图', '<div class="alert alert-warning">暂无趋势数据</div>');
- }
- $dates = [];
- $landData = [
- 1 => [], // 普通土地
- 2 => [], // 红土地
- 3 => [], // 黑土地
- 4 => [], // 金色特殊土地
- 5 => [], // 蓝色特殊土地
- 6 => [], // 紫色特殊土地
- ];
- foreach ($stats as $stat) {
- $dates[] = $stat->stats_date->format('m-d');
-
- for ($type = 1; $type <= 6; $type++) {
- $field = "land_type_{$type}";
- $landData[$type][] = $stat->$field ?? 0;
- }
- }
- $landTypeNames = [
- 1 => '普通土地',
- 2 => '红土地',
- 3 => '黑土地',
- 4 => '金色特殊土地',
- 5 => '蓝色特殊土地',
- 6 => '紫色特殊土地',
- ];
- // 构建图表数据
- $series = [];
- $colors = ['#6c757d', '#dc3545', '#343a40', '#ffc107', '#007bff', '#17a2b8'];
-
- for ($type = 1; $type <= 6; $type++) {
- $series[] = [
- 'name' => $landTypeNames[$type],
- 'type' => 'line',
- 'data' => $landData[$type],
- 'itemStyle' => [
- 'color' => $colors[$type - 1]
- ]
- ];
- }
- $chartId = 'land-trends-chart';
- $chartScript = "
- <script>
- $(function() {
- var chart = echarts.init(document.getElementById('{$chartId}'));
- var option = {
- title: {
- text: '土地类型趋势图',
- left: 'center'
- },
- tooltip: {
- trigger: 'axis'
- },
- legend: {
- data: ['" . implode("','", array_values($landTypeNames)) . "'],
- top: 30
- },
- grid: {
- left: '3%',
- right: '4%',
- bottom: '3%',
- containLabel: true
- },
- xAxis: {
- type: 'category',
- boundaryGap: false,
- data: ['" . implode("','", $dates) . "']
- },
- yAxis: {
- type: 'value'
- },
- series: " . json_encode($series) . "
- };
- chart.setOption(option);
-
- // 响应式
- window.addEventListener('resize', function() {
- chart.resize();
- });
- });
- </script>
- ";
- $chartHtml = "
- <div id='{$chartId}' style='height: 400px;'></div>
- {$chartScript}
- ";
- // 添加ECharts库
- Admin::script('https://cdn.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.min.js');
- return new Card('土地类型趋势图', $chartHtml);
- }
- /**
- * 获取房屋等级排名卡片
- */
- protected function getHouseRankingCard()
- {
- $latestStats = FarmDailyStats::orderBy('stats_date', 'desc')->first();
-
- if (!$latestStats) {
- return new Card('房屋等级排名', '<div class="alert alert-warning">暂无统计数据</div>');
- }
- $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,
- 'ratio' => 0 // 这里可以计算占比
- ];
- }
- }
- // 按数量排序
- 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;
- }
- $rankingHtml = '<div class="table-responsive">';
- $rankingHtml .= '<table class="table table-striped">';
- $rankingHtml .= '<thead><tr><th>排名</th><th>房屋等级</th><th>数量</th><th>占比</th></tr></thead>';
- $rankingHtml .= '<tbody>';
- if (empty($houseData)) {
- $rankingHtml .= '<tr><td colspan="4" class="text-center text-muted">暂无数据</td></tr>';
- } else {
- foreach ($houseData as $index => $item) {
- $rank = $index + 1;
- $rankingHtml .= "<tr>";
- $rankingHtml .= "<td><span class='badge badge-primary'>{$rank}</span></td>";
- $rankingHtml .= "<td>{$item['label']}</td>";
- $rankingHtml .= "<td><strong>{$item['value']}个</strong></td>";
- $rankingHtml .= "<td>{$item['ratio']}%</td>";
- $rankingHtml .= "</tr>";
- }
- }
- $rankingHtml .= '</tbody></table></div>';
- return new Card('房屋等级排名', $rankingHtml);
- }
- }
|