98 lines
4.1 KiB
PHP
98 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use RuntimeException;
|
|
|
|
class LegacyProductCatalogSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
$catalog = [
|
|
'vps' => [
|
|
'category' => ['name' => 'VPS', 'slug' => 'vps', 'type' => 'vps', 'sort_order' => 10],
|
|
'product_type' => 'vps',
|
|
'ids' => [1, 2, 3, 4, 22, 23, 24, 25, 26, 28, 34, 35, 36, 37, 38, 71, 82, 84, 85, 86, 87, 88, 89, 90],
|
|
],
|
|
'hosting' => [
|
|
'category' => ['name' => 'Hosting', 'slug' => 'hosting', 'type' => 'hosting', 'sort_order' => 20],
|
|
'product_type' => 'hosting',
|
|
'ids' => [5, 6, 7, 17, 18, 19, 20, 46, 47, 72, 73, 74, 75, 77, 83],
|
|
],
|
|
'cloud-storage' => [
|
|
'category' => ['name' => 'Cloud Storage', 'slug' => 'cloud-storage', 'type' => 'cloud', 'sort_order' => 30],
|
|
'product_type' => 'cloud',
|
|
'ids' => [39, 40, 41, 42, 43, 57, 58, 59, 78],
|
|
],
|
|
'email' => [
|
|
'category' => ['name' => 'Email', 'slug' => 'email', 'type' => 'email', 'sort_order' => 40],
|
|
'product_type' => 'email',
|
|
'ids' => [16, 52, 53, 54, 55, 56, 67, 68, 69, 70, 94, 95, 96, 97],
|
|
],
|
|
'hardware' => [
|
|
'category' => ['name' => 'Hardware', 'slug' => 'hardware', 'type' => 'hardware', 'sort_order' => 50],
|
|
'product_type' => 'hardware',
|
|
'ids' => [62, 63, 64, 76, 79, 81],
|
|
],
|
|
'web-design' => [
|
|
'category' => ['name' => 'Thiết kế website', 'slug' => 'thiet-ke-website', 'type' => 'web_design', 'sort_order' => 60],
|
|
'product_type' => 'web_design',
|
|
'ids' => [14],
|
|
],
|
|
'it-support' => [
|
|
'category' => ['name' => 'IT Support', 'slug' => 'it-support', 'type' => 'it_support', 'sort_order' => 70],
|
|
'product_type' => 'it_support',
|
|
'ids' => [66],
|
|
],
|
|
'other' => [
|
|
'category' => ['name' => 'Other', 'slug' => 'other', 'type' => null, 'sort_order' => 80],
|
|
'product_type' => 'other',
|
|
'ids' => [8, 9, 10, 11, 12, 13, 15, 21, 27, 29, 30, 31, 32, 33, 44, 45, 48, 49, 50, 51, 60, 61, 65, 80, 91, 92, 93, 98, 99, 100, 101, 102],
|
|
],
|
|
];
|
|
|
|
$allIds = collect($catalog)->pluck('ids')->flatten()->map(fn ($id) => (int) $id);
|
|
|
|
if ($allIds->count() !== 102 || $allIds->unique()->count() !== 102) {
|
|
throw new RuntimeException('Product catalog mapping must contain exactly 102 unique product IDs.');
|
|
}
|
|
|
|
$missing = $allIds->diff(DB::table('products')->whereIn('id', $allIds)->pluck('id'));
|
|
if ($missing->isNotEmpty()) {
|
|
throw new RuntimeException('Missing products: ' . $missing->implode(', '));
|
|
}
|
|
|
|
DB::transaction(function () use ($catalog): void {
|
|
foreach ($catalog as $definition) {
|
|
$category = $definition['category'];
|
|
DB::table('product_categories')->updateOrInsert(
|
|
['slug' => $category['slug']],
|
|
$category + [
|
|
'parent_id' => null,
|
|
'is_active' => true,
|
|
'deleted_at' => null,
|
|
'updated_at' => now(),
|
|
'created_at' => now(),
|
|
],
|
|
);
|
|
|
|
$categoryId = DB::table('product_categories')
|
|
->where('slug', $category['slug'])
|
|
->value('id');
|
|
|
|
DB::table('products')
|
|
->whereIn('id', $definition['ids'])
|
|
->update([
|
|
'product_categories_id' => $categoryId,
|
|
'product_type' => $definition['product_type'],
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
});
|
|
|
|
$this->command?->info('Categorized all 102 legacy products.');
|
|
}
|
|
}
|