GameConsumeItemController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Fund\Models\Currency;
  4. use App\Module\Game\Enums\CONSUME_TYPE;
  5. use App\Module\Game\Models\GameConsumeGroup;
  6. use App\Module\Game\Repositorys\GameConsumeItemRepository;
  7. use App\Module\GameItems\Models\Item;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Spatie\RouteAttributes\Attributes\Resource;
  13. /**
  14. * 消耗项管理控制器
  15. */
  16. #[Resource('game-consume-items', names: 'dcat.admin.game-consume-items')]
  17. class GameConsumeItemController extends AdminController
  18. {
  19. /**
  20. * 标题
  21. *
  22. * @return string
  23. */
  24. protected function title()
  25. {
  26. return '消耗项管理';
  27. }
  28. /**
  29. * 创建表格
  30. *
  31. * @return Grid
  32. */
  33. protected function grid()
  34. {
  35. return Grid::make(new GameConsumeItemRepository(), function (Grid $grid) {
  36. $grid->column('id', 'ID')->sortable();
  37. $grid->column('group_id', '消耗组')->display(function ($groupId) {
  38. $group = GameConsumeGroup::find($groupId);
  39. return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
  40. });
  41. $grid->column('consume_type', '消耗类型')->display(function ($type) {
  42. return CONSUME_TYPE::getName($type);
  43. });
  44. $grid->column('target_id', '目标ID')->display(function ($targetId) {
  45. // 根据消耗类型显示目标名称
  46. if ($this->consume_type == CONSUME_TYPE::ITEM->value) {
  47. $item = Item::find($targetId);
  48. return $item ? "{$item->name} ({$targetId})" : $targetId;
  49. } elseif ($this->consume_type == CONSUME_TYPE::CURRENCY->value) {
  50. $currency = Currency::find($targetId);
  51. return $currency ? "{$currency->name} ({$targetId})" : $targetId;
  52. }
  53. return $targetId;
  54. });
  55. $grid->column('quantity', '数量');
  56. $grid->column('created_at', '创建时间');
  57. $grid->column('updated_at', '更新时间');
  58. $grid->filter(function (Grid\Filter $filter) {
  59. $filter->equal('id', 'ID');
  60. $filter->equal('group_id', '消耗组')->select(
  61. GameConsumeGroup::pluck('name', 'id')
  62. );
  63. $filter->equal('consume_type', '消耗类型')->select(CONSUME_TYPE::getAll());
  64. $filter->equal('target_id', '目标ID');
  65. });
  66. });
  67. }
  68. /**
  69. * 创建详情页
  70. *
  71. * @param mixed $id
  72. * @return Show
  73. */
  74. protected function detail($id)
  75. {
  76. return Show::make($id, new GameConsumeItemRepository(), function (Show $show) {
  77. $show->field('id', 'ID');
  78. $show->field('group_id', '消耗组')->as(function ($groupId) {
  79. $group = GameConsumeGroup::find($groupId);
  80. return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
  81. });
  82. $show->field('consume_type', '消耗类型')->as(function ($type) {
  83. return CONSUME_TYPE::getName($type);
  84. });
  85. $show->field('target_id', '目标ID')->as(function ($targetId) {
  86. // 根据消耗类型显示目标名称
  87. if ($this->consume_type == CONSUME_TYPE::ITEM->value) {
  88. $item = Item::find($targetId);
  89. return $item ? "{$item->name} ({$targetId})" : $targetId;
  90. } elseif ($this->consume_type == CONSUME_TYPE::CURRENCY->value) {
  91. $currency = Currency::find($targetId);
  92. return $currency ? "{$currency->name} ({$targetId})" : $targetId;
  93. }
  94. return $targetId;
  95. });
  96. $show->field('param1', '参数1');
  97. $show->field('param2', '参数2');
  98. $show->field('quantity', '数量');
  99. $show->field('extra_data', '额外数据')->json();
  100. $show->field('created_at', '创建时间');
  101. $show->field('updated_at', '更新时间');
  102. });
  103. }
  104. /**
  105. * 创建表单
  106. *
  107. * @return Form
  108. */
  109. protected function form()
  110. {
  111. return Form::make(new GameConsumeItemRepository(), function (Form $form) {
  112. $form->display('id', 'ID');
  113. $form->select('group_id', '消耗组')
  114. ->options(GameConsumeGroup::pluck('name', 'id'))
  115. ->required();
  116. $form->select('consume_type', '消耗类型')
  117. ->options(CONSUME_TYPE::getAll())
  118. ->required()
  119. ->when(CONSUME_TYPE::ITEM->value, function (Form $form) {
  120. // 物品消耗
  121. $form->select('target_id', '物品')
  122. ->options(Item::pluck('name', 'id'))
  123. ->required()
  124. ->help('选择要消耗的物品');
  125. })
  126. ->when(CONSUME_TYPE::CURRENCY->value, function (Form $form) {
  127. // 货币消耗
  128. $form->select('target_id', '货币')
  129. ->options(Currency::pluck('name', 'id'))
  130. ->required()
  131. ->help('选择要消耗的货币');
  132. });
  133. $form->number('param1', '参数1')
  134. ->default(0)
  135. ->help('根据消耗类型不同含义,如物品的品质、货币的来源等');
  136. $form->number('param2', '参数2')
  137. ->default(0)
  138. ->help('根据消耗类型不同含义,如物品的绑定状态、货币的类型等');
  139. $form->number('quantity', '数量')
  140. ->default(1)
  141. ->min(1)
  142. ->required();
  143. $form->textarea('extra_data', '额外数据')
  144. ->help('JSON格式,可存储特定消耗类型的额外参数');
  145. $form->display('created_at', '创建时间');
  146. $form->display('updated_at', '更新时间');
  147. });
  148. }
  149. }