| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace Database\Seeders;
- use App\Module\Shop\Models\ShopItem;
- use App\Module\Shop\Models\ShopPurchaseLimit;
- use App\Module\Shop\Enums\PURCHASE_LIMIT_TYPE;
- use App\Module\Shop\Enums\PURCHASE_LIMIT_PERIOD;
- use Illuminate\Database\Seeder;
- class ShopPurchaseLimitSeeder extends Seeder
- {
- /**
- * Run the database seeds.
- *
- * @return void
- */
- public function run()
- {
- $this->command->info('开始创建商店限购配置示例数据...');
- // 获取一些商品用于演示
- $shopItems = ShopItem::where('is_active', true)->limit(3)->get();
- if ($shopItems->isEmpty()) {
- $this->command->warn('没有找到激活的商品,请先创建商品数据');
- return;
- }
- $limitConfigs = [];
- foreach ($shopItems as $index => $item) {
- switch ($index) {
- case 0:
- // 第一个商品:单次购买限制
- $limitConfigs[] = [
- 'shop_item_id' => $item->id,
- 'limit_type' => PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE,
- 'limit_period' => PURCHASE_LIMIT_PERIOD::PERMANENT,
- 'max_quantity' => 5,
- 'name' => '单次购买限制',
- 'description' => '每次购买最多5个',
- 'is_active' => true,
- 'sort_order' => 1,
- ];
- break;
- case 1:
- // 第二个商品:每日限购
- $limitConfigs[] = [
- 'shop_item_id' => $item->id,
- 'limit_type' => PURCHASE_LIMIT_TYPE::PERIODIC_PURCHASE,
- 'limit_period' => PURCHASE_LIMIT_PERIOD::DAILY,
- 'max_quantity' => 10,
- 'name' => '每日限购',
- 'description' => '每天最多购买10个',
- 'is_active' => true,
- 'sort_order' => 1,
- ];
- break;
- case 2:
- // 第三个商品:每周限购 + 单次限购
- $limitConfigs[] = [
- 'shop_item_id' => $item->id,
- 'limit_type' => PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE,
- 'limit_period' => PURCHASE_LIMIT_PERIOD::PERMANENT,
- 'max_quantity' => 3,
- 'name' => '单次购买限制',
- 'description' => '每次购买最多3个',
- 'is_active' => true,
- 'sort_order' => 1,
- ];
-
- $limitConfigs[] = [
- 'shop_item_id' => $item->id,
- 'limit_type' => PURCHASE_LIMIT_TYPE::PERIODIC_PURCHASE,
- 'limit_period' => PURCHASE_LIMIT_PERIOD::WEEKLY,
- 'max_quantity' => 15,
- 'name' => '每周限购',
- 'description' => '每周最多购买15个',
- 'is_active' => true,
- 'sort_order' => 2,
- ];
- break;
- }
- }
- // 创建限购配置
- foreach ($limitConfigs as $config) {
- $limit = ShopPurchaseLimit::firstOrCreate(
- [
- 'shop_item_id' => $config['shop_item_id'],
- 'limit_type' => $config['limit_type'],
- 'limit_period' => $config['limit_period'],
- ],
- $config
- );
- if ($limit->wasRecentlyCreated) {
- $this->command->info("创建限购配置: {$limit->name} (商品ID: {$limit->shop_item_id})");
- } else {
- $this->command->info("限购配置已存在: {$limit->name} (商品ID: {$limit->shop_item_id})");
- }
- }
- $this->command->info('商店限购配置示例数据创建完成!');
- }
- }
|