ItemChestConfigController.php 7.7 KB

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