ItemChestOpenCostController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Enums\CHEST_COST_TYPE;
  4. use App\Module\GameItems\Models\Item;
  5. use App\Module\GameItems\Models\ItemChestOpenCost;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Layout\Content;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. /**
  13. * 宝箱开启消耗配置控制器
  14. */
  15. #[Resource('game-items-chest-costs', names: 'dcat.admin.game-items-chest-costs')]
  16. class ItemChestOpenCostController extends AdminController
  17. {
  18. protected $title ='宝箱开启消耗配置';
  19. /**
  20. * 列表页
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. $grid = new Grid(new ItemChestOpenCost());
  27. $grid->column('id', 'ID')->sortable();
  28. $grid->column('chest.name', '宝箱名称')->link(function () {
  29. return admin_url('game-items/items/' . $this->chest_id);
  30. });
  31. $grid->column('cost_type', '消耗类型')->display(function ($value) {
  32. return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
  33. });
  34. $grid->column('cost_id', '消耗ID')->display(function ($value) {
  35. if ($this->cost_type == CHEST_COST_TYPE::ITEM->value) {
  36. $item = Item::find($value);
  37. return $item ? "{$item->name} (ID: {$value})" : $value;
  38. }
  39. return $value;
  40. });
  41. $grid->column('cost_quantity', '消耗数量');
  42. $grid->column('is_active', '是否激活')->switch();
  43. $grid->column('created_at', '创建时间');
  44. $grid->column('updated_at', '更新时间');
  45. // 筛选器
  46. $grid->filter(function (Grid\Filter $filter) {
  47. $filter->equal('chest_id', '宝箱ID');
  48. $filter->where('chest_name', function ($query) {
  49. $query->whereHas('chest', function ($query) {
  50. $query->where('name', 'like', "%{$this->input}%");
  51. });
  52. }, '宝箱名称');
  53. $filter->equal('cost_type', '消耗类型')->select(CHEST_COST_TYPE::getAll());
  54. $filter->equal('cost_id', '消耗ID');
  55. $filter->equal('is_active', '是否激活')->select([0 => '否', 1 => '是']);
  56. });
  57. // 批量操作
  58. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  59. $batch->add(new \App\Module\GameItems\AdminControllers\Actions\BatchActivate());
  60. $batch->add(new \App\Module\GameItems\AdminControllers\Actions\BatchDeactivate());
  61. });
  62. // 工具栏
  63. $grid->tools(function (Grid\Tools $tools) {
  64. $tools->append(new \App\Module\GameItems\AdminControllers\Actions\CopyToAnotherChest());
  65. });
  66. return $grid;
  67. }
  68. /**
  69. * 详情页
  70. *
  71. * @param mixed $id
  72. * @return Show
  73. */
  74. protected function detail($id)
  75. {
  76. $show = new Show(ItemChestOpenCost::findOrFail($id));
  77. $show->field('id', 'ID');
  78. $show->field('chest.name', '宝箱名称');
  79. $show->field('chest_id', '宝箱ID');
  80. $show->field('cost_type', '消耗类型')->as(function ($value) {
  81. return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
  82. });
  83. $show->field('cost_id', '消耗ID');
  84. $show->field('cost_quantity', '消耗数量');
  85. $show->field('is_active', '是否激活')->as(function ($value) {
  86. return $value ? '是' : '否';
  87. });
  88. $show->field('created_at', '创建时间');
  89. $show->field('updated_at', '更新时间');
  90. return $show;
  91. }
  92. /**
  93. * 表单
  94. *
  95. * @return Form
  96. */
  97. protected function form()
  98. {
  99. $form = new Form(new ItemChestOpenCost());
  100. // 只显示宝箱类型的物品
  101. $chests = Item::where('type', 3)->pluck('name', 'id');
  102. $form->select('chest_id', '宝箱')->options($chests)->required();
  103. $form->select('cost_type', '消耗类型')->options(CHEST_COST_TYPE::getAll())->required();
  104. // 根据消耗类型显示不同的选择器
  105. $form->select('cost_id', '消耗ID')->options(function ($id) {
  106. $costType = request()->get('cost_type');
  107. if ($costType == CHEST_COST_TYPE::ITEM->value) {
  108. return Item::pluck('name', 'id');
  109. }
  110. return [];
  111. })->required();
  112. $form->number('cost_quantity', '消耗数量')->min(1)->default(1)->required();
  113. $form->switch('is_active', '是否激活')->default(1);
  114. // 保存前回调
  115. $form->saving(function (Form $form) {
  116. // 验证消耗ID
  117. if ($form->cost_type == CHEST_COST_TYPE::ITEM->value) {
  118. $item = Item::find($form->cost_id);
  119. if (!$item) {
  120. return $form->response()->error('无效的物品ID');
  121. }
  122. }
  123. });
  124. return $form;
  125. }
  126. }