DismantleRuleController.php 8.5 KB

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