CraftLogController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Repositorys\ItemCraftLogRepository;
  4. use App\Module\GameItems\Repositorys\ItemRecipeRepository;
  5. use App\Module\GameItems\Repositorys\ItemRepository;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use UCore\DcatAdmin\AdminController;
  9. use Dcat\Admin\Layout\Content;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use UCore\DcatAdmin\FilterHelper;
  12. use UCore\DcatAdmin\FormHelper;
  13. use UCore\DcatAdmin\GridHelper;
  14. use UCore\DcatAdmin\ShowHelper;
  15. /**
  16. * 物品合成记录管理控制器
  17. *
  18. * @package App\Module\GameItems\AdminControllers
  19. */
  20. #[Resource('game-items-craft-logs', names: 'dcat.admin.game-items-craft-logs')]
  21. class CraftLogController extends AdminController
  22. {
  23. /**
  24. * 标题
  25. *
  26. * @var string
  27. */
  28. protected $title = '物品合成记录';
  29. /**
  30. * 禁用创建按钮
  31. *
  32. * @var bool
  33. */
  34. protected $showCreateButton = false;
  35. /**
  36. * 列表页
  37. *
  38. * @return Grid
  39. */
  40. protected function grid()
  41. {
  42. return Grid::make(new ItemCraftLogRepository(), function (Grid $grid) {
  43. $helper = new GridHelper($grid, $this);
  44. // 禁用创建、编辑和删除按钮
  45. $grid->disableCreateButton();
  46. $grid->disableActions();
  47. $grid->disableBatchDelete();
  48. $grid->disableDeleteButton();
  49. $grid->disableEditButton();
  50. // 只保留详情按钮
  51. $grid->actions(function (Grid\Displayers\Actions $actions) {
  52. $actions->disableDelete();
  53. $actions->disableEdit();
  54. $actions->disableQuickEdit();
  55. });
  56. $helper->columnId();
  57. $grid->column('user_id', '用户ID');
  58. $grid->column('recipe.name', '配方名称');
  59. $grid->column('is_success', '是否成功')->switch();
  60. $grid->column('resultItem.name', '产出物品');
  61. $grid->column('result_instance_id', '产出实例ID');
  62. $grid->column('result_quantity', '产出数量');
  63. $grid->column('craft_time', '合成时间')->sortable();
  64. $grid->column('ip_address', 'IP地址');
  65. $grid->column('created_at', '创建时间');
  66. // 筛选
  67. $grid->filter(function ($filter) {
  68. $helper = new \App\Module\GameItems\AdminControllers\Helper\FilterHelper($filter, $this);
  69. $helper->equal('id', 'ID');
  70. $helper->equal('user_id', '用户ID');
  71. $helper->equalSelectModelRecipe('recipe_id', '配方');
  72. $filter->equal('is_success', '是否成功')->radio([
  73. 1 => '是',
  74. 0 => '否',
  75. ]);
  76. $helper->equalSelectModelItem('result_item_id', '产出物品');
  77. $filter->between('craft_time', '合成时间')->datetime();
  78. });
  79. });
  80. }
  81. /**
  82. * 详情页
  83. *
  84. * @param mixed $id
  85. * @param Content $content
  86. * @return Content
  87. */
  88. public function show($id, Content $content)
  89. {
  90. return $content
  91. ->header($this->title)
  92. ->description('详情')
  93. ->body($this->detail($id));
  94. }
  95. /**
  96. * 详情页
  97. *
  98. * @param mixed $id
  99. * @return Show
  100. */
  101. protected function detail($id)
  102. {
  103. return Show::make($id, new ItemCraftLogRepository(), function (Show $show) {
  104. $helper = new ShowHelper($show, $this);
  105. // 禁用编辑和删除按钮
  106. $show->panel()->tools(function ($tools) {
  107. $tools->disableEdit();
  108. $tools->disableDelete();
  109. });
  110. $helper->field('id', 'ID');
  111. $helper->field('user_id', '用户ID');
  112. $show->field('recipe.name', '配方名称');
  113. $helper->field('is_success', '是否成功')->as(function ($value) {
  114. return $value ? '是' : '否';
  115. });
  116. // 显示使用的材料
  117. $show->field('materials_used', '使用的材料')->as(function ($materials) {
  118. if (empty($materials)) {
  119. return '无';
  120. }
  121. if (is_string($materials)) {
  122. $materials = json_decode($materials, true);
  123. }
  124. if (is_array($materials)) {
  125. $html = '<table class="table table-bordered">';
  126. $html .= '<thead><tr><th>物品ID</th><th>物品名称</th><th>数量</th></tr></thead>';
  127. $html .= '<tbody>';
  128. foreach ($materials as $material) {
  129. $itemInfo = (new ItemRepository())->find($material['item_id']);
  130. $itemName = $itemInfo ? $itemInfo->name : '未知物品';
  131. $html .= '<tr>';
  132. $html .= '<td>' . $material['item_id'] . '</td>';
  133. $html .= '<td>' . $itemName . '</td>';
  134. $html .= '<td>' . $material['quantity'] . '</td>';
  135. $html .= '</tr>';
  136. }
  137. $html .= '</tbody></table>';
  138. return $html;
  139. }
  140. return $materials;
  141. })->unescape();
  142. $show->field('resultItem.name', '产出物品');
  143. $helper->field('result_instance_id', '产出实例ID');
  144. $helper->field('result_quantity', '产出数量');
  145. $helper->field('craft_time', '合成时间');
  146. $helper->field('ip_address', 'IP地址');
  147. $helper->field('device_info', '设备信息');
  148. $helper->field('created_at', '创建时间');
  149. });
  150. }
  151. }