ItemChestConfigController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. use App\Module\GameItems\AdminControllers\Actions\DuplicateItemChestConfigAction;
  15. /**
  16. * 宝箱配置控制器
  17. *
  18. * 路由: game-items/chest-configs
  19. * 菜单: 游戏管理 -> 物品管理 -> 宝箱配置
  20. */
  21. #[Resource('game-items-chest-configs', names: 'dcat.admin.game-items-chest-configs')]
  22. class ItemChestConfigController extends AdminController
  23. {
  24. /**
  25. * 页面标题
  26. *
  27. * @var string
  28. */
  29. protected $title = '宝箱配置';
  30. /**
  31. * 数据仓库
  32. *
  33. * @var ItemChestConfigRepository
  34. */
  35. protected $repository;
  36. /**
  37. * 构造函数
  38. */
  39. public function __construct()
  40. {
  41. $this->repository = new ItemChestConfigRepository();
  42. }
  43. /**
  44. * 列表页面
  45. *
  46. * @return Grid
  47. */
  48. protected function grid(): Grid
  49. {
  50. return Grid::make(new ItemChestConfig(), function (Grid $grid) {
  51. // 首先设置关联查询,确保数据正确预加载,包括子项目
  52. $grid->model()->with(['item', 'consumeGroup.consumeItems', 'rewardGroup.rewardItems', 'conditionGroup.conditionItems']);
  53. $helper = new GridHelper($grid, $this);
  54. $helper->columnId();
  55. $grid->column('item.name', '宝箱名称')->display(function ($name) {
  56. return $name ?: '未知宝箱';
  57. });
  58. $grid->column('item_id', '宝箱ID')->sortable();
  59. // 消耗组列 - 可点击跳转
  60. $grid->column('consumeGroup.name', '消耗组')->display(function ($name) {
  61. if (!$name || !$this->consume_group_id) {
  62. return '<span class="text-muted">无消耗</span>';
  63. }
  64. return $name;
  65. })->link(function () {
  66. if (!$this->consume_group_id) {
  67. return '';
  68. }
  69. return admin_url('game-consume-groups/' . $this->consume_group_id);
  70. });
  71. // 奖励组列 - 可点击跳转
  72. $grid->column('rewardGroup.name', '奖励组')->display(function ($name) {
  73. if (!$name || !$this->reward_group_id) {
  74. return '<span class="text-danger">未配置</span>';
  75. }
  76. return $name;
  77. })->link(function () {
  78. if (!$this->reward_group_id) {
  79. return '';
  80. }
  81. return admin_url('game-reward-groups/' . $this->reward_group_id);
  82. });
  83. // 条件组列 - 可点击跳转
  84. $grid->column('conditionGroup.name', '条件组')->display(function ($name) {
  85. if (!$name || !$this->condition_group_id) {
  86. return '<span class="text-muted">无条件</span>';
  87. }
  88. return $name;
  89. })->link(function () {
  90. if (!$this->condition_group_id) {
  91. return '';
  92. }
  93. return admin_url('game-condition-groups/' . $this->condition_group_id);
  94. });
  95. // 消耗组详情列
  96. $grid->column('consume_group_details', '消耗组详情')->display(function () {
  97. if (!$this->consumeGroup) {
  98. return '<span class="text-muted">无消耗组</span>';
  99. }
  100. return $this->consumeGroup->formatConsumeDetails();
  101. })->width('200px');
  102. // 奖励组详情列
  103. $grid->column('reward_group_details', '奖励组详情')->display(function () {
  104. if (!$this->rewardGroup) {
  105. return '<span class="text-muted">无奖励组</span>';
  106. }
  107. return $this->rewardGroup->formatRewardDetails();
  108. })->width('200px');
  109. // 条件组详情列
  110. $grid->column('condition_group_details', '条件组详情')->display(function () {
  111. if (!$this->conditionGroup) {
  112. return '<span class="text-muted">无条件组</span>';
  113. }
  114. return $this->conditionGroup->formatConditionDetails();
  115. })->width('200px');
  116. $grid->column('is_active', '状态')->switch();
  117. $grid->column('status_text', '配置状态')->display(function () {
  118. return $this->status_text;
  119. })->label([
  120. '正常' => 'success',
  121. '未激活' => 'warning',
  122. '缺少奖励组' => 'danger',
  123. ]);
  124. $helper->columnCreatedAt();
  125. $helper->columnUpdatedAt();
  126. // 筛选器
  127. $grid->filter(function (Grid\Filter $filter) {
  128. $filter->equal('item_id', '宝箱ID');
  129. $filter->like('item.name', '宝箱名称');
  130. $filter->equal('consume_group_id', '消耗组')->select(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id'));
  131. $filter->equal('reward_group_id', '奖励组')->select(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id'));
  132. $filter->equal('condition_group_id', '条件组')->select(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id'));
  133. $filter->equal('is_active', '状态')->select([1 => '激活', 0 => '未激活']);
  134. });
  135. // 工具栏
  136. $grid->tools(function (Grid\Tools $tools) {
  137. $tools->append('<a href="' . admin_url('game-items') . '" class="btn btn-sm btn-primary">返回物品管理</a>');
  138. });
  139. // 行操作
  140. $grid->actions(function (Grid\Displayers\Actions $actions) {
  141. // 添加复制按钮
  142. $actions->append(new DuplicateItemChestConfigAction());
  143. $actions->disableDelete();
  144. });
  145. });
  146. }
  147. /**
  148. * 详情页面
  149. *
  150. * @param mixed $id
  151. * @return Show
  152. */
  153. protected function detail($id): Show
  154. {
  155. return Show::make($id, new ItemChestConfig(), function (Show $show) {
  156. $helper = new ShowHelper($show, $this);
  157. $helper->field('id', 'ID');
  158. $show->field('item.name', '宝箱名称');
  159. $show->field('item_id', '宝箱ID');
  160. $show->divider('消耗组配置');
  161. $show->field('consumeGroup.name', '消耗组名称');
  162. $show->field('consumeGroup.code', '消耗组编码');
  163. $show->field('consumeGroup.description', '消耗组描述');
  164. $show->divider('奖励组配置');
  165. $show->field('rewardGroup.name', '奖励组名称');
  166. $show->field('rewardGroup.code', '奖励组编码');
  167. $show->field('rewardGroup.description', '奖励组描述');
  168. $show->field('rewardGroup.is_random', '是否随机')->using([0 => '否', 1 => '是']);
  169. $show->field('rewardGroup.random_count', '随机数量');
  170. $show->divider('条件组配置');
  171. $show->field('conditionGroup.name', '条件组名称');
  172. $show->field('conditionGroup.code', '条件组编码');
  173. $show->field('conditionGroup.description', '条件组描述');
  174. $show->divider('基本信息');
  175. $show->field('is_active', '状态')->using([0 => '未激活', 1 => '激活']);
  176. $show->field('status_text', '配置状态');
  177. $helper->field('created_at', '创建时间');
  178. $helper->field('updated_at', '更新时间');
  179. });
  180. }
  181. /**
  182. * 表单页面
  183. *
  184. * @return Form
  185. */
  186. protected function form(): Form
  187. {
  188. return Form::make(new ItemChestConfig(), function (Form $form) {
  189. $form->display('id', 'ID');
  190. $form->select('item_id', '宝箱物品')
  191. ->options(Item::where('type', ITEM_TYPE::CHEST)->pluck('name', 'id'))
  192. ->required()
  193. ->help('只能选择宝箱类型的物品');
  194. $form->divider('组配置');
  195. $form->select('consume_group_id', '消耗组')
  196. ->options(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id'))
  197. ->help('选择宝箱开启时需要消耗的资源组,留空表示无额外消耗');
  198. $form->select('reward_group_id', '奖励组')
  199. ->options(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id'))
  200. ->required()
  201. ->help('选择宝箱开启时获得的奖励组');
  202. $form->select('condition_group_id', '条件组')
  203. ->options(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id'))
  204. ->help('选择宝箱开启的前置条件组,留空表示无条件限制');
  205. $form->switch('is_active', '激活状态')
  206. ->default(1)
  207. ->help('只有激活的配置才会生效');
  208. $form->display('created_at', '创建时间');
  209. $form->display('updated_at', '更新时间');
  210. // 保存前验证
  211. $form->saving(function (Form $form) {
  212. // 检查宝箱是否已有配置
  213. if ($form->isCreating()) {
  214. $exists = ItemChestConfig::where('item_id', $form->item_id)->exists();
  215. if ($exists) {
  216. return $form->response()->error('该宝箱已有配置,请编辑现有配置');
  217. }
  218. }
  219. // 检查物品是否为宝箱类型
  220. $item = Item::find($form->item_id);
  221. if (!$item || $item->type !== ITEM_TYPE::CHEST) {
  222. return $form->response()->error('只能为宝箱类型物品创建配置');
  223. }
  224. });
  225. });
  226. }
  227. }