186 lines
7.8 KiB
PHP
186 lines
7.8 KiB
PHP
<?php
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Peidikeji\Goods\GoodsService;
|
|
use Peidikeji\Goods\Models\Goods;
|
|
use Peidikeji\Goods\Models\GoodsBrand;
|
|
use Peidikeji\Goods\Models\GoodsCategory;
|
|
use Peidikeji\Goods\Models\GoodsSku;
|
|
use Peidikeji\Goods\Models\GoodsType;
|
|
|
|
class GoodsTableSeeder extends Seeder
|
|
{
|
|
public function run()
|
|
{
|
|
DB::statement('SET FOREIGN_KEY_CHECKS = 0;');
|
|
GoodsCategory::truncate();
|
|
$categoryList = [
|
|
['name' => '1', 'children' => [
|
|
['name' => '1-1', 'children' => [
|
|
['name' => '1-1-1']
|
|
]]
|
|
]],
|
|
['name' => '2', 'children' => [
|
|
['name' => '2-2', 'children' => [
|
|
['name' => '2-2-2']
|
|
]]
|
|
]],
|
|
];
|
|
|
|
$this->createCategory($categoryList);
|
|
|
|
GoodsType::truncate();
|
|
$types = [
|
|
[
|
|
'name' => '手机',
|
|
'attr' => [
|
|
['name' => '主体', 'values' => [
|
|
['name' => '入网型号', 'values' => ['5G', '4G']],
|
|
['name' => '上市年份', 'values' => null],
|
|
['name' => '品牌', 'values' => null]
|
|
]]
|
|
],
|
|
'spec' => [
|
|
['name' => '颜色', 'values' => ['白色', '红色', '黑色']],
|
|
['name' => '内存', 'values' => ['32G', '64G', '128G']],
|
|
],
|
|
'part' => [
|
|
['name' => '套餐', 'values' => ['套餐1', '套餐2', '套餐3']]
|
|
]
|
|
],
|
|
[
|
|
'name' => '笔记本电脑',
|
|
'attr' => [
|
|
['group' => '显示器', 'name' => '屏幕类型', 'values' => ['LED 背光显示屏']],
|
|
['group' => '显示器','name' => '物理分辨率', 'values' => ['3072 x 1920 (226 ppi)']],
|
|
],
|
|
'spec' => [
|
|
['name' => '颜色', 'values' => ['白色', '灰色']],
|
|
['name' => '内存', 'values' => ['16G', '32G', '64G']],
|
|
],
|
|
'part' => [
|
|
['name' => '套餐', 'values' => ['优惠套装1', '优惠套装2', '优惠套装3']]
|
|
]
|
|
],
|
|
];
|
|
foreach($types as $item) {
|
|
GoodsType::create($item);
|
|
}
|
|
|
|
GoodsBrand::truncate();
|
|
GoodsBrand::insert([
|
|
['name' => '三星', 'image' => 'https://img20.360buyimg.com/popshop/jfs/t1/1534/38/9873/3556/5bc93df2E73c40121/74dc92d16e483509.jpg'],
|
|
['name' => 'Apple', 'image' => 'https://img20.360buyimg.com/popshop/jfs/t2989/240/151377693/3895/30ad9044/574d36dbN262ef26d.jpg'],
|
|
]);
|
|
|
|
Goods::truncate();
|
|
GoodsSku::truncate();
|
|
$goodsList = [
|
|
[
|
|
'category_id' => 3,
|
|
'type_id' => 1,
|
|
'brand_id' => 1,
|
|
'goods_sn' => '1016',
|
|
'name' => '三星Galaxy Noet10+ 5G(SM-N9760)',
|
|
'cover_image' => 'https://img14.360buyimg.com/n5/s54x54_jfs/t1/85701/3/3164/116271/5ddcffaeEd7924f35/013d69c48b507982.jpg',
|
|
'content' => ['https://img30.360buyimg.com/sku/jfs/t1/91355/34/4028/288919/5de4c653Ed267b5d0/b67ac088ded04947.jpg'],
|
|
'images' => [
|
|
'https://img14.360buyimg.com/n0/jfs/t1/138249/34/51/266266/5edaed2fE2d4d4050/297b76afaff928bb.jpg',
|
|
'https://img14.360buyimg.com/n0/jfs/t1/85701/3/3164/116271/5ddcffaeEd7924f35/013d69c48b507982.jpg',
|
|
'https://img14.360buyimg.com/n0/jfs/t1/43997/21/12754/274595/5d5f87f1Ec419d2f9/358032d0a7a2ccd7.jpg'
|
|
],
|
|
'stock' => 100,
|
|
'price' => 6499.00,
|
|
'attr' => [
|
|
['name' => '主体', 'values' => [
|
|
['name' => '入网型号', 'value' => '5G'],
|
|
['name' => '品牌', 'value' => '三星Galaxy'],
|
|
['name' => '上市年份', 'value' => '2020'],
|
|
]],
|
|
],
|
|
'spec' => [
|
|
['name' => '颜色', 'values' => [
|
|
['value' => '白色', 'price' => 0],
|
|
['value' => '红色', 'price' => 800],
|
|
['value' => '黑色', 'price' => 0],
|
|
]],
|
|
['name' => '内存', 'values' => [
|
|
['value' => '32G', 'price' => 0],
|
|
['value' => '64G', 'price' => 1000],
|
|
['value' => '128G', 'price' => 2000],
|
|
]]
|
|
],
|
|
'part' => [
|
|
['name' => '套餐', 'values' => [
|
|
['value' => '套餐1', 'price' => 850],
|
|
['value' => '套餐2', 'price' => 1200],
|
|
['value' => '套餐3', 'price' => 1800],
|
|
]]
|
|
],
|
|
],
|
|
[
|
|
'category_id' => 6,
|
|
'type_id' => 2,
|
|
'brand_id' => 2,
|
|
'goods_sn' => '1017',
|
|
'name' => 'MacBook Pro 16英寸',
|
|
'cover_image' => 'https://img14.360buyimg.com/n0/jfs/t1/64979/31/15492/115459/5dd3d4f2E75b0a9a6/95c273eda00e67c0.jpg',
|
|
'description' => '',
|
|
'content' => ['https://img11.360buyimg.com/cms/jfs/t1/77779/20/15834/638477/5dd3d469Eca9fa4a7/26ff2bd661580a86.jpg'],
|
|
'images' => [
|
|
'https://img14.360buyimg.com/n0/jfs/t1/64979/31/15492/115459/5dd3d4f2E75b0a9a6/95c273eda00e67c0.jpg',
|
|
'https://img14.360buyimg.com/n0/jfs/t1/50902/13/16242/169086/5dd3d4f2E19e1994f/ff8ecd5a61c1bebb.jpg',
|
|
'https://img14.360buyimg.com/n0/jfs/t1/104429/27/2676/303491/5dd3d4f3E6fd2b80a/b7213eaf5be44b49.jpg'
|
|
],
|
|
'stock' => 150,
|
|
'price' => 17999.00,
|
|
'attr' => [
|
|
['name' => '显示器', 'values' => [
|
|
['name' => '屏幕类型', 'value' => 'LED 背光显示屏'],
|
|
['name' => '物理分辨率', 'value' => '3072 x 1920 (226 ppi)']
|
|
]],
|
|
],
|
|
'spec' => [
|
|
['name' => '颜色', 'values' => [
|
|
['value' => '白色', 'price' => 0],
|
|
['value' => '灰色', 'price' => 0]
|
|
]],
|
|
['name' => '内存', 'values' => [
|
|
['value' => '16G', 'price' => 0],
|
|
['value' => '32G', 'price' => 3000],
|
|
['value' => '64G', 'price' => 6000],
|
|
]]
|
|
],
|
|
'part' => [
|
|
['name' => '套餐', 'values' => [
|
|
['value' => '优惠套装1', 'price' => 850],
|
|
['value' => '优惠套装2', 'price' => 650],
|
|
['value' => '优惠套装3', 'price' => 1000],
|
|
]]
|
|
],
|
|
]
|
|
];
|
|
|
|
$service = GoodsService::make();
|
|
foreach($goodsList as $item) {
|
|
$goods = Goods::create($item);
|
|
$service->generateSku($goods);
|
|
}
|
|
}
|
|
|
|
protected function createCategory($list, $pid = 0)
|
|
{
|
|
foreach($list as $key => $item) {
|
|
$attributes = Arr::except($item, ['children']);
|
|
$attributes['parent_id'] = $pid;
|
|
$attributes['sort'] = $key + 1;
|
|
$category = GoodsCategory::create($attributes);
|
|
if ($children = data_get($item, 'children')) {
|
|
$this->createCategory($children, $category->id);
|
|
}
|
|
}
|
|
}
|
|
}
|