| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace App\Module\Shop\AdminControllers;
- use App\Module\Shop\Models\ShopPurchaseLimit;
- use App\Module\Shop\Models\ShopItem;
- use App\Module\Shop\Enums\PURCHASE_LIMIT_TYPE;
- use App\Module\Shop\Enums\PURCHASE_LIMIT_PERIOD;
- use App\Module\Shop\Repositorys\ShopPurchaseLimitRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 商店限购配置控制器
- *
- * 路由前缀: /admin/shop/purchase-limits
- * 路由名称: admin.shop.purchase-limits
- *
- * 提供商店限购配置的管理功能,包括:
- * - 限购规则的增删改查
- * - 限购类型和周期的配置
- * - 限购状态的管理
- */
- #[Resource('shop/purchase-limits', names: 'dcat.admin.shop.purchase-limits')]
- class ShopPurchaseLimitController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '商店限购配置';
- /**
- * 创建表格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ShopPurchaseLimitRepository(), function (Grid $grid) {
- // 预加载关联数据
- $grid->model()->with(['shopItem']);
- $grid->column('id', 'ID')->sortable();
- $grid->column('shopItem.name', '商品名称')->limit(30);
- $grid->column('name', '限购规则名称')->limit(30);
- $grid->column('limit_type', '限购类型')->using(PURCHASE_LIMIT_TYPE::getAll());
- $grid->column('limit_period', '限购周期')->using(PURCHASE_LIMIT_PERIOD::getAll());
- $grid->column('max_quantity', '最大数量')->sortable();
- $grid->column('is_active', '状态')->switch();
- $grid->column('sort_order', '排序')->sortable()->editable();
- $grid->column('created_at', '创建时间')->sortable();
- // 过滤器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('shop_item_id', '商品')->select(
- ShopItem::where('is_active', true)->pluck('name', 'id')
- );
- $filter->equal('limit_type', '限购类型')->select(PURCHASE_LIMIT_TYPE::getAll());
- $filter->equal('limit_period', '限购周期')->select(PURCHASE_LIMIT_PERIOD::getAll());
- $filter->equal('is_active', '状态')->select([1 => '激活', 0 => '禁用']);
- });
- // 工具栏
- $grid->toolsWithOutline(false);
- $grid->enableDialogCreate();
- $grid->enableDialogEdit();
- $grid->setDialogFormDimensions('800px', '600px');
- });
- }
- /**
- * 创建详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ShopPurchaseLimitRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('shopItem.name', '商品名称');
- $show->field('name', '限购规则名称');
- $show->field('description', '限购规则描述');
- $show->field('limit_type', '限购类型')->using(PURCHASE_LIMIT_TYPE::getAll());
- $show->field('limit_period', '限购周期')->using(PURCHASE_LIMIT_PERIOD::getAll());
- $show->field('max_quantity', '最大数量');
- $show->field('is_active', '状态')->using([1 => '激活', 0 => '禁用']);
- $show->field('sort_order', '排序权重');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 创建表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ShopPurchaseLimitRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->select('shop_item_id', '商品')
- ->options(ShopItem::where('is_active', true)->pluck('name', 'id'))
- ->required()
- ->help('选择要设置限购的商品');
- $form->text('name', '限购规则名称')
- ->required()
- ->help('为此限购规则起一个便于识别的名称');
- $form->textarea('description', '限购规则描述')
- ->rows(3)
- ->help('详细描述此限购规则的用途和限制条件');
- $form->select('limit_type', '限购类型')
- ->options(PURCHASE_LIMIT_TYPE::getAll())
- ->required()
- ->help('选择限购类型:单次购买限制或周期性购买限制');
- $form->select('limit_period', '限购周期')
- ->options(PURCHASE_LIMIT_PERIOD::getAll())
- ->default(PURCHASE_LIMIT_PERIOD::PERMANENT->value)
- ->help('选择限购周期,仅在周期性购买限制时有效');
- $form->number('max_quantity', '最大购买数量')
- ->min(1)
- ->required()
- ->help('设置在限购周期内允许购买的最大数量');
- $form->switch('is_active', '是否激活')
- ->default(true)
- ->help('是否启用此限购规则');
- $form->number('sort_order', '排序权重')
- ->default(0)
- ->help('数值越小排序越靠前,用于控制多个限购规则的检查顺序');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 表单验证
- $form->saving(function (Form $form) {
- // 验证限购类型和周期的组合
- if ($form->limit_type == PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE->value
- && $form->limit_period != PURCHASE_LIMIT_PERIOD::PERMANENT->value) {
- return $form->response()->error('单次购买限制只能使用永久周期');
- }
- });
- });
- }
- /**
- * 批量切换状态
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function toggleStatus()
- {
- $ids = request('ids', []);
-
- if (empty($ids)) {
- return response()->json([
- 'status' => false,
- 'message' => '请选择要操作的记录'
- ]);
- }
- $successCount = 0;
- foreach ($ids as $id) {
- if ($this->repository()->toggleStatus($id)) {
- $successCount++;
- }
- }
- return response()->json([
- 'status' => true,
- 'message' => "成功切换 {$successCount} 条记录的状态"
- ]);
- }
- /**
- * 获取商品的限购配置
- *
- * @param int $shopItemId
- * @return \Illuminate\Http\JsonResponse
- */
- public function getByShopItem($shopItemId)
- {
- $limits = $this->repository()->getByShopItem($shopItemId);
-
- return response()->json([
- 'status' => true,
- 'data' => $limits->map(function ($limit) {
- return [
- 'id' => $limit->id,
- 'name' => $limit->name,
- 'limit_type_text' => $limit->limit_type_text,
- 'limit_period_text' => $limit->limit_period_text,
- 'max_quantity' => $limit->max_quantity,
- 'is_active' => $limit->is_active,
- ];
- })
- ]);
- }
- /**
- * 复制限购配置
- *
- * @param int $id
- * @return \Illuminate\Http\JsonResponse
- */
- public function copy($id)
- {
- $limit = $this->repository()->newQuery()->find($id);
-
- if (!$limit) {
- return response()->json([
- 'status' => false,
- 'message' => '限购配置不存在'
- ]);
- }
- $newData = $limit->toArray();
- unset($newData['id'], $newData['created_at'], $newData['updated_at']);
- $newData['name'] = $newData['name'] . ' (副本)';
- $newData['is_active'] = false; // 副本默认为禁用状态
- $repository = $this->repository();
- $newLimit = $repository->createLimit($newData);
- return response()->json([
- 'status' => true,
- 'message' => '限购配置复制成功',
- 'data' => [
- 'id' => $newLimit->id,
- 'name' => $newLimit->name
- ]
- ]);
- }
- }
|