CraftLogController.php 5.0 KB

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