CraftLogController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $grid = new Grid(new ItemCraftLog());
  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. * @param mixed $id
  77. * @param Content $content
  78. * @return Content
  79. */
  80. public function show($id, Content $content)
  81. {
  82. return $content
  83. ->header($this->title)
  84. ->description('详情')
  85. ->body($this->detail($id));
  86. }
  87. /**
  88. * 详情页
  89. *
  90. * @param mixed $id
  91. * @return Show
  92. */
  93. protected function detail($id)
  94. {
  95. $show = new Show(ItemCraftLog::findOrFail($id));
  96. // 禁用编辑和删除按钮
  97. $show->panel()->tools(function ($tools) {
  98. $tools->disableEdit();
  99. $tools->disableDelete();
  100. });
  101. $show->field('id', 'ID');
  102. $show->field('user_id', '用户ID');
  103. $show->field('recipe.name', '配方名称');
  104. $show->field('is_success', '是否成功')->as(function ($value) {
  105. return $value ? '是' : '否';
  106. });
  107. // 显示使用的材料
  108. $show->field('materials_used', '使用的材料')->as(function ($materials) {
  109. if (empty($materials)) {
  110. return '无';
  111. }
  112. if (is_string($materials)) {
  113. $materials = json_decode($materials, true);
  114. }
  115. if (is_array($materials)) {
  116. $html = '<table class="table table-bordered">';
  117. $html .= '<thead><tr><th>物品ID</th><th>物品名称</th><th>数量</th></tr></thead>';
  118. $html .= '<tbody>';
  119. foreach ($materials as $material) {
  120. $itemInfo = ItemItem::find($material['item_id']);
  121. $itemName = $itemInfo ? $itemInfo->name : '未知物品';
  122. $html .= '<tr>';
  123. $html .= '<td>' . $material['item_id'] . '</td>';
  124. $html .= '<td>' . $itemName . '</td>';
  125. $html .= '<td>' . $material['quantity'] . '</td>';
  126. $html .= '</tr>';
  127. }
  128. $html .= '</tbody></table>';
  129. return $html;
  130. }
  131. return $materials;
  132. })->unescape();
  133. $show->field('resultItem.name', '产出物品');
  134. $show->field('result_instance_id', '产出实例ID');
  135. $show->field('result_quantity', '产出数量');
  136. $show->field('craft_time', '合成时间');
  137. $show->field('ip_address', 'IP地址');
  138. $show->field('device_info', '设备信息');
  139. $show->field('created_at', '创建时间');
  140. return $show;
  141. }
  142. }