ShopPurchaseLimitSeeder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Module\Shop\Models\ShopItem;
  4. use App\Module\Shop\Models\ShopPurchaseLimit;
  5. use App\Module\Shop\Enums\PURCHASE_LIMIT_TYPE;
  6. use App\Module\Shop\Enums\PURCHASE_LIMIT_PERIOD;
  7. use Illuminate\Database\Seeder;
  8. class ShopPurchaseLimitSeeder extends Seeder
  9. {
  10. /**
  11. * Run the database seeds.
  12. *
  13. * @return void
  14. */
  15. public function run()
  16. {
  17. $this->command->info('开始创建商店限购配置示例数据...');
  18. // 获取一些商品用于演示
  19. $shopItems = ShopItem::where('is_active', true)->limit(3)->get();
  20. if ($shopItems->isEmpty()) {
  21. $this->command->warn('没有找到激活的商品,请先创建商品数据');
  22. return;
  23. }
  24. $limitConfigs = [];
  25. foreach ($shopItems as $index => $item) {
  26. switch ($index) {
  27. case 0:
  28. // 第一个商品:单次购买限制
  29. $limitConfigs[] = [
  30. 'shop_item_id' => $item->id,
  31. 'limit_type' => PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE,
  32. 'limit_period' => PURCHASE_LIMIT_PERIOD::PERMANENT,
  33. 'max_quantity' => 5,
  34. 'name' => '单次购买限制',
  35. 'description' => '每次购买最多5个',
  36. 'is_active' => true,
  37. 'sort_order' => 1,
  38. ];
  39. break;
  40. case 1:
  41. // 第二个商品:每日限购
  42. $limitConfigs[] = [
  43. 'shop_item_id' => $item->id,
  44. 'limit_type' => PURCHASE_LIMIT_TYPE::PERIODIC_PURCHASE,
  45. 'limit_period' => PURCHASE_LIMIT_PERIOD::DAILY,
  46. 'max_quantity' => 10,
  47. 'name' => '每日限购',
  48. 'description' => '每天最多购买10个',
  49. 'is_active' => true,
  50. 'sort_order' => 1,
  51. ];
  52. break;
  53. case 2:
  54. // 第三个商品:每周限购 + 单次限购
  55. $limitConfigs[] = [
  56. 'shop_item_id' => $item->id,
  57. 'limit_type' => PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE,
  58. 'limit_period' => PURCHASE_LIMIT_PERIOD::PERMANENT,
  59. 'max_quantity' => 3,
  60. 'name' => '单次购买限制',
  61. 'description' => '每次购买最多3个',
  62. 'is_active' => true,
  63. 'sort_order' => 1,
  64. ];
  65. $limitConfigs[] = [
  66. 'shop_item_id' => $item->id,
  67. 'limit_type' => PURCHASE_LIMIT_TYPE::PERIODIC_PURCHASE,
  68. 'limit_period' => PURCHASE_LIMIT_PERIOD::WEEKLY,
  69. 'max_quantity' => 15,
  70. 'name' => '每周限购',
  71. 'description' => '每周最多购买15个',
  72. 'is_active' => true,
  73. 'sort_order' => 2,
  74. ];
  75. break;
  76. }
  77. }
  78. // 创建限购配置
  79. foreach ($limitConfigs as $config) {
  80. $limit = ShopPurchaseLimit::firstOrCreate(
  81. [
  82. 'shop_item_id' => $config['shop_item_id'],
  83. 'limit_type' => $config['limit_type'],
  84. 'limit_period' => $config['limit_period'],
  85. ],
  86. $config
  87. );
  88. if ($limit->wasRecentlyCreated) {
  89. $this->command->info("创建限购配置: {$limit->name} (商品ID: {$limit->shop_item_id})");
  90. } else {
  91. $this->command->info("限购配置已存在: {$limit->name} (商品ID: {$limit->shop_item_id})");
  92. }
  93. }
  94. $this->command->info('商店限购配置示例数据创建完成!');
  95. }
  96. }