add attribute seeder

创建属性组

产品属性和分类demo

多规格商品

子商品demo数据
This commit is contained in:
Edward Yang 2023-01-12 19:26:44 +08:00
parent d3624990e8
commit 1e2583b421
10 changed files with 1422 additions and 429 deletions

View File

@ -80,6 +80,7 @@ class FileManagerService
$currentImages = $imageCollection->forPage($page, $perPage);
$currentImages = $currentImages->map(function ($item) {
$item['url'] = image_resize("{$item['path']}");
return $item;
});
@ -210,6 +211,7 @@ class FileManagerService
private function handleImage($filePath, $baseName): array
{
$path = "catalog{$filePath}";
return [
'path' => $path,
'name' => $baseName,

View File

@ -32,9 +32,9 @@ class DatabaseController extends Controller
public function index()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
$rows = DB::select('SHOW TABLES');
$rows = DB::select('SHOW TABLES');
$database = config('database.connections.mysql.database');
$tables = array_column($rows, 'Tables_in_' . $database);
$tables = array_column($rows, 'Tables_in_' . $database);
foreach ($tables as $table) {
Schema::drop($table);
}

View File

@ -80,7 +80,6 @@ class AddressRepo
return $customer->addresses()->with('country')->get();
}
return collect();
return collect();
}
}

View File

