UserOutputCounterController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Repositorys\ItemUserOutputCounterRepository;
  4. use App\Module\GameItems\Repositorys\ItemOutputLimitRepository;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Layout\Content;
  8. use Spatie\RouteAttributes\Attributes\Resource;
  9. use UCore\DcatAdmin\AdminController;
  10. use UCore\DcatAdmin\FilterHelper;
  11. use UCore\DcatAdmin\GridHelper;
  12. use UCore\DcatAdmin\ShowHelper;
  13. #[Resource('game-items-user-output-counters', names: 'dcat.admin.game-items-user-output-counters')]
  14. class UserOutputCounterController extends AdminController
  15. {
  16. /**
  17. * 标题
  18. *
  19. * @var string
  20. */
  21. protected $title = '用户产出限制计数';
  22. /**
  23. * 禁用创建按钮
  24. *
  25. * @var bool
  26. */
  27. protected $showCreateButton = false;
  28. /**
  29. * 列表页
  30. *
  31. * @return Grid
  32. */
  33. protected function grid()
  34. {
  35. return Grid::make(new ItemUserOutputCounterRepository(), function (Grid $grid) {
  36. $helper = new GridHelper($grid, $this);
  37. // 禁用创建、编辑和删除按钮
  38. $grid->disableCreateButton();
  39. $grid->disableActions();
  40. $grid->disableBatchDelete();
  41. $grid->disableDeleteButton();
  42. $grid->disableEditButton();
  43. // 只保留详情按钮
  44. $grid->actions(function (Grid\Displayers\Actions $actions) {
  45. $actions->disableDelete();
  46. $actions->disableEdit();
  47. $actions->disableQuickEdit();
  48. });
  49. $helper->columnId();
  50. $grid->column('user_id', '用户ID');
  51. $grid->column('outputLimit.item.name', '物品名称');
  52. $grid->column('current_count', '当前计数');
  53. $grid->column('last_reset_time', '上次重置时间');
  54. $grid->column('created_at', '创建时间');
  55. $grid->column('updated_at', '更新时间');
  56. // 筛选
  57. $grid->filter(function ($filter) {
  58. $helper = new FilterHelper($filter, $this);
  59. $helper->equal('id', 'ID');
  60. $helper->equal('user_id', '用户ID');
  61. $filter->equal('limit_id', '限制ID')->select(
  62. (new ItemOutputLimitRepository())->with('item')->get()->pluck('item.name', 'id')
  63. );
  64. $helper->between('current_count', '当前计数');
  65. $helper->between('last_reset_time', '上次重置时间')->datetime();
  66. });
  67. });
  68. }
  69. /**
  70. * 详情页
  71. *
  72. * @param mixed $id
  73. * @param Content $content
  74. * @return Content
  75. */
  76. public function show($id, Content $content)
  77. {
  78. return $content
  79. ->header($this->title)
  80. ->description('详情')
  81. ->body($this->detail($id));
  82. }
  83. /**
  84. * 详情页
  85. *
  86. * @param mixed $id
  87. * @return Show
  88. */
  89. protected function detail($id)
  90. {
  91. return Show::make((new ItemUserOutputCounterRepository())->findOrFail($id), function (Show $show) {
  92. $helper = new ShowHelper($show, $this);
  93. // 禁用编辑和删除按钮
  94. $show->panel()->tools(function ($tools) {
  95. $tools->disableEdit();
  96. $tools->disableDelete();
  97. });
  98. $helper->field('id', 'ID');
  99. $helper->field('user_id', '用户ID');
  100. // 显示限制信息
  101. $helper->field('limit_id', '限制ID');
  102. $show->field('outputLimit.item.name', '物品名称');
  103. $show->field('outputLimit.limit_type', '限制类型')->as(function ($value) {
  104. $types = [
  105. ItemOutputLimit::LIMIT_TYPE_GLOBAL => '全局限制',
  106. ItemOutputLimit::LIMIT_TYPE_USER => '用户限制',
  107. ItemOutputLimit::LIMIT_TYPE_DAILY => '每日限制',
  108. ItemOutputLimit::LIMIT_TYPE_WEEKLY => '每周限制',
  109. ItemOutputLimit::LIMIT_TYPE_MONTHLY => '每月限制',
  110. ];
  111. return $types[$value] ?? '未知';
  112. });
  113. $show->field('outputLimit.max_quantity', '最大数量');
  114. $helper->field('current_count', '当前计数');
  115. // 计算剩余可获取数量
  116. $show->field('remaining_count', '剩余可获取数量')->as(function () {
  117. if (!$this->outputLimit) {
  118. return '未知';
  119. }
  120. $remaining = max(0, $this->outputLimit->max_quantity - $this->current_count);
  121. return $remaining;
  122. });
  123. $helper->field('last_reset_time', '上次重置时间');
  124. $helper->field('created_at', '创建时间');
  125. $helper->field('updated_at', '更新时间');
  126. });
  127. }
  128. }