CraftLogController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #[Resource('game-items-craft-logs', names: 'dcat.admin.game-items-craft-logs')]
  12. class CraftLogController extends AdminController
  13. {
  14. /**
  15. * 标题
  16. *
  17. * @var string
  18. */
  19. protected $title = '物品合成记录';
  20. /**
  21. * 禁用创建按钮
  22. *
  23. * @var bool
  24. */
  25. protected $showCreateButton = false;
  26. /**
  27. * 列表页
  28. *
  29. * @return Grid
  30. */
  31. protected function grid()
  32. {
  33. return Grid::make(new ItemCraftLog(), function (Grid $grid) {
  34. // 禁用创建、编辑和删除按钮
  35. $grid->disableCreateButton();
  36. $grid->disableActions();
  37. $grid->disableBatchDelete();
  38. $grid->disableDeleteButton();
  39. $grid->disableEditButton();
  40. // 只保留详情按钮
  41. $grid->actions(function (Grid\Displayers\Actions $actions) {
  42. $actions->disableDelete();
  43. $actions->disableEdit();
  44. $actions->disableQuickEdit();
  45. });
  46. $grid->column('id', 'ID')->sortable();
  47. $grid->column('user_id', '用户ID');
  48. $grid->column('recipe.name', '配方名称');
  49. $grid->column('is_success', '是否成功')->switch();
  50. $grid->column('resultItem.name', '产出物品');
  51. $grid->column('result_instance_id', '产出实例ID');
  52. $grid->column('result_quantity', '产出数量');
  53. $grid->column('craft_time', '合成时间')->sortable();
  54. $grid->column('ip_address', 'IP地址');
  55. $grid->column('created_at', '创建时间');
  56. // 筛选
  57. $grid->filter(function ($filter) {
  58. $filter->equal('id', 'ID');
  59. $filter->equal('user_id', '用户ID');
  60. $filter->equal('recipe_id', '配方')->select(
  61. ItemRecipe::pluck('name', 'id')
  62. );
  63. $filter->equal('is_success', '是否成功')->radio([
  64. 1 => '是',
  65. 0 => '否',
  66. ]);
  67. $filter->equal('result_item_id', '产出物品')->select(
  68. ItemItem::pluck('name', 'id')
  69. );
  70. $filter->between('craft_time', '合成时间')->datetime();
  71. });
  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(ItemCraftLog::findOrFail($id), 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. });
  142. }
  143. }