PetBattleSeasonController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Module\Pet\AdminControllers;
  3. use App\Module\Pet\Models\PetBattleSeason;
  4. use App\Module\Pet\Repositorys\PetBattleSeasonRepository;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use UCore\DcatAdmin\AdminController;
  9. use Spatie\RouteAttributes\Attributes\Resource;
  10. use App\Module\Pet\AdminControllers\Helper\FilterHelper;
  11. use App\Module\Pet\AdminControllers\Helper\FormHelper;
  12. use App\Module\Pet\AdminControllers\Helper\GridHelper;
  13. use App\Module\Pet\AdminControllers\Helper\ShowHelper;
  14. /**
  15. * 宠物争霸赛赛季控制器
  16. *
  17. * @package App\Module\Pet\AdminControllers
  18. */
  19. #[Resource('pet-battle-seasons', names: 'dcat.admin.pet-battle-seasons')]
  20. class PetBattleSeasonController extends AdminController
  21. {
  22. /**
  23. * 标题
  24. *
  25. * @var string
  26. */
  27. protected $title = '宠物争霸赛赛季';
  28. /**
  29. * 列表页
  30. *
  31. * @return Grid
  32. */
  33. protected function grid()
  34. {
  35. return Grid::make(new PetBattleSeasonRepository(), function (Grid $grid) {
  36. $helper = new GridHelper($grid, $this);
  37. $grid->column('id', 'ID')->sortable();
  38. $grid->column('name', '赛季名称');
  39. $grid->column('start_time', '开始时间')->sortable();
  40. $grid->column('end_time', '结束时间')->sortable();
  41. $grid->column('boss_power', 'Boss战力')->sortable();
  42. $grid->column('status', '状态')->display(function ($value) {
  43. $statuses = [
  44. PetBattleSeason::STATUS_NOT_STARTED => '<span class="label bg-info">未开始</span>',
  45. PetBattleSeason::STATUS_IN_PROGRESS => '<span class="label bg-success">进行中</span>',
  46. PetBattleSeason::STATUS_ENDED => '<span class="label bg-default">已结束</span>',
  47. ];
  48. return $statuses[$value] ?? '未知';
  49. })->label();
  50. $grid->column('created_at', '创建时间');
  51. $grid->column('updated_at', '更新时间');
  52. // 添加行操作
  53. $grid->actions(function (Grid\Displayers\Actions $actions) {
  54. // 添加查看队伍按钮
  55. $actions->append('<a href="' . admin_url('pet-battle-teams?season_id=' . $actions->getKey()) . '" class="btn btn-sm btn-primary">查看队伍</a>');
  56. });
  57. // 筛选
  58. $grid->filter(function ($filter) {
  59. $helper = new FilterHelper($filter, $this);
  60. $helper->equal('id', 'ID');
  61. $filter->like('name', '赛季名称');
  62. $filter->equal('status', '状态')->select([
  63. PetBattleSeason::STATUS_NOT_STARTED => '未开始',
  64. PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
  65. PetBattleSeason::STATUS_ENDED => '已结束',
  66. ]);
  67. $filter->between('start_time', '开始时间')->datetime();
  68. $filter->between('end_time', '结束时间')->datetime();
  69. });
  70. // 默认按开始时间倒序排序
  71. $grid->model()->orderBy('start_time', 'desc');
  72. return $grid;
  73. });
  74. }
  75. /**
  76. * 详情页
  77. *
  78. * @param mixed $id
  79. * @return Show
  80. */
  81. protected function detail($id)
  82. {
  83. return Show::make($id, new PetBattleSeasonRepository(['teams']), function (Show $show) {
  84. $helper = new ShowHelper($show, $this);
  85. $helper->field('id', 'ID');
  86. $show->field('name', '赛季名称');
  87. $show->field('start_time', '开始时间');
  88. $show->field('end_time', '结束时间');
  89. $show->field('boss_power', 'Boss战力');
  90. $show->field('reward_pool', '奖池配置')->json();
  91. $show->field('status', '状态')->as(function ($value) {
  92. $statuses = [
  93. PetBattleSeason::STATUS_NOT_STARTED => '未开始',
  94. PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
  95. PetBattleSeason::STATUS_ENDED => '已结束',
  96. ];
  97. return $statuses[$value] ?? '未知';
  98. });
  99. $show->field('created_at', '创建时间');
  100. $show->field('updated_at', '更新时间');
  101. // 显示赛季队伍
  102. $show->teams('参赛队伍', function ($teams) {
  103. $teams->resource('/admin/pet-battle-teams');
  104. $teams->id('ID');
  105. $teams->name('队伍名称');
  106. $teams->leader_id('队长ID');
  107. $teams->total_power('队伍总战力');
  108. $teams->member_count('成员数量');
  109. $teams->created_at('创建时间');
  110. });
  111. return $show;
  112. });
  113. }
  114. /**
  115. * 表单
  116. *
  117. * @return Form
  118. */
  119. protected function form()
  120. {
  121. return Form::make(new PetBattleSeasonRepository(), function (Form $form) {
  122. $helper = new FormHelper($form, $this);
  123. $form->display('id', 'ID');
  124. $form->text('name', '赛季名称')
  125. ->required()
  126. ->help('赛季的名称,如"第一赛季"、"松狮争霸赛2023春季"等');
  127. $form->datetime('start_time', '开始时间')
  128. ->required();
  129. $form->datetime('end_time', '结束时间')
  130. ->required();
  131. $form->number('boss_power', 'Boss战力')
  132. ->min(1000)
  133. ->default(10000)
  134. ->required()
  135. ->help('Boss的战力值,决定了挑战的难度');
  136. $helper->embedsCats('reward_pool', '奖池配置')
  137. ->help('赛季奖池配置,JSON格式');
  138. $form->select('status', '状态')
  139. ->options([
  140. PetBattleSeason::STATUS_NOT_STARTED => '未开始',
  141. PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
  142. PetBattleSeason::STATUS_ENDED => '已结束',
  143. ])
  144. ->default(PetBattleSeason::STATUS_NOT_STARTED)
  145. ->required();
  146. $form->display('created_at', '创建时间');
  147. $form->display('updated_at', '更新时间');
  148. // 保存前回调
  149. $form->saving(function (Form $form) {
  150. // 验证开始时间必须早于结束时间
  151. if ($form->start_time && $form->end_time) {
  152. $startTime = strtotime($form->start_time);
  153. $endTime = strtotime($form->end_time);
  154. if ($startTime >= $endTime) {
  155. return $form->error('开始时间必须早于结束时间');
  156. }
  157. }
  158. });
  159. return $form;
  160. });
  161. }
  162. }