ItemChestOpenCostController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\AdminControllers\Actions\BatchActivateAction;
  4. use App\Module\GameItems\Enums\CHEST_COST_TYPE;
  5. use App\Module\GameItems\Models\Item;
  6. use App\Module\GameItems\Models\ItemChestOpenCost;
  7. use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  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. use Dcat\Admin\Layout\Content;
  17. use Spatie\RouteAttributes\Attributes\Resource;
  18. /**
  19. * 宝箱开启消耗配置控制器
  20. */
  21. #[Resource('game-items-chest-costs', names: 'dcat.admin.game-items-chest-costs')]
  22. class ItemChestOpenCostController extends AdminController
  23. {
  24. protected $title ='宝箱开启消耗配置';
  25. /**
  26. * 列表页
  27. *
  28. * @return Grid
  29. */
  30. protected function grid()
  31. {
  32. return Grid::make(new ItemChestOpenCostRepository(['chest', 'costItem']), function (Grid $grid) {
  33. $helper = new GridHelper($grid, $this);
  34. $helper->columnId();
  35. $grid->column('chest.name', '宝箱名称')->link(function () {
  36. return admin_url('game-items/' . $this->chest_id);
  37. });
  38. $grid->column('cost_type', '消耗类型')->display(function ($value) {
  39. return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
  40. });
  41. $grid->column('cost_id', '消耗ID')->as(function ($value) {
  42. if ($this->cost_type == CHEST_COST_TYPE::ITEM->value) {
  43. if ($this->costItem) {
  44. $itemUrl = admin_url('game-items-items/' . $value);
  45. return "<a href='{$itemUrl}' target='_blank'>{$this->costItem->name} <i class='fa fa-external-link'></i></a> (ID: {$value})";
  46. }
  47. return $value;
  48. }
  49. return $value;
  50. })->unescape();
  51. $grid->column('cost_quantity', '消耗数量');
  52. $grid->column('is_active', '是否激活')->switch();
  53. $grid->column('created_at', '创建时间');
  54. $grid->column('updated_at', '更新时间');
  55. // 筛选器
  56. $grid->filter(function (Grid\Filter $filter) {
  57. $helper = new FilterHelper($filter, $this);
  58. $helper->equal('id', 'ID');
  59. $helper->equalSelectModelChestItem('chest_id', '宝箱ID');
  60. $filter->equal('cost_type', '消耗类型')->select(CHEST_COST_TYPE::getAll());
  61. $filter->equal('is_active', '是否激活')->radio([
  62. 1 => '是',
  63. 0 => '否',
  64. ]);
  65. });
  66. // 批量操作
  67. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  68. $batch->add(new \App\Module\GameItems\AdminControllers\Actions\BatchDeactivateAction());
  69. $batch->add(new BatchActivateAction());
  70. });
  71. // 工具栏
  72. $grid->tools(function (Grid\Tools $tools) {
  73. $tools->append(new \App\Module\GameItems\AdminControllers\Actions\CopyToAnotherChestAction());
  74. });
  75. });
  76. }
  77. /**
  78. * 详情页
  79. *
  80. * @param mixed $id
  81. * @return Show
  82. */
  83. protected function detail($id)
  84. {
  85. return Show::make($id, new ItemChestOpenCostRepository(['chest', 'costItem']), function (Show $show) {
  86. $helper = new ShowHelper($show, $this);
  87. $helper->field('id', 'ID');
  88. $show->field('chest.name', '宝箱名称');
  89. $helper->field('chest_id', '宝箱ID');
  90. $show->field('cost_type', '消耗类型')->as(function ($value) {
  91. return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
  92. });
  93. $helper->field('cost_id', '消耗ID');
  94. // 根据消耗类型显示不同的关联信息
  95. $show->field('costItem.name', '消耗物品名称')->unescape()->as(function ($name) {
  96. if ($this->cost_type == CHEST_COST_TYPE::ITEM->value && $name) {
  97. $itemUrl = admin_url('game-items/items/' . $this->cost_id);
  98. return "<span class='badge badge-primary'>{$name}</span> "
  99. . "<a href='{$itemUrl}' target='_blank' class='btn btn-sm btn-primary ml-1'>"
  100. . "<i class='fa fa-external-link'></i> 查看物品详情</a>";
  101. }
  102. return '-';
  103. });
  104. $helper->field('cost_quantity', '消耗数量');
  105. $show->field('is_active', '是否激活')->as(function ($value) {
  106. return $value ? '是' : '否';
  107. });
  108. $helper->field('created_at', '创建时间');
  109. $helper->field('updated_at', '更新时间');
  110. });
  111. }
  112. /**
  113. * 表单
  114. *
  115. * @return Form
  116. */
  117. protected function form()
  118. {
  119. return Form::make(new ItemChestOpenCostRepository(), function (Form $form) {
  120. $helper = new FormHelper($form, $this);
  121. // 只显示宝箱类型的物品
  122. $chests = Item::where('type', 3)->pluck('name', 'id');
  123. $helper->selectModelChestItem('chest_id', '宝箱');
  124. $form->select('cost_type', '消耗类型')->options(CHEST_COST_TYPE::getAll())->required();
  125. // 根据消耗类型显示不同的选择器
  126. $form->number('cost_id', '消耗ID')->required();
  127. $helper->number('cost_quantity', '消耗数量')->min(1)->default(1)->required();
  128. $form->switch('is_active', '是否激活')->default(1);
  129. // 保存前回调
  130. $form->saving(function (Form $form) {
  131. // 验证消耗ID
  132. if ($form->cost_type == CHEST_COST_TYPE::ITEM->value) {
  133. $item = Item::find($form->cost_id);
  134. if (!$item) {
  135. return $form->response()->error('无效的物品ID');
  136. }
  137. }
  138. });
  139. });
  140. }
  141. }