| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- namespace App\Module\Farm\AdminControllers;
- use App\Module\Farm\Models\FarmDailyStats;
- use UCore\DcatAdmin\AdminController;
- use UCore\DcatAdmin\GridHelper;
- use UCore\DcatAdmin\ShowHelper;
- use UCore\DcatAdmin\FormHelper;
- use UCore\DcatAdmin\FilterHelper;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Form;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Widgets\Card;
- use Dcat\Admin\Widgets\Alert;
- use Carbon\Carbon;
- /**
- * 农场每日统计后台控制器
- *
- * @AdminController(
- * title="农场每日统计",
- * permission="farm.daily-stats",
- * menu_title="每日统计",
- * menu_parent="农场管理",
- * menu_order=100
- * )
- */
- class FarmDailyStatsController extends AdminController
- {
- /**
- * 列表页面
- */
- protected function grid(): Grid
- {
- return Grid::make(new FarmDailyStats(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $grid->model()->orderBy('stats_date', 'desc');
- $grid->column('id', 'ID')->sortable();
- $grid->column('stats_date', '统计日期')->sortable();
- $grid->column('total_users', '总用户数')->sortable();
- $grid->column('active_users', '活跃用户数')->sortable();
-
- // 房屋等级统计(显示前5级)
- $grid->column('house_stats', '房屋等级统计')->display(function () {
- $html = '';
- for ($level = 1; $level <= 5; $level++) {
- $field = "house_level_{$level}";
- $count = $this->$field ?? 0;
- if ($count > 0) {
- $html .= "<div>{$level}级: {$count}个</div>";
- }
- }
- return $html ?: '<span class="text-muted">无数据</span>';
- });
-
- // 土地类型统计
- $grid->column('land_type_stats', '土地类型统计')->display(function () {
- $typeNames = [
- 1 => '普通',
- 2 => '红土',
- 3 => '黑土',
- 4 => '金土',
- 5 => '蓝土',
- 6 => '紫土',
- ];
-
- $html = '';
- for ($type = 1; $type <= 6; $type++) {
- $field = "land_type_{$type}";
- $count = $this->$field ?? 0;
- if ($count > 0) {
- $typeName = $typeNames[$type];
- $html .= "<div>{$typeName}: {$count}块</div>";
- }
- }
- return $html ?: '<span class="text-muted">无数据</span>';
- });
-
- $grid->column('total_lands', '总土地数')->sortable();
- $grid->column('total_special_lands', '特殊土地数')->sortable();
- $grid->column('total_crops', '总作物数')->sortable();
- // 作物生长阶段统计
- $grid->column('crop_stage_stats', '作物生长阶段统计')->display(function () {
- $stageNames = [
- 1 => '种子期',
- 20 => '发芽期',
- 30 => '生长期',
- 35 => '果实期',
- 40 => '成熟期',
- 50 => '枯萎期',
- ];
- $stageFields = [
- 1 => 'crops_seed_stage',
- 20 => 'crops_sprout_stage',
- 30 => 'crops_growth_stage',
- 35 => 'crops_fruit_stage',
- 40 => 'crops_mature_stage',
- 50 => 'crops_withered_stage',
- ];
- $html = '';
- foreach ($stageFields as $stageValue => $field) {
- $count = $this->$field ?? 0;
- if ($count > 0) {
- $stageName = $stageNames[$stageValue];
- $html .= "<div>{$stageName}: {$count}个</div>";
- }
- }
- return $html ?: '<span class="text-muted">无数据</span>';
- });
- $grid->column('total_disasters', '总灾害数')->sortable();
-
- $helper->columnCreatedAt();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->between('stats_date', '统计日期')->date();
- $filter->equal('total_users', '总用户数');
- $filter->equal('active_users', '活跃用户数');
- });
- // 禁用创建按钮(统计数据由命令生成)
- $grid->disableCreateButton();
- // 禁用批量删除
- $grid->disableBatchActions();
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableEdit(); // 禁用编辑
- });
- });
- }
- /**
- * 详情页面
- */
- protected function detail($id): Show
- {
- return Show::make($id, new FarmDailyStats(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $show->field('id', 'ID');
- $show->field('stats_date', '统计日期');
- $show->field('total_users', '总用户数');
- $show->field('active_users', '活跃用户数');
-
- // 房屋等级统计
- $show->divider('房屋等级统计');
- for ($level = 1; $level <= 10; $level++) {
- $field = "house_level_{$level}";
- $show->field($field, "{$level}级房屋数量");
- }
- // 土地类型统计
- $show->divider('土地类型统计');
- $landTypeNames = [
- 1 => '普通土地数量',
- 2 => '红土地数量',
- 3 => '黑土地数量',
- 4 => '金色特殊土地数量',
- 5 => '蓝色特殊土地数量',
- 6 => '紫色特殊土地数量',
- ];
-
- for ($type = 1; $type <= 6; $type++) {
- $field = "land_type_{$type}";
- $show->field($field, $landTypeNames[$type]);
- }
-
- // 土地状态统计
- $show->divider('土地状态统计');
- $landStatusNames = [
- 0 => '空闲土地数量',
- 1 => '种植中土地数量',
- 2 => '灾害土地数量',
- 3 => '可收获土地数量',
- 4 => '枯萎土地数量',
- ];
-
- for ($status = 0; $status <= 4; $status++) {
- $field = "land_status_{$status}";
- $show->field($field, $landStatusNames[$status]);
- }
-
- // 总计统计
- $show->divider('总计统计');
- $show->field('total_lands', '总土地数量');
- $show->field('total_special_lands', '特殊土地总数量');
- $show->field('total_crops', '总作物数量');
- $show->field('total_disasters', '总灾害数量');
-
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 表单页面(禁用)
- */
- protected function form(): Form
- {
- return Form::make(new FarmDailyStats(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
- $form->date('stats_date', '统计日期')->required();
-
- // 基础统计
- $form->number('total_users', '总用户数')->required();
- $form->number('active_users', '活跃用户数')->required();
-
- // 房屋等级统计
- $form->tab('房屋等级统计', function (Form $form) {
- for ($level = 1; $level <= 10; $level++) {
- $form->number("house_level_{$level}", "{$level}级房屋数量")->default(0);
- }
- });
-
- // 土地类型统计
- $form->tab('土地类型统计', function (Form $form) {
- $landTypeNames = [
- 1 => '普通土地数量',
- 2 => '红土地数量',
- 3 => '黑土地数量',
- 4 => '金色特殊土地数量',
- 5 => '蓝色特殊土地数量',
- 6 => '紫色特殊土地数量',
- ];
-
- for ($type = 1; $type <= 6; $type++) {
- $form->number("land_type_{$type}", $landTypeNames[$type])->default(0);
- }
- });
-
- // 土地状态统计
- $form->tab('土地状态统计', function (Form $form) {
- $landStatusNames = [
- 0 => '空闲土地数量',
- 1 => '种植中土地数量',
- 2 => '灾害土地数量',
- 3 => '可收获土地数量',
- 4 => '枯萎土地数量',
- ];
-
- for ($status = 0; $status <= 4; $status++) {
- $form->number("land_status_{$status}", $landStatusNames[$status])->default(0);
- }
- });
-
- // 总计统计
- $form->tab('总计统计', function (Form $form) {
- $form->number('total_lands', '总土地数量')->default(0);
- $form->number('total_special_lands', '特殊土地总数量')->default(0);
- $form->number('total_crops', '总作物数量')->default(0);
- $form->number('total_disasters', '总灾害数量')->default(0);
- });
- // 作物生长阶段统计
- $form->tab('作物生长阶段统计', function (Form $form) {
- $form->number('crops_seed_stage', '种子期作物数量')->default(0);
- $form->number('crops_sprout_stage', '发芽期作物数量')->default(0);
- $form->number('crops_growth_stage', '生长期作物数量')->default(0);
- $form->number('crops_fruit_stage', '果实期作物数量')->default(0);
- $form->number('crops_mature_stage', '成熟期作物数量')->default(0);
- $form->number('crops_withered_stage', '枯萎期作物数量')->default(0);
- });
- });
- }
- /**
- * 首页概览
- */
- public function index(Content $content)
- {
- return $content
- ->title('农场每日统计')
- ->description('查看农场模块的每日统计数据')
- ->body($this->grid())
- ->body($this->getStatsCards());
- }
- /**
- * 获取统计卡片
- */
- protected function getStatsCards()
- {
- $latestStats = FarmDailyStats::orderBy('stats_date', 'desc')->first();
- if (!$latestStats) {
- return new Alert('warning', '暂无统计数据,请先运行统计命令生成数据');
- }
- $content = "
- <div class='row'>
- <div class='col-md-3'>
- <div class='info-box'>
- <span class='info-box-icon bg-info'><i class='fa fa-calendar'></i></span>
- <div class='info-box-content'>
- <span class='info-box-text'>最新统计日期</span>
- <span class='info-box-number'>{$latestStats->stats_date->format('Y-m-d')}</span>
- </div>
- </div>
- </div>
- <div class='col-md-3'>
- <div class='info-box'>
- <span class='info-box-icon bg-success'><i class='fa fa-users'></i></span>
- <div class='info-box-content'>
- <span class='info-box-text'>总用户数</span>
- <span class='info-box-number'>{$latestStats->total_users}</span>
- </div>
- </div>
- </div>
- <div class='col-md-3'>
- <div class='info-box'>
- <span class='info-box-icon bg-warning'><i class='fa fa-user'></i></span>
- <div class='info-box-content'>
- <span class='info-box-text'>活跃用户数</span>
- <span class='info-box-number'>{$latestStats->active_users}</span>
- </div>
- </div>
- </div>
- <div class='col-md-3'>
- <div class='info-box'>
- <span class='info-box-icon bg-danger'><i class='fa fa-map'></i></span>
- <div class='info-box-content'>
- <span class='info-box-text'>总土地数</span>
- <span class='info-box-number'>{$latestStats->total_lands}</span>
- </div>
- </div>
- </div>
- </div>
- ";
- return new Card('农场统计概览', $content);
- }
- }
|