ShopPurchaseLimitRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Module\Shop\Repositorys;
  3. use App\Module\Shop\Models\ShopPurchaseLimit;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. /**
  6. * 商店限购配置数据仓库
  7. *
  8. * 提供商店限购配置的数据访问和管理功能
  9. */
  10. class ShopPurchaseLimitRepository extends EloquentRepository
  11. {
  12. /**
  13. * 模型类名
  14. *
  15. * @var string
  16. */
  17. protected $eloquentClass = ShopPurchaseLimit::class;
  18. /**
  19. * 获取商品的限购配置列表
  20. *
  21. * @param int $shopItemId 商品ID
  22. * @return \Illuminate\Database\Eloquent\Collection
  23. */
  24. public function getByShopItem(int $shopItemId)
  25. {
  26. return ShopPurchaseLimit::where('shop_item_id', $shopItemId)
  27. ->where('is_active', true)
  28. ->orderBy('sort_order')
  29. ->get();
  30. }
  31. /**
  32. * 批量获取多个商品的限购配置
  33. *
  34. * @param array $shopItemIds 商品ID数组
  35. * @return \Illuminate\Database\Eloquent\Collection
  36. */
  37. public function getByShopItems(array $shopItemIds)
  38. {
  39. return ShopPurchaseLimit::whereIn('shop_item_id', $shopItemIds)
  40. ->where('is_active', true)
  41. ->orderBy('sort_order')
  42. ->get()
  43. ->groupBy('shop_item_id');
  44. }
  45. /**
  46. * 创建限购配置
  47. *
  48. * @param array $data 限购配置数据
  49. * @return ShopPurchaseLimit
  50. */
  51. public function createLimit(array $data): ShopPurchaseLimit
  52. {
  53. return ShopPurchaseLimit::create($data);
  54. }
  55. /**
  56. * 更新限购配置
  57. *
  58. * @param int $id 限购配置ID
  59. * @param array $data 更新数据
  60. * @return bool
  61. */
  62. public function updateLimit(int $id, array $data): bool
  63. {
  64. return ShopPurchaseLimit::where('id', $id)->update($data) > 0;
  65. }
  66. /**
  67. * 删除限购配置
  68. *
  69. * @param int $id 限购配置ID
  70. * @return bool
  71. */
  72. public function deleteLimit(int $id): bool
  73. {
  74. return ShopPurchaseLimit::where('id', $id)->delete() > 0;
  75. }
  76. /**
  77. * 切换限购配置状态
  78. *
  79. * @param int $id 限购配置ID
  80. * @return bool
  81. */
  82. public function toggleStatus(int $id): bool
  83. {
  84. $limit = ShopPurchaseLimit::find($id);
  85. if (!$limit) {
  86. return false;
  87. }
  88. $limit->is_active = !$limit->is_active;
  89. return $limit->save();
  90. }
  91. }