| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\Module\Pet\AdminControllers;
- use App\Module\Pet\Models\PetBattleSeason;
- use App\Module\Pet\Repositorys\PetBattleSeasonRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- use App\Module\Pet\AdminControllers\Helper\FilterHelper;
- use App\Module\Pet\AdminControllers\Helper\FormHelper;
- use App\Module\Pet\AdminControllers\Helper\GridHelper;
- use App\Module\Pet\AdminControllers\Helper\ShowHelper;
- /**
- * 宠物争霸赛赛季控制器
- *
- * @package App\Module\Pet\AdminControllers
- */
- #[Resource('pet-battle-seasons', names: 'dcat.admin.pet-battle-seasons')]
- class PetBattleSeasonController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '宠物争霸赛赛季';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new PetBattleSeasonRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $grid->column('id', 'ID')->sortable();
- $grid->column('name', '赛季名称');
- $grid->column('start_time', '开始时间')->sortable();
- $grid->column('end_time', '结束时间')->sortable();
- $grid->column('boss_power', 'Boss战力')->sortable();
- $grid->column('status', '状态')->display(function ($value) {
- $statuses = [
- PetBattleSeason::STATUS_NOT_STARTED => '<span class="label bg-info">未开始</span>',
- PetBattleSeason::STATUS_IN_PROGRESS => '<span class="label bg-success">进行中</span>',
- PetBattleSeason::STATUS_ENDED => '<span class="label bg-default">已结束</span>',
- ];
- return $statuses[$value] ?? '未知';
- })->label();
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 添加行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- // 添加查看队伍按钮
- $actions->append('<a href="' . admin_url('pet-battle-teams?season_id=' . $actions->getKey()) . '" class="btn btn-sm btn-primary">查看队伍</a>');
- });
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $filter->like('name', '赛季名称');
- $filter->equal('status', '状态')->select([
- PetBattleSeason::STATUS_NOT_STARTED => '未开始',
- PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
- PetBattleSeason::STATUS_ENDED => '已结束',
- ]);
- $filter->between('start_time', '开始时间')->datetime();
- $filter->between('end_time', '结束时间')->datetime();
- });
- // 默认按开始时间倒序排序
- $grid->model()->orderBy('start_time', 'desc');
- return $grid;
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new PetBattleSeasonRepository(['teams']), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- $show->field('name', '赛季名称');
- $show->field('start_time', '开始时间');
- $show->field('end_time', '结束时间');
- $show->field('boss_power', 'Boss战力');
- $show->field('reward_pool', '奖池配置')->json();
- $show->field('status', '状态')->as(function ($value) {
- $statuses = [
- PetBattleSeason::STATUS_NOT_STARTED => '未开始',
- PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
- PetBattleSeason::STATUS_ENDED => '已结束',
- ];
- return $statuses[$value] ?? '未知';
- });
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 显示赛季队伍
- $show->teams('参赛队伍', function ($teams) {
- $teams->resource('/admin/pet-battle-teams');
- $teams->id('ID');
- $teams->name('队伍名称');
- $teams->leader_id('队长ID');
- $teams->total_power('队伍总战力');
- $teams->member_count('成员数量');
- $teams->created_at('创建时间');
- });
- return $show;
- });
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new PetBattleSeasonRepository(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
-
- $form->text('name', '赛季名称')
- ->required()
- ->help('赛季的名称,如"第一赛季"、"松狮争霸赛2023春季"等');
-
- $form->datetime('start_time', '开始时间')
- ->required();
-
- $form->datetime('end_time', '结束时间')
- ->required();
-
- $form->number('boss_power', 'Boss战力')
- ->min(1000)
- ->default(10000)
- ->required()
- ->help('Boss的战力值,决定了挑战的难度');
-
- $helper->embedsCats('reward_pool', '奖池配置')
- ->help('赛季奖池配置,JSON格式');
-
- $form->select('status', '状态')
- ->options([
- PetBattleSeason::STATUS_NOT_STARTED => '未开始',
- PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
- PetBattleSeason::STATUS_ENDED => '已结束',
- ])
- ->default(PetBattleSeason::STATUS_NOT_STARTED)
- ->required();
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 保存前回调
- $form->saving(function (Form $form) {
- // 验证开始时间必须早于结束时间
- if ($form->start_time && $form->end_time) {
- $startTime = strtotime($form->start_time);
- $endTime = strtotime($form->end_time);
-
- if ($startTime >= $endTime) {
- return $form->error('开始时间必须早于结束时间');
- }
- }
- });
- return $form;
- });
- }
- }
|