DismantleRuleController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. return Show::make($id, new ItemDismantleRuleRepository(), function (Show $show) {
  106. $helper = new ShowHelper($show, $this);
  107. $helper->field('id', 'ID');
  108. // 规则类型
  109. $show->field('rule_type', '规则类型')->as(function () {
  110. if (!empty($this->item_id)) {
  111. return '物品规则';
  112. } elseif (!empty($this->category_id)) {
  113. return '分类规则';
  114. } else {
  115. return '未知';
  116. }
  117. });
  118. // 根据规则类型显示不同字段
  119. if ($show->getModel()->item_id) {
  120. $show->field('item.name', '物品名称');
  121. } elseif ($show->getModel()->category_id) {
  122. $show->field('category.name', '分类名称');
  123. }
  124. $helper->field('priority', '优先级');
  125. $show->field('coin_return_rate', '金币返还率')->as(function ($value) {
  126. return $value * 100 . '%';
  127. });
  128. $show->field('is_active', '是否启用')->as(function ($value) {
  129. return $value ? '是' : '否';
  130. });
  131. $helper->field('created_at', '创建时间');
  132. $helper->field('updated_at', '更新时间');
  133. // 显示分解结果
  134. $show->divider('分解结果');
  135. $show->field('results', '结果列表')->as(function ($results) {
  136. $html = '<table class="table table-bordered">';
  137. $html .= '<thead><tr><th>物品名称</th><th>最小数量</th><th>最大数量</th><th>概率</th></tr></thead>';
  138. $html .= '<tbody>';
  139. foreach ($results as $result) {
  140. $html .= '<tr>';
  141. $html .= '<td>' . $result->resultItem->name . '</td>';
  142. $html .= '<td>' . $result->min_quantity . '</td>';
  143. $html .= '<td>' . $result->max_quantity . '</td>';
  144. $html .= '<td>' . ($result->chance * 100) . '%</td>';
  145. $html .= '</tr>';
  146. }
  147. $html .= '</tbody></table>';
  148. return $html;
  149. })->unescape();
  150. });
  151. }
  152. /**
  153. * 创建页
  154. *
  155. * @param Content $content
  156. * @return Content
  157. */
  158. public function create(Content $content)
  159. {
  160. return $content
  161. ->header($this->title)
  162. ->description('创建')
  163. ->body($this->form());
  164. }
  165. /**
  166. * 编辑页
  167. *
  168. * @param mixed $id
  169. * @param Content $content
  170. * @return Content
  171. */
  172. public function edit($id, Content $content)
  173. {
  174. return $content
  175. ->header($this->title)
  176. ->description('编辑')
  177. ->body($this->form()->edit($id));
  178. }
  179. /**
  180. * 表单
  181. *
  182. * @return Form
  183. */
  184. protected function form()
  185. {
  186. return Form::make(new ItemDismantleRuleRepository(), function (Form $form) {
  187. $helper = new FormHelper($form, $this);
  188. // 规则类型
  189. $form->radio('rule_type', '规则类型')
  190. ->options(['item' => '物品规则', 'category' => '分类规则'])
  191. ->default('item')
  192. ->when('item', function (Form $form) {
  193. $form->select('item_id', '物品')
  194. ->options(ItemItem::pluck('name', 'id'))
  195. ->required();
  196. })
  197. ->when('category', function (Form $form) {
  198. $form->select('category_id', '分类')
  199. ->options(ItemCategory::pluck('name', 'id'))
  200. ->required();
  201. });
  202. $helper->number('priority')
  203. ->default(0)
  204. ->help('数字越大优先级越高,当物品同时匹配多个规则时,使用优先级最高的规则');
  205. $form->rate('coin_return_rate', '金币返还率')
  206. ->default(0.5)
  207. ->help('分解时返还的金币比例,基于物品的sell_price计算');
  208. $form->switch('is_active', '是否启用')
  209. ->default(true);
  210. // 分解结果
  211. $form->hasMany('results', '分解结果', function (Form\NestedForm $form) {
  212. $form->select('result_item_id', '物品')
  213. ->options(ItemItem::pluck('name', 'id'))
  214. ->required();
  215. $form->number('min_quantity', '最小数量')
  216. ->default(1)
  217. ->min(0)
  218. ->required();
  219. $form->number('max_quantity', '最大数量')
  220. ->default(1)
  221. ->min(0)
  222. ->required();
  223. $form->rate('chance', '概率')
  224. ->default(1)
  225. ->help('获得该物品的概率,1表示100%');
  226. });
  227. // 保存前回调
  228. $form->saving(function (Form $form) {
  229. // 根据规则类型设置对应的字段
  230. if ($form->rule_type == 'item') {
  231. $form->category_id = null;
  232. } else {
  233. $form->item_id = null;
  234. }
  235. });
  236. });
  237. }
  238. }