| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace App\Module\Shop\Controllers;
- use App\Module\Shop\Repositorys\ShopPurchaseLimitRepository;
- use Dcat\Admin\Http\Controllers\AdminController;
- /**
- * 商店限购配置控制器
- *
- * 路由前缀: /admin/shop/purchase-limits
- * 路由名称: admin.shop.purchase-limits
- *
- * 提供商店限购配置的管理功能,包括:
- * - 限购规则的增删改查
- * - 限购类型和周期的配置
- * - 限购状态的管理
- */
- class ShopPurchaseLimitController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '商店限购配置';
- /**
- * 获取数据仓库实例
- *
- * @return ShopPurchaseLimitRepository
- */
- protected function repository(): ShopPurchaseLimitRepository
- {
- return new ShopPurchaseLimitRepository();
- }
- /**
- * 批量切换状态
- *
- * @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
- ]
- ]);
- }
- }
|