GameRewardLogController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Widgets\Card;
  11. use Dcat\Admin\Widgets\Table;
  12. use Spatie\RouteAttributes\Attributes\Resource;
  13. /**
  14. * 奖励日志管理控制器
  15. */
  16. #[Resource('game-reward-logs', names: 'dcat.admin.game-reward-logs')]
  17. class GameRewardLogController 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 GameRewardLogRepository(), function (Grid $grid) {
  36. $grid->column('id', 'ID')->sortable();
  37. $grid->column('user_id', '用户ID');
  38. $grid->column('group_id', '奖励组')->display(function ($groupId) {
  39. $group = GameRewardGroup::find($groupId);
  40. return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
  41. });
  42. $grid->column('source_type', '来源类型')->display(function ($type) {
  43. return REWARD_SOURCE_TYPE::getName($type);
  44. });
  45. $grid->column('source_id', '来源ID');
  46. $grid->column('reward_items', '奖励项')->display(function ($items) {
  47. $items = json_decode($items, true);
  48. if (empty($items)) {
  49. return '无奖励项';
  50. }
  51. $count = count($items);
  52. return "<span class=\"badge badge-primary\">{$count}个奖励项</span>";
  53. });
  54. $grid->column('created_at', '创建时间');
  55. $grid->filter(function (Grid\Filter $filter) {
  56. $filter->equal('id', 'ID');
  57. $filter->equal('user_id', '用户ID');
  58. $filter->equal('group_id', '奖励组')->select(
  59. GameRewardGroup::pluck('name', 'id')
  60. );
  61. $filter->equal('source_type', '来源类型')->select(REWARD_SOURCE_TYPE::getAll());
  62. $filter->equal('source_id', '来源ID');
  63. $filter->between('created_at', '创建时间')->datetime();
  64. });
  65. // 禁用创建按钮
  66. $grid->disableCreateButton();
  67. // 禁用编辑和删除
  68. $grid->actions(function (Grid\Displayers\Actions $actions) {
  69. $actions->disableEdit();
  70. $actions->disableDelete();
  71. });
  72. });
  73. }
  74. /**
  75. * 创建详情页
  76. *
  77. * @param mixed $id
  78. * @return Show
  79. */
  80. protected function detail($id)
  81. {
  82. return Show::make($id, new GameRewardLogRepository(), function (Show $show) {
  83. $show->field('id', 'ID');
  84. $show->field('user_id', '用户ID');
  85. $show->field('group_id', '奖励组')->as(function ($groupId) {
  86. $group = GameRewardGroup::find($groupId);
  87. return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
  88. });
  89. $show->field('source_type', '来源类型')->as(function ($type) {
  90. return REWARD_SOURCE_TYPE::getName($type);
  91. });
  92. $show->field('source_id', '来源ID');
  93. $show->field('created_at', '创建时间');
  94. // 显示奖励项
  95. $show->divider();
  96. $show->field('奖励项')->as(function () {
  97. $items = json_decode($this->reward_items, true);
  98. if (empty($items)) {
  99. return '无奖励项';
  100. }
  101. $headers = ['奖励类型', '目标ID', '参数1', '参数2', '数量', '额外数据'];
  102. $rows = [];
  103. foreach ($items as $item) {
  104. $rows[] = [
  105. REWARD_TYPE::getName($item['reward_type']),
  106. $item['target_id'],
  107. $item['param1'] ?? 0,
  108. $item['param2'] ?? 0,
  109. $item['quantity'],
  110. json_encode($item['extra_data'] ?? null)
  111. ];
  112. }
  113. return Card::make(
  114. Table::make($headers, $rows)
  115. );
  116. });
  117. });
  118. }
  119. }