@ -74,11 +74,12 @@ class CategoryRepo
*/
public static function getPerPages(): array
{
$perPages = [];
$perPages = [];
$configPerPage = system_setting('base.product_per_page', 20);
for ($index = 1; $index <= 5; $index++) {
$perPages[] = $configPerPage * $index;
}
return $perPages;
}
@ -165,9 +166,9 @@ class CategoryRepo
$pathName .= $path->pathCategory->description->name;
}
$results[] = [
'id' => $category->id,
'id' => $category->id,
'status' => $category->active,
'name' => $pathName,
'name' => $pathName,
];
}
@ -182,7 +183,7 @@ class CategoryRepo
{
if (is_int($category)) {
$category = Category::query()->findOrFail($category);
} elseif (!($category instanceof Category)) {
} elseif (! ($category instanceof Category)) {
throw new \Exception('invalid category');
}
$category->descriptions()->delete();
@ -198,7 +199,7 @@ class CategoryRepo
*/
public static function getName($category)
{
$id = is_int($category) ? $category : $category->id;
$id = is_int($category) ? $category : $category->id;
$categories = self::getAllCategoriesWithName();
return $categories[$id]['name'] ?? '';
@ -214,11 +215,11 @@ class CategoryRepo
return self::$allCategoryWithName;
}
$items = [];
$items = [];
$categories = self::getBuilder()->select('id')->get();
foreach ($categories as $category) {
$items[$category->id] = [
'id' => $category->id,
'id' => $category->id,
'name' => $category->description->name ?? '',
];
}

View File

@ -222,8 +222,9 @@ class ProductRepo
}
}
$results = array_map(function($item) {
$results = array_map(function ($item) {
$item['values'] = array_values($item['values']);
return $item;
}, $results);
@ -239,15 +240,15 @@ class ProductRepo
$min = $builder->min('ps.price');
$max = $builder->max('ps.price');
$priceArr = explode('-', $selectPrice);
$priceArr = explode('-', $selectPrice);
$selectMin = $priceArr[0];
$selectMax = $priceArr[1];
return [
'min' => $min,
'max' => $max,
'select_min' => ($selectMin && $selectMin > $min) ? $selectMin : $min,
'select_max' => ($selectMax && $selectMax < $max) ? $selectMax: $max,
'min' => $min,
'max' => $max,
'select_min' => ($selectMin && $selectMin > $min) ? $selectMin : $min,
'select_max' => ($selectMax && $selectMax < $max) ? $selectMax : $max,
];
}

View File

@ -247,7 +247,7 @@ class StateMachineService
private function updateSales()
{
$this->order->loadMissing([
'orderProducts'
'orderProducts',
]);
$orderProducts = $this->order->orderProducts;
foreach ($orderProducts as $orderProduct) {

View File

@ -23,7 +23,7 @@ class CategoryController extends Controller
$filterData = array_merge($filterData, ['category_id' => $category->id, 'active' => 1]);
$data = [
'all_categories' => CategoryRepo::getTwoLevelCategories(),
'all_categories' => CategoryRepo::getTwoLevelCategories(),
'category' => $category,
'filter_data' => ['attr' => ProductRepo::getFilterAttribute($filterData), 'price' => ProductRepo::getFilterPrice($filterData)],
'products_format' => ProductSimple::collection($products)->jsonSerialize(),

View File

@ -22,7 +22,7 @@ class ProductDetail extends JsonResource
{
$attributes = [];
foreach ($this->attributes as $ProductAttribute) {
if (!isset($attributes[$ProductAttribute->attribute->attribute_group_id]['attribute_group_name'])) {
if (! isset($attributes[$ProductAttribute->attribute->attribute_group_id]['attribute_group_name'])) {
$attributes[$ProductAttribute->attribute->attribute_group_id]['attribute_group_name'] = $ProductAttribute->attribute->attributeGroup->description->name;
}
$attributes[$ProductAttribute->attribute->attribute_group_id]['attributes'][] = [

View File

@ -0,0 +1,340 @@
<?php
/**
* AttributesSeeder.php
* php artisan db:seed --class=AttributesSeeder
*
* @copyright 2023 beikeshop.com - All Rights Reserved
* @link https://beikeshop.com
* @author Edward Yang <yangjin@guangda.work>
* @created 2023-01-12 19:20:05
* @modified 2023-01-12 19:20:05
*/
namespace Database\Seeders;
use Beike\Models\Attribute;
use Beike\Models\AttributeDescription;
use Beike\Models\AttributeGroup;
use Beike\Models\AttributeGroupDescription;
use Beike\Models\AttributeValue;
use Beike\Models\AttributeValueDescription;
use Beike\Models\ProductAttribute;
use Illuminate\Database\Seeder;
class AttributesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
AttributeGroup::query()->truncate();
AttributeGroupDescription::query()->truncate();
Attribute::query()->truncate();
AttributeDescription::query()->truncate();
AttributeValue::query()->truncate();
AttributeValueDescription::query()->truncate();
ProductAttribute::query()->truncate();
// 属性组
$attributeGroupsNumber = 4;
for ($i = 1; $i <= $attributeGroupsNumber; $i++) {
AttributeGroup::query()->create([
'sort_order' => $i
]);
}
// 属性组描述
$items = $this->getGroupDescriptions();
AttributeGroupDescription::query()->insert(
collect($items)->map(function ($item) {
$item['created_at'] = now();
$item['updated_at'] = now();
return $item;
})->toArray()
);
// 属性
$items = $this->getAttributes();
Attribute::query()->insert(
collect($items)->map(function ($item) {
$item['created_at'] = now();
$item['updated_at'] = now();
return $item;
})->toArray()
);
// 属性描述
$items = $this->getAttributeDescriptions();
AttributeDescription::query()->insert(
collect($items)->map(function ($item) {
$item['created_at'] = now();
$item['updated_at'] = now();
return $item;
})->toArray()
);
// 属性值
$items = $this->getAttributeValues();
AttributeValue::query()->insert(
collect($items)->map(function ($item) {
$item['created_at'] = now();
$item['updated_at'] = now();
return $item;
})->toArray()
);
// 属性值描述
$items = $this->getAttributeValueDescriptions();
AttributeValueDescription::query()->insert(
collect($items)->map(function ($item) {
$item['created_at'] = now();
$item['updated_at'] = now();
return $item;
})->toArray()
);
// 产品属性关联
$items = $this->productAttributes();
ProductAttribute::query()->insert(
collect($items)->map(function ($item) {
$item['created_at'] = now();
$item['updated_at'] = now();
return $item;
})->toArray()
);
}
private function getGroupDescriptions(): array
{
return [
["attribute_group_id" => 1, "locale" => "zh_cn", "name" => "女装"],
["attribute_group_id" => 1, "locale" => "en", "name" => "Women's clothing"],
["attribute_group_id" => 2, "locale" => "zh_cn", "name" => "衣服"],
["attribute_group_id" => 2, "locale" => "en", "name" => "Clothing"],
["attribute_group_id" => 3, "locale" => "zh_cn", "name" => "运动户外"],
["attribute_group_id" => 3, "locale" => "en", "name" => "Outdoor sport"],
["attribute_group_id" => 4, "locale" => "zh_cn", "name" => "数码"],
["attribute_group_id" => 4, "locale" => "en", "name" => "Digital"]
];
}
private function getAttributes(): array
{
return [
["attribute_group_id" => 2, "sort_order" => 0],
["attribute_group_id" => 2, "sort_order" => 0],
["attribute_group_id" => 2, "sort_order" => 0],
["attribute_group_id" => 3, "sort_order" => 0],
["attribute_group_id" => 4, "sort_order" => 0],
["attribute_group_id" => 4, "sort_order" => 0]
];
}
private function getAttributeDescriptions(): array
{
return [
["attribute_id" => 1, "locale" => "zh_cn", "name" => "功能"],
["attribute_id" => 1, "locale" => "en", "name" => "Features"],
["attribute_id" => 2, "locale" => "zh_cn", "name" => "面料"],
["attribute_id" => 2, "locale" => "en", "name" => "Fabric"],
["attribute_id" => 3, "locale" => "zh_cn", "name" => "样式"],
["attribute_id" => 3, "locale" => "en", "name" => "Style"],
["attribute_id" => 4, "locale" => "zh_cn", "name" => "缓震"],
["attribute_id" => 4, "locale" => "en", "name" => "Cushioning"],
["attribute_id" => 5, "locale" => "zh_cn", "name" => "CUP"],
["attribute_id" => 5, "locale" => "en", "name" => "CUP"],
["attribute_id" => 6, "locale" => "zh_cn", "name" => "内存"],
["attribute_id" => 6, "locale" => "en", "name" => "Memory"]
];
}
private function getAttributeValues(): array
{
return [
["attribute_id" => 2],
["attribute_id" => 2],
["attribute_id" => 1],
["attribute_id" => 3],
["attribute_id" => 2],
["attribute_id" => 2],
["attribute_id" => 2],
["attribute_id" => 3],
["attribute_id" => 3],
["attribute_id" => 3],
["attribute_id" => 1],
["attribute_id" => 1],
["attribute_id" => 4],
["attribute_id" => 4],
["attribute_id" => 4],
["attribute_id" => 4],
["attribute_id" => 4],
["attribute_id" => 5],
["attribute_id" => 5],
["attribute_id" => 5],
["attribute_id" => 5],
["attribute_id" => 6],
["attribute_id" => 6]
];
}
private function getAttributeValueDescriptions(): array
{
return [
["attribute_value_id" => 1, "locale" => "zh_cn", "name" => ""],
["attribute_value_id" => 1, "locale" => "en", "name" => "Cotton"],
["attribute_value_id" => 2, "locale" => "zh_cn", "name" => ""],
["attribute_value_id" => 2, "locale" => "en", "name" => "Numb"],
["attribute_value_id" => 5, "locale" => "en", "name" => "Silk"],
["attribute_value_id" => 5, "locale" => "zh_cn", "name" => ""],
["attribute_value_id" => 6, "locale" => "en", "name" => "Hair"],
["attribute_value_id" => 6, "locale" => "zh_cn", "name" => ""],
["attribute_value_id" => 7, "locale" => "zh_cn", "name" => "化纤"],
["attribute_value_id" => 7, "locale" => "en", "name" => "Chemical fiber"],
["attribute_value_id" => 4, "locale" => "zh_cn", "name" => "圆领"],
["attribute_value_id" => 4, "locale" => "en", "name" => "Round neck"],
["attribute_value_id" => 8, "locale" => "en", "name" => "Collarless"],
["attribute_value_id" => 8, "locale" => "zh_cn", "name" => "无领"],
["attribute_value_id" => 9, "locale" => "en", "name" => "Short sleeve"],
["attribute_value_id" => 9, "locale" => "zh_cn", "name" => "短袖"],
["attribute_value_id" => 10, "locale" => "zh_cn", "name" => "T恤"],
["attribute_value_id" => 10, "locale" => "en", "name" => "T-shirt"],
["attribute_value_id" => 3, "locale" => "zh_cn", "name" => "防水"],
["attribute_value_id" => 3, "locale" => "en", "name" => "Water proof"],
["attribute_value_id" => 11, "locale" => "zh_cn", "name" => "保暖"],
["attribute_value_id" => 11, "locale" => "en", "name" => "keep warm"],
["attribute_value_id" => 12, "locale" => "zh_cn", "name" => "防晒"],
["attribute_value_id" => 12, "locale" => "en", "name" => "Sun protection"],
["attribute_value_id" => 13, "locale" => "zh_cn", "name" => "Zoom气垫"],
["attribute_value_id" => 13, "locale" => "en", "name" => "Zoom Air Cushion"],
["attribute_value_id" => 14, "locale" => "zh_cn", "name" => "Max气垫"],
["attribute_value_id" => 14, "locale" => "en", "name" => "Max Air Cushion"],
["attribute_value_id" => 15, "locale" => "zh_cn", "name" => "Boost缓震材料"],
["attribute_value_id" => 15, "locale" => "en", "name" => "Boost cushioning material"],
["attribute_value_id" => 16, "locale" => "zh_cn", "name" => "Lightstrike科技"],
["attribute_value_id" => 16, "locale" => "en", "name" => "Lightstrike Technology"],
["attribute_value_id" => 17, "locale" => "en", "name" => "Fuel Cell Technology"],
["attribute_value_id" => 17, "locale" => "zh_cn", "name" => "FuelCell科技"],
["attribute_value_id" => 18, "locale" => "zh_cn", "name" => "i3"],
["attribute_value_id" => 18, "locale" => "en", "name" => "i3"],
["attribute_value_id" => 19, "locale" => "zh_cn", "name" => "i5"],
["attribute_value_id" => 19, "locale" => "en", "name" => "i5"],
["attribute_value_id" => 20, "locale" => "zh_cn", "name" => "i7"],
["attribute_value_id" => 20, "locale" => "en", "name" => "i7"],
["attribute_value_id" => 21, "locale" => "zh_cn", "name" => "i9"],
["attribute_value_id" => 21, "locale" => "en", "name" => "i9"],
["attribute_value_id" => 22, "locale" => "zh_cn", "name" => "DDR3"],
["attribute_value_id" => 22, "locale" => "en", "name" => "DDR3"],
["attribute_value_id" => 23, "locale" => "zh_cn", "name" => "DDR4"],
["attribute_value_id" => 23, "locale" => "en", "name" => "DDR4"],
];
}
private function productAttributes(): array
{
return [
["product_id" => 5, "attribute_id" => 1, "attribute_value_id" => 11,],
["product_id" => 5, "attribute_id" => 2, "attribute_value_id" => 5,],
["product_id" => 5, "attribute_id" => 5, "attribute_value_id" => 20,],
["product_id" => 5, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 5, "attribute_id" => 3, "attribute_value_id" => 10,],
["product_id" => 6, "attribute_id" => 3, "attribute_value_id" => 8,],
["product_id" => 6, "attribute_id" => 2, "attribute_value_id" => 1,],
["product_id" => 6, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 6, "attribute_id" => 4, "attribute_value_id" => 14,],
["product_id" => 7, "attribute_id" => 1, "attribute_value_id" => 11,],
["product_id" => 7, "attribute_id" => 3, "attribute_value_id" => 10,],
["product_id" => 7, "attribute_id" => 5, "attribute_value_id" => 21,],
["product_id" => 7, "attribute_id" => 2, "attribute_value_id" => 5,],
["product_id" => 7, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 8, "attribute_id" => 1, "attribute_value_id" => 12,],
["product_id" => 8, "attribute_id" => 4, "attribute_value_id" => 14,],
["product_id" => 8, "attribute_id" => 5, "attribute_value_id" => 21,],
["product_id" => 8, "attribute_id" => 6, "attribute_value_id" => 22,],
["product_id" => 8, "attribute_id" => 3, "attribute_value_id" => 9,],
["product_id" => 8, "attribute_id" => 2, "attribute_value_id" => 5,],
["product_id" => 9, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 9, "attribute_id" => 2, "attribute_value_id" => 1,],
["product_id" => 9, "attribute_id" => 3, "attribute_value_id" => 8,],
["product_id" => 9, "attribute_id" => 6, "attribute_value_id" => 22,],
["product_id" => 9, "attribute_id" => 4, "attribute_value_id" => 16,],
["product_id" => 9, "attribute_id" => 5, "attribute_value_id" => 19,],
["product_id" => 10, "attribute_id" => 4, "attribute_value_id" => 17,],
["product_id" => 10, "attribute_id" => 2, "attribute_value_id" => 5,],
["product_id" => 10, "attribute_id" => 5, "attribute_value_id" => 20,],
["product_id" => 10, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 10, "attribute_id" => 3, "attribute_value_id" => 9,],
["product_id" => 11, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 11, "attribute_id" => 2, "attribute_value_id" => 1,],
["product_id" => 11, "attribute_id" => 3, "attribute_value_id" => 4,],
["product_id" => 11, "attribute_id" => 4, "attribute_value_id" => 14,],
["product_id" => 11, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 12, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 12, "attribute_id" => 2, "attribute_value_id" => 5,],
["product_id" => 12, "attribute_id" => 4, "attribute_value_id" => 13,],
["product_id" => 12, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 12, "attribute_id" => 5, "attribute_value_id" => 20,],
["product_id" => 12, "attribute_id" => 3, "attribute_value_id" => 9,],
["product_id" => 13, "attribute_id" => 1, "attribute_value_id" => 11,],
["product_id" => 13, "attribute_id" => 2, "attribute_value_id" => 1,],
["product_id" => 13, "attribute_id" => 3, "attribute_value_id" => 9,],
["product_id" => 13, "attribute_id" => 4, "attribute_value_id" => 14,],
["product_id" => 13, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 13, "attribute_id" => 5, "attribute_value_id" => 19,],
["product_id" => 14, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 14, "attribute_id" => 2, "attribute_value_id" => 2,],
["product_id" => 14, "attribute_id" => 3, "attribute_value_id" => 8,],
["product_id" => 14, "attribute_id" => 4, "attribute_value_id" => 14,],
["product_id" => 14, "attribute_id" => 5, "attribute_value_id" => 20,],
["product_id" => 14, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 15, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 15, "attribute_id" => 2, "attribute_value_id" => 1,],
["product_id" => 15, "attribute_id" => 2, "attribute_value_id" => 6,],
["product_id" => 15, "attribute_id" => 5, "attribute_value_id" => 20,],
["product_id" => 15, "attribute_id" => 4, "attribute_value_id" => 16,],
["product_id" => 15, "attribute_id" => 6, "attribute_value_id" => 22,],
["product_id" => 35, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 35, "attribute_id" => 2, "attribute_value_id" => 6,],
["product_id" => 35, "attribute_id" => 3, "attribute_value_id" => 8,],
["product_id" => 35, "attribute_id" => 4, "attribute_value_id" => 15,],
["product_id" => 35, "attribute_id" => 5, "attribute_value_id" => 21,],
["product_id" => 35, "attribute_id" => 6, "attribute_value_id" => 22,],
["product_id" => 39, "attribute_id" => 1, "attribute_value_id" => 11,],
["product_id" => 39, "attribute_id" => 2, "attribute_value_id" => 2,],
["product_id" => 39, "attribute_id" => 3, "attribute_value_id" => 9,],
["product_id" => 39, "attribute_id" => 4, "attribute_value_id" => 15,],
["product_id" => 39, "attribute_id" => 5, "attribute_value_id" => 19,],
["product_id" => 39, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 1, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 1, "attribute_id" => 2, "attribute_value_id" => 1,],
["product_id" => 1, "attribute_id" => 4, "attribute_value_id" => 15,],
["product_id" => 1, "attribute_id" => 3, "attribute_value_id" => 10,],
["product_id" => 1, "attribute_id" => 5, "attribute_value_id" => 21,],
["product_id" => 1, "attribute_id" => 6, "attribute_value_id" => 22,],
["product_id" => 2, "attribute_id" => 1, "attribute_value_id" => 3,],
["product_id" => 2, "attribute_id" => 2, "attribute_value_id" => 1,],
["product_id" => 2, "attribute_id" => 3, "attribute_value_id" => 4,],
["product_id" => 2, "attribute_id" => 4, "attribute_value_id" => 13,],
["product_id" => 2, "attribute_id" => 5, "attribute_value_id" => 18,],
["product_id" => 2, "attribute_id" => 6, "attribute_value_id" => 22,],
["product_id" => 3, "attribute_id" => 1, "attribute_value_id" => 12,],
["product_id" => 3, "attribute_id" => 2, "attribute_value_id" => 5,],
["product_id" => 3, "attribute_id" => 4, "attribute_value_id" => 14,],
["product_id" => 3, "attribute_id" => 5, "attribute_value_id" => 20,],
["product_id" => 3, "attribute_id" => 6, "attribute_value_id" => 23,],
["product_id" => 4, "attribute_id" => 1, "attribute_value_id" => 11,],
["product_id" => 4, "attribute_id" => 2, "attribute_value_id" => 7,],
["product_id" => 4, "attribute_id" => 3, "attribute_value_id" => 10,],
["product_id" => 4, "attribute_id" => 5, "attribute_value_id" => 21,],
["product_id" => 4, "attribute_id" => 6, "attribute_value_id" => 23,]
];
}
}

File diff suppressed because it is too large Load Diff