ItemChestConfigController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Enums\ITEM_TYPE;
  4. use App\Module\GameItems\Models\Item;
  5. use App\Module\GameItems\Models\ItemChestConfig;
  6. use App\Module\GameItems\Repositories\ItemChestConfigRepository;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use UCore\DcatAdmin\AdminController;
  12. use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
  13. use App\Module\GameItems\AdminControllers\Helper\FormHelper;
  14. use App\Module\GameItems\AdminControllers\Helper\GridHelper;
  15. use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
  16. /**
  17. * 宝箱配置控制器
  18. *
  19. * 路由: /admin/game-items/chest-configs
  20. * 菜单: 游戏管理 -> 物品管理 -> 宝箱配置
  21. */
  22. #[Resource('game-items/chest-configs', names: 'dcat.admin.game-items.chest-configs')]
  23. class ItemChestConfigController extends AdminController
  24. {
  25. /**
  26. * 页面标题
  27. *
  28. * @var string
  29. */
  30. protected $title = '宝箱配置';
  31. /**
  32. * 数据仓库
  33. *
  34. * @var ItemChestConfigRepository
  35. */
  36. protected $repository;
  37. /**
  38. * 构造函数
  39. */
  40. public function __construct()
  41. {
  42. $this->repository = new ItemChestConfigRepository();
  43. }
  44. /**
  45. * 列表页面
  46. *
  47. * @return Grid
  48. */
  49. protected function grid(): Grid
  50. {
  51. return Grid::make(new ItemChestConfig(), function (Grid $grid) {
  52. $helper = new GridHelper($grid, $this);
  53. $helper->columnId();
  54. $grid->column('item.name', '宝箱名称')->display(function ($name) {
  55. return $name ?: '未知宝箱';
  56. });
  57. $grid->column('item_id', '宝箱ID')->sortable();
  58. $grid->column('consume_group.name', '消耗组')->display(function ($name) {
  59. return $name ?: '<span class="text-muted">无消耗</span>';
  60. });
  61. $grid->column('reward_group.name', '奖励组')->display(function ($name) {
  62. return $name ?: '<span class="text-danger">未配置</span>';
  63. });
  64. $grid->column('condition_group.name', '条件组')->display(function ($name) {
  65. return $name ?: '<span class="text-muted">无条件</span>';
  66. });
  67. $grid->column('is_active', '状态')->switch();
  68. $grid->column('status_text', '配置状态')->display(function () {
  69. return $this->status_text;
  70. })->label([
  71. '正常' => 'success',
  72. '未激活' => 'warning',
  73. '缺少奖励组' => 'danger',
  74. ]);
  75. $helper->columnCreatedAt();
  76. $helper->columnUpdatedAt();
  77. // 筛选器
  78. $grid->filter(function (Grid\Filter $filter) {
  79. $filterHelper = new FilterHelper($filter, $this);
  80. $filter->equal('item_id', '宝箱ID');
  81. $filter->like('item.name', '宝箱名称');
  82. $filter->equal('consume_group_id', '消耗组')->select(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id'));
  83. $filter->equal('reward_group_id', '奖励组')->select(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id'));
  84. $filter->equal('condition_group_id', '条件组')->select(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id'));
  85. $filter->equal('is_active', '状态')->select([1 => '激活', 0 => '未激活']);
  86. });
  87. // 关联查询
  88. $grid->model()->with(['item', 'consumeGroup', 'rewardGroup', 'conditionGroup']);
  89. // 工具栏
  90. $grid->tools(function (Grid\Tools $tools) {
  91. $tools->append('<a href="' . admin_url('game-items') . '" class="btn btn-sm btn-primary">返回物品管理</a>');
  92. });
  93. // 行操作
  94. $grid->actions(function (Grid\Displayers\Actions $actions) {
  95. $actions->disableDelete();
  96. });
  97. });
  98. }
  99. /**
  100. * 详情页面
  101. *
  102. * @param mixed $id
  103. * @return Show
  104. */
  105. protected function detail($id): Show
  106. {
  107. return Show::make($id, new ItemChestConfig(), function (Show $show) {
  108. $helper = new ShowHelper($show, $this);
  109. $helper->field('id', 'ID');
  110. $show->field('item.name', '宝箱名称');
  111. $show->field('item_id', '宝箱ID');
  112. $show->divider('消耗组配置');
  113. $show->field('consume_group.name', '消耗组名称');
  114. $show->field('consume_group.code', '消耗组编码');
  115. $show->field('consume_group.description', '消耗组描述');
  116. $show->divider('奖励组配置');
  117. $show->field('reward_group.name', '奖励组名称');
  118. $show->field('reward_group.code', '奖励组编码');
  119. $show->field('reward_group.description', '奖励组描述');
  120. $show->field('reward_group.is_random', '是否随机')->using([0 => '否', 1 => '是']);
  121. $show->field('reward_group.random_count', '随机数量');
  122. $show->divider('条件组配置');
  123. $show->field('condition_group.name', '条件组名称');
  124. $show->field('condition_group.code', '条件组编码');
  125. $show->field('condition_group.description', '条件组描述');
  126. $show->divider('基本信息');
  127. $show->field('is_active', '状态')->using([0 => '未激活', 1 => '激活']);
  128. $show->field('status_text', '配置状态');
  129. $helper->field('created_at', '创建时间');
  130. $helper->field('updated_at', '更新时间');
  131. });
  132. }
  133. /**
  134. * 表单页面
  135. *
  136. * @return Form
  137. */
  138. protected function form(): Form
  139. {
  140. return Form::make(new ItemChestConfig(), function (Form $form) {
  141. $helper = new FormHelper($form, $this);
  142. $form->display('id', 'ID');
  143. $form->select('item_id', '宝箱物品')
  144. ->options(Item::where('type', ITEM_TYPE::CHEST)->pluck('name', 'id'))
  145. ->required()
  146. ->help('只能选择宝箱类型的物品');
  147. $form->divider('组配置');
  148. $form->select('consume_group_id', '消耗组')
  149. ->options(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id'))
  150. ->help('选择宝箱开启时需要消耗的资源组,留空表示无额外消耗');
  151. $form->select('reward_group_id', '奖励组')
  152. ->options(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id'))
  153. ->required()
  154. ->help('选择宝箱开启时获得的奖励组');
  155. $form->select('condition_group_id', '条件组')
  156. ->options(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id'))
  157. ->help('选择宝箱开启的前置条件组,留空表示无条件限制');
  158. $form->switch('is_active', '激活状态')
  159. ->default(1)
  160. ->help('只有激活的配置才会生效');
  161. $form->display('created_at', '创建时间');
  162. $form->display('updated_at', '更新时间');
  163. // 保存前验证
  164. $form->saving(function (Form $form) {
  165. // 检查宝箱是否已有配置
  166. if ($form->isCreating()) {
  167. $exists = ItemChestConfig::where('item_id', $form->item_id)->exists();
  168. if ($exists) {
  169. return $form->response()->error('该宝箱已有配置,请编辑现有配置');
  170. }
  171. }
  172. // 检查物品是否为宝箱类型
  173. $item = Item::find($form->item_id);
  174. if (!$item || $item->type !== ITEM_TYPE::CHEST) {
  175. return $form->response()->error('只能为宝箱类型物品创建配置');
  176. }
  177. });
  178. });
  179. }
  180. }