DismantleRuleController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Models\ItemDismantleRule;
  4. use App\Module\GameItems\Models\ItemCategory;
  5. use Dcat\Admin\Form;
  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-dismantle-rules', names: 'dcat.admin.game-items-dismantle-rules')]
  12. class DismantleRuleController extends AdminController
  13. {
  14. /**
  15. * 标题
  16. *
  17. * @var string
  18. */
  19. protected $title = '物品分解规则管理';
  20. /**
  21. * 列表页
  22. *
  23. * @return Grid
  24. */
  25. protected function grid()
  26. {
  27. $grid = new Grid(new ItemDismantleRule());
  28. $grid->column('id', 'ID')->sortable();
  29. $grid->column('rule_type', '规则类型')->display(function () {
  30. if (!empty($this->item_id)) {
  31. return '物品规则';
  32. } elseif (!empty($this->category_id)) {
  33. return '分类规则';
  34. } else {
  35. return '未知';
  36. }
  37. });
  38. $grid->column('item.name', '物品名称');
  39. $grid->column('category.name', '分类名称');
  40. $grid->column('priority', '优先级')->sortable();
  41. $grid->column('coin_return_rate', '金币返还率')->display(function ($value) {
  42. return $value * 100 . '%';
  43. });
  44. $grid->column('results', '结果数量')->display(function ($results) {
  45. return count($results);
  46. });
  47. $grid->column('is_active', '是否启用')->switch();
  48. $grid->column('created_at', '创建时间');
  49. $grid->column('updated_at', '更新时间');
  50. // 筛选
  51. $grid->filter(function ($filter) {
  52. $filter->equal('id', 'ID');
  53. $filter->equal('item_id', '物品')->select(
  54. ItemItem::pluck('name', 'id')
  55. );
  56. $filter->equal('category_id', '分类')->select(
  57. ItemCategory::pluck('name', 'id')
  58. );
  59. $filter->where('rule_type', function ($query) {
  60. if ($this->input == 'item') {
  61. $query->whereNotNull('item_id')->whereNull('category_id');
  62. } elseif ($this->input == 'category') {
  63. $query->whereNotNull('category_id')->whereNull('item_id');
  64. }
  65. }, '规则类型')->select([
  66. 'item' => '物品规则',
  67. 'category' => '分类规则',
  68. ]);
  69. $filter->equal('is_active', '是否启用')->radio([
  70. 1 => '是',
  71. 0 => '否',
  72. ]);
  73. });
  74. return $grid;
  75. }
  76. /**
  77. * 详情页
  78. *
  79. * @param mixed $id
  80. * @param Content $content
  81. * @return Content
  82. */
  83. public function show($id, Content $content)
  84. {
  85. return $content
  86. ->header($this->title)
  87. ->description('详情')
  88. ->body($this->detail($id));
  89. }
  90. /**
  91. * 详情页
  92. *
  93. * @param mixed $id
  94. * @return Show
  95. */
  96. protected function detail($id)
  97. {
  98. $show = new Show(ItemDismantleRule::findOrFail($id));
  99. $show->field('id', 'ID');
  100. // 规则类型
  101. $show->field('rule_type', '规则类型')->as(function () {
  102. if (!empty($this->item_id)) {
  103. return '物品规则';
  104. } elseif (!empty($this->category_id)) {
  105. return '分类规则';
  106. } else {
  107. return '未知';
  108. }
  109. });
  110. // 根据规则类型显示不同字段
  111. if ($show->getModel()->item_id) {
  112. $show->field('item.name', '物品名称');
  113. } elseif ($show->getModel()->category_id) {
  114. $show->field('category.name', '分类名称');
  115. }
  116. $show->field('priority', '优先级');
  117. $show->field('coin_return_rate', '金币返还率')->as(function ($value) {
  118. return $value * 100 . '%';
  119. });
  120. $show->field('is_active', '是否启用')->as(function ($value) {
  121. return $value ? '是' : '否';
  122. });
  123. $show->field('created_at', '创建时间');
  124. $show->field('updated_at', '更新时间');
  125. // 显示分解结果
  126. $show->divider('分解结果');
  127. $show->field('results', '结果列表')->as(function ($results) {
  128. $html = '<table class="table table-bordered">';
  129. $html .= '<thead><tr><th>物品名称</th><th>最小数量</th><th>最大数量</th><th>概率</th></tr></thead>';
  130. $html .= '<tbody>';
  131. foreach ($results as $result) {
  132. $html .= '<tr>';
  133. $html .= '<td>' . $result->resultItem->name . '</td>';
  134. $html .= '<td>' . $result->min_quantity . '</td>';
  135. $html .= '<td>' . $result->max_quantity . '</td>';
  136. $html .= '<td>' . ($result->chance * 100) . '%</td>';
  137. $html .= '</tr>';
  138. }
  139. $html .= '</tbody></table>';
  140. return $html;
  141. })->unescape();
  142. return $show;
  143. }
  144. /**
  145. * 创建页
  146. *
  147. * @param Content $content
  148. * @return Content
  149. */
  150. public function create(Content $content)
  151. {
  152. return $content
  153. ->header($this->title)
  154. ->description('创建')
  155. ->body($this->form());
  156. }
  157. /**
  158. * 编辑页
  159. *
  160. * @param mixed $id
  161. * @param Content $content
  162. * @return Content
  163. */
  164. public function edit($id, Content $content)
  165. {
  166. return $content
  167. ->header($this->title)
  168. ->description('编辑')
  169. ->body($this->form()->edit($id));
  170. }
  171. /**
  172. * 表单
  173. *
  174. * @return Form
  175. */
  176. protected function form()
  177. {
  178. $form = new Form(new ItemDismantleRule());
  179. // 规则类型
  180. $form->radio('rule_type', '规则类型')
  181. ->options(['item' => '物品规则', 'category' => '分类规则'])
  182. ->default('item')
  183. ->when('item', function (Form $form) {
  184. $form->select('item_id', '物品')
  185. ->options(ItemItem::pluck('name', 'id'))
  186. ->required();
  187. })
  188. ->when('category', function (Form $form) {
  189. $form->select('category_id', '分类')
  190. ->options(ItemCategory::pluck('name', 'id'))
  191. ->required();
  192. });
  193. $form->number('priority', '优先级')
  194. ->default(0)
  195. ->help('数字越大优先级越高,当物品同时匹配多个规则时,使用优先级最高的规则');
  196. $form->rate('coin_return_rate', '金币返还率')
  197. ->default(0.5)
  198. ->help('分解时返还的金币比例,基于物品的sell_price计算');
  199. $form->switch('is_active', '是否启用')
  200. ->default(true);
  201. // 分解结果
  202. $form->hasMany('results', '分解结果', function (Form\NestedForm $form) {
  203. $form->select('result_item_id', '物品')
  204. ->options(ItemItem::pluck('name', 'id'))
  205. ->required();
  206. $form->number('min_quantity', '最小数量')
  207. ->default(1)
  208. ->min(0)
  209. ->required();
  210. $form->number('max_quantity', '最大数量')
  211. ->default(1)
  212. ->min(0)
  213. ->required();
  214. $form->rate('chance', '概率')
  215. ->default(1)
  216. ->help('获得该物品的概率,1表示100%');
  217. });
  218. // 保存前回调
  219. $form->saving(function (Form $form) {
  220. // 根据规则类型设置对应的字段
  221. if ($form->rule_type == 'item') {
  222. $form->category_id = null;
  223. } else {
  224. $form->item_id = null;
  225. }
  226. });
  227. return $form;
  228. }
  229. }