ItemChestConfigController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.consumeItems', 'rewardGroup.rewardItems', 'conditionGroup.conditionItems']);
  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. // 消耗组列 - 可点击跳转
  59. $grid->column('consumeGroup.name', '消耗组')->display(function ($name) {
  60. if (!$name || !$this->consume_group_id) {
  61. return '<span class="text-muted">无消耗</span>';
  62. }
  63. return $name;
  64. })->link(function () {
  65. if (!$this->consume_group_id) {
  66. return '';
  67. }
  68. return admin_url('game-consume-groups/' . $this->consume_group_id);
  69. });
  70. // 奖励组列 - 可点击跳转
  71. $grid->column('rewardGroup.name', '奖励组')->display(function ($name) {
  72. if (!$name || !$this->reward_group_id) {
  73. return '<span class="text-danger">未配置</span>';
  74. }
  75. return $name;
  76. })->link(function () {
  77. if (!$this->reward_group_id) {
  78. return '';
  79. }
  80. return admin_url('game-reward-groups/' . $this->reward_group_id);
  81. });
  82. // 条件组列 - 可点击跳转
  83. $grid->column('conditionGroup.name', '条件组')->display(function ($name) {
  84. if (!$name || !$this->condition_group_id) {
  85. return '<span class="text-muted">无条件</span>';
  86. }
  87. return $name;
  88. })->link(function () {
  89. if (!$this->condition_group_id) {
  90. return '';
  91. }
  92. return admin_url('game-condition-groups/' . $this->condition_group_id);
  93. });
  94. // 消耗组详情列
  95. $grid->column('consume_group_details', '消耗组详情')->display(function () {
  96. if (!$this->consumeGroup) {
  97. return '<span class="text-muted">无消耗组</span>';
  98. }
  99. return $this->consumeGroup->formatConsumeDetails();
  100. })->width('200px');
  101. // 奖励组详情列
  102. $grid->column('reward_group_details', '奖励组详情')->display(function () {
  103. if (!$this->rewardGroup) {
  104. return '<span class="text-muted">无奖励组</span>';
  105. }
  106. return $this->rewardGroup->formatRewardDetails();
  107. })->width('200px');
  108. // 条件组详情列
  109. $grid->column('condition_group_details', '条件组详情')->display(function () {
  110. if (!$this->conditionGroup) {
  111. return '<span class="text-muted">无条件组</span>';
  112. }
  113. return $this->conditionGroup->formatConditionDetails();
  114. })->width('200px');
  115. $grid->column('is_active', '状态')->switch();
  116. $grid->column('status_text', '配置状态')->display(function () {
  117. return $this->status_text;
  118. })->label([
  119. '正常' => 'success',
  120. '未激活' => 'warning',
  121. '缺少奖励组' => 'danger',
  122. ]);
  123. $helper->columnCreatedAt();
  124. $helper->columnUpdatedAt();
  125. // 筛选器
  126. $grid->filter(function (Grid\Filter $filter) {
  127. $filter->equal('item_id', '宝箱ID');
  128. $filter->like('item.name', '宝箱名称');
  129. $filter->equal('consume_group_id', '消耗组')->select(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id'));
  130. $filter->equal('reward_group_id', '奖励组')->select(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id'));
  131. $filter->equal('condition_group_id', '条件组')->select(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id'));
  132. $filter->equal('is_active', '状态')->select([1 => '激活', 0 => '未激活']);
  133. });
  134. // 工具栏
  135. $grid->tools(function (Grid\Tools $tools) {
  136. $tools->append('<a href="' . admin_url('game-items') . '" class="btn btn-sm btn-primary">返回物品管理</a>');
  137. });
  138. // 行操作
  139. $grid->actions(function (Grid\Displayers\Actions $actions) {
  140. $actions->disableDelete();
  141. });
  142. });
  143. }
  144. /**
  145. * 详情页面
  146. *
  147. * @param mixed $id
  148. * @return Show
  149. */
  150. protected function detail($id): Show
  151. {
  152. return Show::make($id, new ItemChestConfig(), function (Show $show) {
  153. $helper = new ShowHelper($show, $this);
  154. $helper->field('id', 'ID');
  155. $show->field('item.name', '宝箱名称');
  156. $show->field('item_id', '宝箱ID');
  157. $show->divider('消耗组配置');
  158. $show->field('consumeGroup.name', '消耗组名称');
  159. $show->field('consumeGroup.code', '消耗组编码');
  160. $show->field('consumeGroup.description', '消耗组描述');
  161. $show->divider('奖励组配置');
  162. $show->field('rewardGroup.name', '奖励组名称');
  163. $show->field('rewardGroup.code', '奖励组编码');
  164. $show->field('rewardGroup.description', '奖励组描述');
  165. $show->field('rewardGroup.is_random', '是否随机')->using([0 => '否', 1 => '是']);
  166. $show->field('rewardGroup.random_count', '随机数量');
  167. $show->divider('条件组配置');
  168. $show->field('conditionGroup.name', '条件组名称');
  169. $show->field('conditionGroup.code', '条件组编码');
  170. $show->field('conditionGroup.description', '条件组描述');
  171. $show->divider('基本信息');
  172. $show->field('is_active', '状态')->using([0 => '未激活', 1 => '激活']);
  173. $show->field('status_text', '配置状态');
  174. $helper->field('created_at', '创建时间');
  175. $helper->field('updated_at', '更新时间');
  176. });
  177. }
  178. /**
  179. * 表单页面
  180. *
  181. * @return Form
  182. */
  183. protected function form(): Form
  184. {
  185. return Form::make(new ItemChestConfig(), function (Form $form) {
  186. $form->display('id', 'ID');
  187. $form->select('item_id', '宝箱物品')
  188. ->options(Item::where('type', ITEM_TYPE::CHEST)->pluck('name', 'id'))
  189. ->required()
  190. ->help('只能选择宝箱类型的物品');
  191. $form->divider('组配置');
  192. $form->select('consume_group_id', '消耗组')
  193. ->options(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id'))
  194. ->help('选择宝箱开启时需要消耗的资源组,留空表示无额外消耗');
  195. $form->select('reward_group_id', '奖励组')
  196. ->options(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id'))
  197. ->required()
  198. ->help('选择宝箱开启时获得的奖励组');
  199. $form->select('condition_group_id', '条件组')
  200. ->options(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id'))
  201. ->help('选择宝箱开启的前置条件组,留空表示无条件限制');
  202. $form->switch('is_active', '激活状态')
  203. ->default(1)
  204. ->help('只有激活的配置才会生效');
  205. $form->display('created_at', '创建时间');
  206. $form->display('updated_at', '更新时间');
  207. // 保存前验证
  208. $form->saving(function (Form $form) {
  209. // 检查宝箱是否已有配置
  210. if ($form->isCreating()) {
  211. $exists = ItemChestConfig::where('item_id', $form->item_id)->exists();
  212. if ($exists) {
  213. return $form->response()->error('该宝箱已有配置,请编辑现有配置');
  214. }
  215. }
  216. // 检查物品是否为宝箱类型
  217. $item = Item::find($form->item_id);
  218. if (!$item || $item->type !== ITEM_TYPE::CHEST) {
  219. return $form->response()->error('只能为宝箱类型物品创建配置');
  220. }
  221. });
  222. });
  223. }
  224. }