ChestOpenLogController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Repositorys\ItemChestOpenLogRepository;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Show;
  6. use UCore\DcatAdmin\AdminController;
  7. use Dcat\Admin\Layout\Content;
  8. use Spatie\RouteAttributes\Attributes\Resource;
  9. use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
  10. use App\Module\GameItems\AdminControllers\Helper\GridHelper;
  11. use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
  12. /**
  13. * 宝箱开启记录管理控制器
  14. *
  15. * @package App\Module\GameItems\AdminControllers
  16. */
  17. #[Resource('game-items-chest-open-logs', names: 'dcat.admin.game-items-chest-open-logs')]
  18. class ChestOpenLogController extends AdminController
  19. {
  20. /**
  21. * 标题
  22. *
  23. * @var string
  24. */
  25. protected $title = '宝箱开启记录';
  26. /**
  27. * 禁用创建按钮
  28. *
  29. * @var bool
  30. */
  31. protected $showCreateButton = false;
  32. /**
  33. * 列表页
  34. *
  35. * @return Grid
  36. */
  37. protected function grid()
  38. {
  39. return Grid::make(new ItemChestOpenLogRepository(), function (Grid $grid) {
  40. $helper = new GridHelper($grid, $this);
  41. // 禁用创建、编辑和删除按钮
  42. $grid->disableCreateButton();
  43. $grid->disableBatchDelete();
  44. $grid->disableDeleteButton();
  45. $grid->disableEditButton();
  46. // 只保留详情按钮
  47. $grid->actions(function (Grid\Displayers\Actions $actions) {
  48. $actions->disableDelete();
  49. $actions->disableEdit();
  50. $actions->disableQuickEdit();
  51. // 保留详情按钮(show)
  52. });
  53. $helper->columnId();
  54. $grid->column('user_id', '用户ID');
  55. $grid->column('chest.name', '宝箱名称');
  56. $grid->column('open_quantity', '开启数量');
  57. // 奖励内容列 - 使用RewardTypeDescriptor解析
  58. $grid->column('result_items', '奖励内容')->display(function ($resultItems) {
  59. if (empty($resultItems)) {
  60. return '无';
  61. }
  62. $rewardTexts = [];
  63. // 遍历每次开箱结果
  64. foreach ($resultItems as $index => $chestResult) {
  65. $chestRewards = [];
  66. // 遍历每个奖励物品(RewardItemDto格式)
  67. foreach ($chestResult as $item) {
  68. // 使用RewardTypeDescriptor解析奖励信息
  69. $rewardText = \App\Module\Game\Services\RewardTypeDescriptor::formatRewardDisplay(
  70. $item['rewardType'], // 奖励类型
  71. $item['targetId'], // 目标ID
  72. $item['quantity'], // 数量
  73. $item['param1'] ?? 0, // 参数1
  74. $item['param2'] ?? 0, // 参数2
  75. false // 不使用HTML徽章样式
  76. );
  77. $chestRewards[] = $rewardText;
  78. }
  79. if (count($resultItems) > 1) {
  80. $rewardTexts[] = '第' . ($index + 1) . '次: ' . implode(', ', $chestRewards);
  81. } else {
  82. $rewardTexts = $chestRewards;
  83. }
  84. }
  85. return implode('<br>', $rewardTexts);
  86. });
  87. $grid->column('open_time', '开启时间')->sortable();
  88. $grid->column('ip_address', 'IP地址');
  89. $grid->column('created_at', '创建时间');
  90. // 筛选
  91. $grid->filter(function ($filter) {
  92. $helper = new FilterHelper($filter, $this);
  93. $helper->equal('id', 'ID');
  94. $helper->equal('user_id', '用户ID');
  95. $filter->equal('chest_id', '宝箱')->select(
  96. \App\Module\GameItems\Models\Item::where('type', 5)->pluck('name', 'id')
  97. );
  98. $filter->between('open_time', '开启时间')->datetime();
  99. });
  100. });
  101. }
  102. /**
  103. * 详情页
  104. *
  105. * @param mixed $id
  106. * @param Content $content
  107. * @return Content
  108. */
  109. public function show($id, Content $content)
  110. {
  111. return $content
  112. ->header($this->title)
  113. ->description('详情')
  114. ->body($this->detail($id));
  115. }
  116. /**
  117. * 详情页
  118. *
  119. * @param mixed $id
  120. * @return Show
  121. */
  122. protected function detail($id)
  123. {
  124. return Show::make($id, new ItemChestOpenLogRepository(), function (Show $show) {
  125. $helper = new ShowHelper($show, $this);
  126. // 禁用编辑和删除按钮
  127. $show->panel()->tools(function ($tools) {
  128. $tools->disableEdit();
  129. $tools->disableDelete();
  130. });
  131. $helper->field('id', 'ID');
  132. $helper->field('user_id', '用户ID');
  133. $show->field('chest.name', '宝箱名称');
  134. $helper->field('open_quantity', '开启数量');
  135. // 显示奖励内容
  136. $show->field('result_items', '奖励内容')->as(function ($resultItems) {
  137. if (empty($resultItems)) {
  138. return '无';
  139. }
  140. if (is_string($resultItems)) {
  141. $resultItems = json_decode($resultItems, true);
  142. }
  143. if (is_array($resultItems)) {
  144. $html = '';
  145. foreach ($resultItems as $index => $chestResult) {
  146. $html .= '<h4>第' . ($index + 1) . '次开箱</h4>';
  147. $html .= '<table class="table table-bordered">';
  148. $html .= '<thead><tr><th>奖励类型</th><th>奖励名称</th><th>数量</th></tr></thead>';
  149. $html .= '<tbody>';
  150. foreach ($chestResult as $item) {
  151. // 使用RewardTypeDescriptor解析奖励信息(RewardItemDto格式)
  152. $rewardTypeName = \App\Module\Game\Enums\REWARD_TYPE::getName($item['rewardType']);
  153. $targetName = \App\Module\Game\Services\RewardTypeDescriptor::getTargetName(
  154. $item['rewardType'],
  155. $item['targetId'],
  156. $item['param1'] ?? 0,
  157. $item['param2'] ?? 0
  158. );
  159. $html .= '<tr>';
  160. $html .= '<td>' . $rewardTypeName . '</td>';
  161. $html .= '<td>' . $targetName . '</td>';
  162. $html .= '<td>' . $item['quantity'] . '</td>';
  163. $html .= '</tr>';
  164. }
  165. $html .= '</tbody></table>';
  166. }
  167. return $html;
  168. }
  169. return $resultItems;
  170. });
  171. $helper->field('open_time', '开启时间');
  172. $helper->field('ip_address', 'IP地址');
  173. $helper->field('device_info', '设备信息');
  174. $helper->field('created_at', '创建时间');
  175. });
  176. }
  177. }