UserRecipeController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Models\ItemUserRecipe;
  4. use App\Module\GameItems\Models\ItemRecipe;
  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-recipes', names: 'dcat.admin.game-items-user-recipes')]
  14. class UserRecipeController 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 ItemUserRecipe(), 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. $helper->column('user_id', '用户ID');
  51. $grid->column('recipe.name', '配方名称');
  52. $helper->column('is_unlocked', '是否解锁')->switch();
  53. $helper->column('unlock_time', '解锁时间');
  54. $helper->column('craft_count', '合成次数');
  55. $helper->column('last_craft_time', '上次合成时间');
  56. $helper->column('created_at', '创建时间');
  57. $helper->column('updated_at', '更新时间');
  58. // 筛选
  59. $grid->filter(function ($filter) {
  60. $helper = new FilterHelper($filter, $this);
  61. $helper->equal('id', 'ID');
  62. $helper->equal('user_id', '用户ID');
  63. $filter->equal('recipe_id', '配方')->select(
  64. ItemRecipe::pluck('name', 'id')
  65. );
  66. $filter->equal('is_unlocked', '是否解锁')->radio([
  67. 1 => '是',
  68. 0 => '否',
  69. ]);
  70. $helper->between('unlock_time', '解锁时间')->datetime();
  71. $helper->between('craft_count', '合成次数');
  72. $helper->between('last_craft_time', '上次合成时间')->datetime();
  73. });
  74. });
  75. }
  76. /**
  77. * 详情页
  78. *
  79. * @param mixed $id
  80. * @param Content $content
  81. * @return Content
  82. */
  83. public function show($id, Content $content)
  84. {
  85. return $content
  86. ->header($this->title)
  87. ->description('详情')
  88. ->body($this->detail($id));
  89. }
  90. /**
  91. * 详情页
  92. *
  93. * @param mixed $id
  94. * @return Show
  95. */
  96. protected function detail($id)
  97. {
  98. return Show::make(ItemUserRecipe::findOrFail($id), function (Show $show) {
  99. $helper = new ShowHelper($show, $this);
  100. // 禁用编辑和删除按钮
  101. $show->panel()->tools(function ($tools) {
  102. $tools->disableEdit();
  103. $tools->disableDelete();
  104. });
  105. $helper->field('id', 'ID');
  106. $helper->field('user_id', '用户ID');
  107. // 显示配方信息
  108. $show->field('recipe.name', '配方名称');
  109. $show->field('recipe.resultItem.name', '产出物品');
  110. $helper->field('recipe.result_quantity', '产出数量');
  111. $show->field('recipe.success_rate', '成功率')->as(function ($value) {
  112. return ($value * 100) . '%';
  113. });
  114. $show->field('recipe.cooldown_seconds', '冷却时间(秒)');
  115. $show->field('is_unlocked', '是否解锁')->as(function ($value) {
  116. return $value ? '是' : '否';
  117. });
  118. $helper->field('unlock_time', '解锁时间');
  119. $helper->field('craft_count', '合成次数');
  120. $helper->field('last_craft_time', '上次合成时间');
  121. // 计算冷却状态
  122. $show->field('cooldown_status', '冷却状态')->as(function () {
  123. if (!$this->recipe || $this->recipe->cooldown_seconds <= 0) {
  124. return '无冷却';
  125. }
  126. if (!$this->last_craft_time) {
  127. return '未合成过';
  128. }
  129. $cooldownEnd = $this->last_craft_time->addSeconds($this->recipe->cooldown_seconds);
  130. if ($cooldownEnd->isPast()) {
  131. return '已冷却完成';
  132. } else {
  133. $remainingSeconds = now()->diffInSeconds($cooldownEnd, false);
  134. return '冷却中,剩余 ' . $remainingSeconds . ' 秒';
  135. }
  136. });
  137. $helper->field('created_at', '创建时间');
  138. $helper->field('updated_at', '更新时间');
  139. });
  140. }
  141. }