ShopPurchaseLimitController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Module\Shop\Controllers;
  3. use App\Module\Shop\Repositorys\ShopPurchaseLimitRepository;
  4. use Dcat\Admin\Http\Controllers\AdminController;
  5. /**
  6. * 商店限购配置控制器
  7. *
  8. * 路由前缀: /admin/shop/purchase-limits
  9. * 路由名称: admin.shop.purchase-limits
  10. *
  11. * 提供商店限购配置的管理功能,包括:
  12. * - 限购规则的增删改查
  13. * - 限购类型和周期的配置
  14. * - 限购状态的管理
  15. */
  16. class ShopPurchaseLimitController extends AdminController
  17. {
  18. /**
  19. * 页面标题
  20. *
  21. * @var string
  22. */
  23. protected $title = '商店限购配置';
  24. /**
  25. * 获取数据仓库实例
  26. *
  27. * @return ShopPurchaseLimitRepository
  28. */
  29. protected function repository(): ShopPurchaseLimitRepository
  30. {
  31. return new ShopPurchaseLimitRepository();
  32. }
  33. /**
  34. * 批量切换状态
  35. *
  36. * @return \Illuminate\Http\JsonResponse
  37. */
  38. public function toggleStatus()
  39. {
  40. $ids = request('ids', []);
  41. if (empty($ids)) {
  42. return response()->json([
  43. 'status' => false,
  44. 'message' => '请选择要操作的记录'
  45. ]);
  46. }
  47. $successCount = 0;
  48. foreach ($ids as $id) {
  49. if ($this->repository()->toggleStatus($id)) {
  50. $successCount++;
  51. }
  52. }
  53. return response()->json([
  54. 'status' => true,
  55. 'message' => "成功切换 {$successCount} 条记录的状态"
  56. ]);
  57. }
  58. /**
  59. * 获取商品的限购配置
  60. *
  61. * @param int $shopItemId
  62. * @return \Illuminate\Http\JsonResponse
  63. */
  64. public function getByShopItem($shopItemId)
  65. {
  66. $limits = $this->repository()->getByShopItem($shopItemId);
  67. return response()->json([
  68. 'status' => true,
  69. 'data' => $limits->map(function ($limit) {
  70. return [
  71. 'id' => $limit->id,
  72. 'name' => $limit->name,
  73. 'limit_type_text' => $limit->limit_type_text,
  74. 'limit_period_text' => $limit->limit_period_text,
  75. 'max_quantity' => $limit->max_quantity,
  76. 'is_active' => $limit->is_active,
  77. ];
  78. })
  79. ]);
  80. }
  81. /**
  82. * 复制限购配置
  83. *
  84. * @param int $id
  85. * @return \Illuminate\Http\JsonResponse
  86. */
  87. public function copy($id)
  88. {
  89. $limit = $this->repository()->newQuery()->find($id);
  90. if (!$limit) {
  91. return response()->json([
  92. 'status' => false,
  93. 'message' => '限购配置不存在'
  94. ]);
  95. }
  96. $newData = $limit->toArray();
  97. unset($newData['id'], $newData['created_at'], $newData['updated_at']);
  98. $newData['name'] = $newData['name'] . ' (副本)';
  99. $newData['is_active'] = false; // 副本默认为禁用状态
  100. $repository = $this->repository();
  101. $newLimit = $repository->createLimit($newData);
  102. return response()->json([
  103. 'status' => true,
  104. 'message' => '限购配置复制成功',
  105. 'data' => [
  106. 'id' => $newLimit->id,
  107. 'name' => $newLimit->name
  108. ]
  109. ]);
  110. }
  111. }