GameRewardGroupController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\AdminControllers\Actions\DuplicateRewardGroupAction;
  4. use App\Module\Game\Enums\REWARD_TYPE;
  5. use App\Module\Game\Models\GameRewardItem;
  6. use App\Module\Game\Repositorys\GameRewardGroupRepository;
  7. use App\Module\GameItems\Models\Item;
  8. use App\Module\Fund\Models\FundCurrencyModel;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Spatie\RouteAttributes\Attributes\Resource;
  14. /**
  15. * 奖励组管理控制器
  16. */
  17. #[Resource('game-reward-groups', names: 'dcat.admin.game-reward-groups')]
  18. class GameRewardGroupController extends AdminController
  19. {
  20. /**
  21. * 标题
  22. *
  23. * @return string
  24. */
  25. protected function title()
  26. {
  27. return '奖励组管理';
  28. }
  29. /**
  30. * 创建表格
  31. *
  32. * @return Grid
  33. */
  34. protected function grid()
  35. {
  36. return Grid::make(new GameRewardGroupRepository(), function (Grid $grid) {
  37. $grid->column('id', 'ID')->sortable();
  38. $grid->column('name', '名称');
  39. $grid->column('code', '编码');
  40. $grid->column('description', '描述')->limit(30);
  41. $grid->column('is_random', '随机发放')->switch();
  42. $grid->column('random_count', '随机数量');
  43. $grid->column('created_at', '创建时间');
  44. $grid->column('updated_at', '更新时间');
  45. $grid->filter(function (Grid\Filter $filter) {
  46. $filter->equal('id', 'ID');
  47. $filter->like('name', '名称');
  48. $filter->like('code', '编码');
  49. $filter->equal('is_random', '随机发放')->select([0 => '否', 1 => '是']);
  50. });
  51. // 使用 RowAction 类添加行操作按钮
  52. $grid->actions(function (Grid\Displayers\Actions $actions) {
  53. // 添加复制按钮
  54. $actions->append(new DuplicateRewardGroupAction());
  55. $actions->disableDelete();
  56. });
  57. });
  58. }
  59. /**
  60. * 创建详情页
  61. *
  62. * @param mixed $id
  63. * @return Show
  64. */
  65. protected function detail($id)
  66. {
  67. return Show::make($id, new GameRewardGroupRepository(), function (Show $show) {
  68. $show->field('id', 'ID');
  69. $show->field('name', '名称');
  70. $show->field('code', '编码');
  71. $show->field('description', '描述');
  72. $show->field('is_random', '随机发放')->as(function ($value) {
  73. return $value ? '是' : '否';
  74. });
  75. $show->field('random_count', '随机数量');
  76. $show->field('created_at', '创建时间');
  77. $show->field('updated_at', '更新时间');
  78. // 显示奖励项
  79. $show->divider();
  80. // 保存控制器实例的引用,以便在闭包中使用
  81. $controller = $this;
  82. $show->field('奖励项')->unescape()->as(function () use ($controller) {
  83. $items = GameRewardItem::where('group_id', $this->getKey())->get();
  84. if ($items->isEmpty()) {
  85. return '无奖励项';
  86. }
  87. $headers = ['ID', '奖励类型', '奖励内容', '数量', '权重', '必中'];
  88. $rows = [];
  89. foreach ($items as $item) {
  90. // 根据奖励类型获取实际内容,使用控制器实例调用方法
  91. $rewardContent = $controller->getRewardContent($item);
  92. $rows[] = [
  93. $item->id,
  94. REWARD_TYPE::getName($item->reward_type),
  95. $rewardContent,
  96. $item->quantity,
  97. $item->weight,
  98. $item->is_guaranteed ? '是' : '否'
  99. ];
  100. }
  101. // 创建HTML表格
  102. $html = '<div class="table-responsive"><table class="table table-bordered">';
  103. // 添加表头
  104. $html .= '<thead><tr>';
  105. foreach ($headers as $header) {
  106. $html .= "<th>{$header}</th>";
  107. }
  108. $html .= '</tr></thead>';
  109. // 添加表体
  110. $html .= '<tbody>';
  111. foreach ($rows as $row) {
  112. $html .= '<tr>';
  113. foreach ($row as $cell) {
  114. $html .= "<td>{$cell}</td>";
  115. }
  116. $html .= '</tr>';
  117. }
  118. $html .= '</tbody>';
  119. $html .= '</table></div>';
  120. return $html;
  121. });
  122. });
  123. }
  124. /**
  125. * 根据奖励项获取实际内容描述
  126. *
  127. * @param GameRewardItem $item 奖励项
  128. * @return string 奖励内容描述
  129. */
  130. public function getRewardContent(GameRewardItem $item): string
  131. {
  132. switch ($item->reward_type) {
  133. case REWARD_TYPE::ITEM->value:
  134. // 物品奖励
  135. try {
  136. $gameItem = Item::find($item->target_id);
  137. if ($gameItem) {
  138. $content = "<strong>{$gameItem->name}</strong> (ID: {$item->target_id})";
  139. // 添加参数说明
  140. $params = [];
  141. if ($item->param1 > 0) {
  142. $params[] = "品质: {$item->param1}";
  143. }
  144. if ($item->param2 > 0) {
  145. $params[] = "绑定: {$item->param2}";
  146. }
  147. if (!empty($params)) {
  148. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  149. }
  150. return $content;
  151. }
  152. } catch (\Exception $e) {
  153. // 忽略异常,返回默认内容
  154. }
  155. return "物品 (ID: {$item->target_id})";
  156. case REWARD_TYPE::CURRENCY->value:
  157. // 货币奖励
  158. try {
  159. $currency = FundCurrencyModel::find($item->target_id);
  160. if ($currency) {
  161. $content = "<strong>{$currency->name}</strong> (ID: {$item->target_id})";
  162. // 添加参数说明
  163. $params = [];
  164. if ($item->param1 > 0) {
  165. $params[] = "来源: {$item->param1}";
  166. }
  167. if (!empty($params)) {
  168. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  169. }
  170. return $content;
  171. }
  172. } catch (\Exception $e) {
  173. // 忽略异常,返回默认内容
  174. }
  175. return "货币 (ID: {$item->target_id})";
  176. case REWARD_TYPE::PET_EXP->value:
  177. // 宠物经验奖励
  178. return "宠物经验 (宠物ID: {$item->target_id})";
  179. case REWARD_TYPE::PET_ENERGY->value:
  180. // 宠物体力奖励
  181. return "宠物体力 (宠物ID: {$item->target_id})";
  182. case REWARD_TYPE::OTHER->value:
  183. // 其他奖励
  184. return "其他奖励 (ID: {$item->target_id})";
  185. default:
  186. return "未知奖励类型 (ID: {$item->target_id})";
  187. }
  188. }
  189. /**
  190. * 创建表单
  191. *
  192. * @return Form
  193. */
  194. protected function form()
  195. {
  196. return Form::make(new GameRewardGroupRepository(), function (Form $form) {
  197. $form->display('id', 'ID');
  198. $form->text('name', '名称')->required();
  199. $form->text('code', '编码')->required()->rules('required|alpha_dash|unique:game_reward_groups,code,' . $form->getKey());
  200. $form->textarea('description', '描述');
  201. $form->switch('is_random', '随机发放')->default(0);
  202. $form->number('random_count', '随机数量')->default(1)->min(1)->help('随机发放时的奖励数量,仅当随机发放开启时有效');
  203. $form->display('created_at', '创建时间');
  204. $form->display('updated_at', '更新时间');
  205. // 添加提示信息,告知用户如何管理奖励项
  206. if ($form->isEditing()) {
  207. $id = $form->getKey();
  208. $form->html('<div class="alert alert-info">
  209. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请保存当前表单后使用以下链接管理奖励项:</p>
  210. <a href="'.admin_url('game-reward-items?group_id='.$id).'" class="btn btn-primary" target="_blank">
  211. <i class="fa fa-list"></i> 管理奖励项
  212. </a>
  213. </div>');
  214. } else {
  215. $form->html('<div class="alert alert-info">
  216. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请先保存当前表单,然后才能添加奖励项。</p>
  217. </div>');
  218. }
  219. });
  220. }
  221. }