CraftLogController.php 5.6 KB

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