ShopItemController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Module\Shop\AdminControllers;
  3. use App\Module\Shop\Models\ShopCategory;
  4. use App\Module\Shop\Repositorys\ShopItemRepository;
  5. use App\Module\Game\Models\GameConsumeGroup;
  6. use App\Module\Game\Models\GameRewardGroup;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. /**
  13. * 商店商品管理控制器
  14. */
  15. #[Resource('shop/items', names: 'dcat.admin.shop.items')]
  16. class ShopItemController extends AdminController
  17. {
  18. /**
  19. * 页面标题
  20. *
  21. * @var string
  22. */
  23. protected $title = '商店商品';
  24. /**
  25. * 创建表格
  26. *
  27. * @return Grid
  28. */
  29. protected function grid()
  30. {
  31. return Grid::make(new ShopItemRepository(), function (Grid $grid) {
  32. // 预加载关联数据,包括消耗组和奖励组的详细项目
  33. $grid->model()->with(['category', 'consumeGroup.consumeItems', 'rewardGroup.rewardItems']);
  34. $grid->column('id', 'ID')->sortable();
  35. $grid->column('name', '商品名称');
  36. $grid->column('image', '商品图片')->image('', 50, 50);
  37. $grid->column('category.name', '所属分类');
  38. $grid->column('category_name', '分类名称');
  39. // 消耗组列 - 可点击跳转
  40. $grid->column('consumeGroup.name', '消耗组')->display(function ($name) {
  41. if (!$name || !$this->consume_group_id) {
  42. return '无';
  43. }
  44. return $name;
  45. })->link(function () {
  46. if (!$this->consume_group_id) {
  47. return '';
  48. }
  49. return admin_url('game-consume-groups/' . $this->consume_group_id);
  50. });
  51. // 奖励组列 - 可点击跳转
  52. $grid->column('rewardGroup.name', '奖励组')->display(function ($name) {
  53. if (!$name || !$this->reward_group_id) {
  54. return '无';
  55. }
  56. return $name;
  57. })->link(function () {
  58. if (!$this->reward_group_id) {
  59. return '';
  60. }
  61. return admin_url('game-reward-groups/' . $this->reward_group_id);
  62. });
  63. // 消耗组详情列
  64. $grid->column('consume_group_details', '消耗组详情')->display(function () {
  65. if (!$this->consumeGroup) {
  66. return '<span class="text-muted">无消耗组</span>';
  67. }
  68. return $this->consumeGroup->formatConsumeDetails();
  69. })->width('200px');
  70. // 奖励组详情列
  71. $grid->column('reward_group_details', '奖励组详情')->display(function () {
  72. if (!$this->rewardGroup) {
  73. return '<span class="text-muted">无奖励组</span>';
  74. }
  75. return $this->rewardGroup->formatRewardDetails();
  76. })->width('200px');
  77. $grid->column('max_buy', '购买限制')->display(function ($maxBuy) {
  78. return $maxBuy > 0 ? $maxBuy : '无限制';
  79. });
  80. $grid->column('sort_order', '排序权重')->sortable();
  81. $grid->column('is_active', '状态')->switch();
  82. $grid->column('start_time', '上架时间')->sortable();
  83. $grid->column('end_time', '下架时间')->sortable();
  84. // 过滤器
  85. $grid->filter(function (Grid\Filter $filter) {
  86. $filter->equal('id', 'ID');
  87. $filter->like('name', '商品名称');
  88. $filter->equal('category_id', '所属分类')->select(
  89. ShopCategory::pluck('name', 'id')
  90. );
  91. $filter->like('category_name', '分类名称');
  92. $filter->equal('consume_group_id', '消耗组')->select(
  93. GameConsumeGroup::pluck('name', 'id')
  94. );
  95. $filter->equal('reward_group_id', '奖励组')->select(
  96. GameRewardGroup::pluck('name', 'id')
  97. );
  98. $filter->equal('is_active', '状态')->select([
  99. 0 => '禁用',
  100. 1 => '启用',
  101. ]);
  102. $filter->between('start_time', '上架时间')->datetime();
  103. $filter->between('end_time', '下架时间')->datetime();
  104. });
  105. // 工具栏
  106. $grid->toolsWithOutline(false);
  107. $grid->disableViewButton();
  108. $grid->showQuickEditButton();
  109. $grid->enableDialogCreate();
  110. $grid->enableDialogEdit();
  111. $grid->setDialogFormDimensions('800px', '720px');
  112. });
  113. }
  114. /**
  115. * 创建详情页
  116. *
  117. * @param mixed $id
  118. * @return Show
  119. */
  120. protected function detail($id)
  121. {
  122. return Show::make($id, new ShopItemRepository(), function (Show $show) {
  123. $show->field('id', 'ID');
  124. $show->field('name', '商品名称');
  125. $show->field('description', '商品描述');
  126. $show->field('image', '商品图片')->image();
  127. $show->field('category.name', '所属分类');
  128. $show->field('category_name', '分类名称');
  129. $show->field('consumeGroup.name', '消耗组')->as(function ($name) {
  130. return $name ?: '无';
  131. });
  132. $show->field('rewardGroup.name', '奖励组')->as(function ($name) {
  133. return $name ?: '无';
  134. });
  135. $show->field('max_buy', '购买限制')->as(function ($maxBuy) {
  136. return $maxBuy > 0 ? $maxBuy : '无限制';
  137. });
  138. $show->field('sort_order', '排序权重');
  139. $show->field('is_active', '状态')->as(function ($isActive) {
  140. return $isActive ? '启用' : '禁用';
  141. });
  142. $show->field('start_time', '上架时间');
  143. $show->field('end_time', '下架时间');
  144. $show->field('created_at', '创建时间');
  145. $show->field('updated_at', '更新时间');
  146. });
  147. }
  148. /**
  149. * 创建表单
  150. *
  151. * @return Form
  152. */
  153. protected function form()
  154. {
  155. return Form::make(new ShopItemRepository(), function (Form $form) {
  156. $form->display('id', 'ID');
  157. $form->text('name', '商品名称')->required();
  158. $form->textarea('description', '商品描述')->rows(3);
  159. $form->image('image', '商品图片')->autoUpload()->uniqueName()->help('建议尺寸:200x200');
  160. $form->select('category_id', '所属分类')
  161. ->options(ShopCategory::pluck('name', 'id'))
  162. ->required();
  163. $form->text('category_name', '分类名称')->help('字符串格式的分类名称,区别于上面的分类ID');
  164. $form->select('consume_group_id', '消耗组')
  165. ->options(GameConsumeGroup::pluck('name', 'id'))
  166. ->help('选择购买此商品需要消耗的资源组');
  167. $form->select('reward_group_id', '奖励组')
  168. ->options(GameRewardGroup::pluck('name', 'id'))
  169. ->help('选择购买此商品获得的奖励组');
  170. $form->number('max_buy', '购买限制')->min(0)->default(0)->help('0表示无限制');
  171. $form->number('sort_order', '排序权重')->default(0)->help('数字越小越靠前');
  172. $form->switch('is_active', '状态')->default(true);
  173. $form->datetime('start_time', '上架时间')->help('留空表示不限制上架时间');
  174. $form->datetime('end_time', '下架时间')->help('留空表示不限制下架时间');
  175. $form->display('created_at', '创建时间');
  176. $form->display('updated_at', '更新时间');
  177. });
  178. }
  179. }