ShopPurchaseLimitController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Module\Shop\AdminControllers;
  3. use App\Module\Shop\Models\ShopPurchaseLimit;
  4. use App\Module\Shop\Models\ShopItem;
  5. use App\Module\Shop\Enums\PURCHASE_LIMIT_TYPE;
  6. use App\Module\Shop\Enums\PURCHASE_LIMIT_PERIOD;
  7. use App\Module\Shop\Repositorys\ShopPurchaseLimitRepository;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Spatie\RouteAttributes\Attributes\Resource;
  13. /**
  14. * 商店限购配置控制器
  15. *
  16. * 路由前缀: /admin/shop/purchase-limits
  17. * 路由名称: admin.shop.purchase-limits
  18. *
  19. * 提供商店限购配置的管理功能,包括:
  20. * - 限购规则的增删改查
  21. * - 限购类型和周期的配置
  22. * - 限购状态的管理
  23. */
  24. #[Resource('shop/purchase-limits', names: 'dcat.admin.shop.purchase-limits')]
  25. class ShopPurchaseLimitController extends AdminController
  26. {
  27. /**
  28. * 页面标题
  29. *
  30. * @var string
  31. */
  32. protected $title = '商店限购配置';
  33. /**
  34. * 创建表格
  35. *
  36. * @return Grid
  37. */
  38. protected function grid()
  39. {
  40. return Grid::make(new ShopPurchaseLimitRepository(), function (Grid $grid) {
  41. // 预加载关联数据
  42. $grid->model()->with(['shopItem']);
  43. $grid->column('id', 'ID')->sortable();
  44. $grid->column('shopItem.name', '商品名称')->limit(30);
  45. $grid->column('name', '限购规则名称')->limit(30);
  46. $grid->column('limit_type', '限购类型')->using(PURCHASE_LIMIT_TYPE::getAll());
  47. $grid->column('limit_period', '限购周期')->using(PURCHASE_LIMIT_PERIOD::getAll());
  48. $grid->column('max_quantity', '最大数量')->sortable();
  49. $grid->column('is_active', '状态')->switch();
  50. $grid->column('sort_order', '排序')->sortable()->editable();
  51. $grid->column('created_at', '创建时间')->sortable();
  52. // 过滤器
  53. $grid->filter(function (Grid\Filter $filter) {
  54. $filter->equal('shop_item_id', '商品')->select(
  55. ShopItem::where('is_active', true)->pluck('name', 'id')
  56. );
  57. $filter->equal('limit_type', '限购类型')->select(PURCHASE_LIMIT_TYPE::getAll());
  58. $filter->equal('limit_period', '限购周期')->select(PURCHASE_LIMIT_PERIOD::getAll());
  59. $filter->equal('is_active', '状态')->select([1 => '激活', 0 => '禁用']);
  60. });
  61. // 工具栏
  62. $grid->toolsWithOutline(false);
  63. $grid->enableDialogCreate();
  64. $grid->enableDialogEdit();
  65. $grid->setDialogFormDimensions('800px', '600px');
  66. });
  67. }
  68. /**
  69. * 创建详情页
  70. *
  71. * @param mixed $id
  72. * @return Show
  73. */
  74. protected function detail($id)
  75. {
  76. return Show::make($id, new ShopPurchaseLimitRepository(), function (Show $show) {
  77. $show->field('id', 'ID');
  78. $show->field('shopItem.name', '商品名称');
  79. $show->field('name', '限购规则名称');
  80. $show->field('description', '限购规则描述');
  81. $show->field('limit_type', '限购类型')->using(PURCHASE_LIMIT_TYPE::getAll());
  82. $show->field('limit_period', '限购周期')->using(PURCHASE_LIMIT_PERIOD::getAll());
  83. $show->field('max_quantity', '最大数量');
  84. $show->field('is_active', '状态')->using([1 => '激活', 0 => '禁用']);
  85. $show->field('sort_order', '排序权重');
  86. $show->field('created_at', '创建时间');
  87. $show->field('updated_at', '更新时间');
  88. });
  89. }
  90. /**
  91. * 创建表单
  92. *
  93. * @return Form
  94. */
  95. protected function form()
  96. {
  97. return Form::make(new ShopPurchaseLimitRepository(), function (Form $form) {
  98. $form->display('id', 'ID');
  99. $form->select('shop_item_id', '商品')
  100. ->options(ShopItem::where('is_active', true)->pluck('name', 'id'))
  101. ->required()
  102. ->help('选择要设置限购的商品');
  103. $form->text('name', '限购规则名称')
  104. ->required()
  105. ->help('为此限购规则起一个便于识别的名称');
  106. $form->textarea('description', '限购规则描述')
  107. ->rows(3)
  108. ->help('详细描述此限购规则的用途和限制条件');
  109. $form->select('limit_type', '限购类型')
  110. ->options(PURCHASE_LIMIT_TYPE::getAll())
  111. ->required()
  112. ->help('选择限购类型:单次购买限制或周期性购买限制');
  113. $form->select('limit_period', '限购周期')
  114. ->options(PURCHASE_LIMIT_PERIOD::getAll())
  115. ->default(PURCHASE_LIMIT_PERIOD::PERMANENT->value)
  116. ->help('选择限购周期,仅在周期性购买限制时有效');
  117. $form->number('max_quantity', '最大购买数量')
  118. ->min(1)
  119. ->required()
  120. ->help('设置在限购周期内允许购买的最大数量');
  121. $form->switch('is_active', '是否激活')
  122. ->default(true)
  123. ->help('是否启用此限购规则');
  124. $form->number('sort_order', '排序权重')
  125. ->default(0)
  126. ->help('数值越小排序越靠前,用于控制多个限购规则的检查顺序');
  127. $form->display('created_at', '创建时间');
  128. $form->display('updated_at', '更新时间');
  129. // 表单验证
  130. $form->saving(function (Form $form) {
  131. // 验证限购类型和周期的组合
  132. if ($form->limit_type == PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE->value
  133. && $form->limit_period != PURCHASE_LIMIT_PERIOD::PERMANENT->value) {
  134. return $form->response()->error('单次购买限制只能使用永久周期');
  135. }
  136. });
  137. });
  138. }
  139. /**
  140. * 批量切换状态
  141. *
  142. * @return \Illuminate\Http\JsonResponse
  143. */
  144. public function toggleStatus()
  145. {
  146. $ids = request('ids', []);
  147. if (empty($ids)) {
  148. return response()->json([
  149. 'status' => false,
  150. 'message' => '请选择要操作的记录'
  151. ]);
  152. }
  153. $successCount = 0;
  154. foreach ($ids as $id) {
  155. if ($this->repository()->toggleStatus($id)) {
  156. $successCount++;
  157. }
  158. }
  159. return response()->json([
  160. 'status' => true,
  161. 'message' => "成功切换 {$successCount} 条记录的状态"
  162. ]);
  163. }
  164. /**
  165. * 获取商品的限购配置
  166. *
  167. * @param int $shopItemId
  168. * @return \Illuminate\Http\JsonResponse
  169. */
  170. public function getByShopItem($shopItemId)
  171. {
  172. $limits = $this->repository()->getByShopItem($shopItemId);
  173. return response()->json([
  174. 'status' => true,
  175. 'data' => $limits->map(function ($limit) {
  176. return [
  177. 'id' => $limit->id,
  178. 'name' => $limit->name,
  179. 'limit_type_text' => $limit->limit_type_text,
  180. 'limit_period_text' => $limit->limit_period_text,
  181. 'max_quantity' => $limit->max_quantity,
  182. 'is_active' => $limit->is_active,
  183. ];
  184. })
  185. ]);
  186. }
  187. /**
  188. * 复制限购配置
  189. *
  190. * @param int $id
  191. * @return \Illuminate\Http\JsonResponse
  192. */
  193. public function copy($id)
  194. {
  195. $limit = $this->repository()->newQuery()->find($id);
  196. if (!$limit) {
  197. return response()->json([
  198. 'status' => false,
  199. 'message' => '限购配置不存在'
  200. ]);
  201. }
  202. $newData = $limit->toArray();
  203. unset($newData['id'], $newData['created_at'], $newData['updated_at']);
  204. $newData['name'] = $newData['name'] . ' (副本)';
  205. $newData['is_active'] = false; // 副本默认为禁用状态
  206. $repository = $this->repository();
  207. $newLimit = $repository->createLimit($newData);
  208. return response()->json([
  209. 'status' => true,
  210. 'message' => '限购配置复制成功',
  211. 'data' => [
  212. 'id' => $newLimit->id,
  213. 'name' => $newLimit->name
  214. ]
  215. ]);
  216. }
  217. }