DismantleRuleController.php 8.9 KB

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