GameRewardLogController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  4. use App\Module\Game\Enums\REWARD_TYPE;
  5. use App\Module\Game\Models\GameRewardGroup;
  6. use App\Module\Game\Repositorys\GameRewardLogRepository;
  7. use App\Module\Game\Services\RewardSourceResolver;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Dcat\Admin\Widgets\Card;
  12. use Dcat\Admin\Widgets\Table;
  13. use Spatie\RouteAttributes\Attributes\Resource;
  14. /**
  15. * 奖励日志管理控制器
  16. */
  17. #[Resource('game-reward-logs', names: 'dcat.admin.game-reward-logs')]
  18. class GameRewardLogController 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 GameRewardLogRepository(), function (Grid $grid) {
  37. $grid->column('id', 'ID')->sortable();
  38. $grid->column('user_id', '用户ID');
  39. $grid->column('group_id', '奖励组')->display(function ($groupId) {
  40. $group = GameRewardGroup::find($groupId);
  41. return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
  42. });
  43. $grid->column('source_type', '来源类型')->display(function ($type) {
  44. return REWARD_SOURCE_TYPE::getName($type);
  45. });
  46. $grid->column('source_id', '来源ID');
  47. $grid->column('source_detail', '来源详情')->display(function () {
  48. $sourceInfo = RewardSourceResolver::resolve($this->source_type, $this->source_id);
  49. $text = $sourceInfo['name'];
  50. if ($sourceInfo['link']) {
  51. return "<a href='{$sourceInfo['link']}' target='_blank' class='text-primary'>{$text}</a>";
  52. }
  53. return $text;
  54. });
  55. $grid->column('reward_items', '奖励项')->display(function ($items) {
  56. // 由于模型中已设置了json cast,$items已经是数组,无需再次json_decode
  57. if (empty($items)) {
  58. return '无奖励项';
  59. }
  60. $count = count($items);
  61. return "<span class=\"badge badge-primary\">{$count}个奖励项</span>";
  62. });
  63. $grid->column('created_at', '创建时间');
  64. $grid->filter(function (Grid\Filter $filter) {
  65. $filter->equal('id', 'ID');
  66. $filter->equal('user_id', '用户ID');
  67. $filter->equal('group_id', '奖励组')->select(
  68. GameRewardGroup::pluck('name', 'id')
  69. );
  70. $filter->equal('source_type', '来源类型')->select(REWARD_SOURCE_TYPE::getValueDescription());
  71. $filter->equal('source_id', '来源ID');
  72. $filter->between('created_at', '创建时间')->datetime();
  73. });
  74. // 禁用创建按钮
  75. $grid->disableCreateButton();
  76. // 禁用编辑和删除
  77. $grid->actions(function (Grid\Displayers\Actions $actions) {
  78. $actions->disableEdit();
  79. $actions->disableDelete();
  80. });
  81. });
  82. }
  83. /**
  84. * 创建详情页
  85. *
  86. * @param mixed $id
  87. * @return Show
  88. */
  89. protected function detail($id)
  90. {
  91. return Show::make($id, new GameRewardLogRepository(), function (Show $show) {
  92. $show->field('id', 'ID');
  93. $show->field('user_id', '用户ID');
  94. $show->field('group_id', '奖励组')->as(function ($groupId) {
  95. $group = GameRewardGroup::find($groupId);
  96. return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
  97. });
  98. $show->field('source_type', '来源类型')->as(function ($type) {
  99. return REWARD_SOURCE_TYPE::getName($type);
  100. });
  101. $show->field('source_id', '来源ID');
  102. // 显示详细的来源信息
  103. $show->field('source_detail', '来源详情')->as(function () {
  104. $sourceInfo = RewardSourceResolver::resolve($this->source_type, $this->source_id);
  105. $html = "<div class='source-detail'>";
  106. $html .= "<h5>{$sourceInfo['type']}: {$sourceInfo['name']}</h5>";
  107. $html .= "<p class='text-muted'>{$sourceInfo['description']}</p>";
  108. if ($sourceInfo['link']) {
  109. $html .= "<p><a href='{$sourceInfo['link']}' target='_blank' class='btn btn-sm btn-primary'>查看详情</a></p>";
  110. }
  111. if (!empty($sourceInfo['extra'])) {
  112. $html .= "<div class='mt-2'>";
  113. $html .= "<small class='text-muted'>额外信息:</small><br>";
  114. foreach ($sourceInfo['extra'] as $key => $value) {
  115. $html .= "<small><strong>{$key}:</strong> {$value}</small><br>";
  116. }
  117. $html .= "</div>";
  118. }
  119. $html .= "</div>";
  120. return $html;
  121. })->unescape();
  122. $show->field('created_at', '创建时间');
  123. // 显示奖励项
  124. $show->divider();
  125. $show->field('奖励项')->as(function () {
  126. // 由于模型中已设置了json cast,reward_items已经是数组,无需再次json_decode
  127. $items = $this->reward_items;
  128. if (empty($items)) {
  129. return '无奖励项';
  130. }
  131. $headers = ['奖励类型', '目标ID', '参数1', '参数2', '数量', '额外数据'];
  132. $rows = [];
  133. foreach ($items as $item) {
  134. $rows[] = [
  135. REWARD_TYPE::getName($item['reward_type']),
  136. $item['target_id'],
  137. $item['param1'] ?? 0,
  138. $item['param2'] ?? 0,
  139. $item['quantity'],
  140. json_encode($item['extra_data'] ?? null)
  141. ];
  142. }
  143. return Card::make(
  144. Table::make($headers, $rows)
  145. );
  146. })->unescape();
  147. });
  148. }
  149. }