6
0
Fork 0
jiqu-library-server/app/Admin/Imports/Product.php

179 lines
6.2 KiB
PHP

<?php
namespace App\Admin\Imports;
use App\Exceptions\ImportException;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Illuminate\Support\Facades\Storage;
use App\Models\{ProductSpu, ProductCategory, ProductGroup};
use Illuminate\Support\Str;
class Product
{
public function load($file)
{
$reader = ReaderEntityFactory::createXLSXReader();
$reader->open($file);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $num => $row) {
if ($num === 1) {
continue;
}
$cells = $row->toArray();
$goods = [
'name' => $cells[0],
'subtitle' => $cells[1],
'shipping_template_id' => 1,
];
$category = ProductCategory::where('name', $cells[2])->firstOrFail();
$goods['category_id'] = $category->id;
// 图片组
if ($path = $cells[3]) {
if (Str::startsWith($path, ['http://', 'https://'])) {
$images = explode(',', $path);
} else {
$images = $this->getImageUrlFromPath($path);
}
if (count($images) > 0) {
$goods['cover'] = $images[0];
$goods['images'] = $images;
}
}
// 运费模板
if ($cell_11 = data_get($cells, 11)) {
$goods['shipping_template_id'] = $cell_11;
}
// 重量
if ($cell_12 = data_get($cells, 12)) {
$goods['weight'] = $cell_12;
}
// 详细描述
if ($cell_13 = data_get($cells, 13)) {
$description = '';
foreach(explode(',', $cell_13) as $item) {
$description .= '<p><img src="'.$item.'"/></p>';
}
$goods['description'] = $description;
}
$goods = array_merge($goods, [
'sell_price' => $cells[4] * 100,
'market_price' => $cells[5] * 100,
'cost_price' => $cells[6] * 100,
'vip_price' => $cells[7] * 100,
'sales_value' => $cells[8] * 100,
]);
$product = ProductSpu::updateOrCreate(['name' => $goods['name']],$goods);
// 属性规格
$coll_9 = data_get($cells, 9);
$cell_10 = data_get($cells, 10);
if ($coll_9) {
$format_specs = $this->formatAttr($coll_9);
$format_attrs = $this->formatAttr($cell_10);
$spec_group = $this->getSpec($cells[0], $coll_9, $cell_10);
$attrs = [];
foreach($spec_group->attrs as $index => $item) {
$attr = ['name' => $item['title'], 'attrs' => []];
foreach(explode(PHP_EOL, $item['value']) as $value) {
array_push($attr['attrs'], ['name' => $value, 'value' => data_get($format_attrs, "$index.values.$value")]);
}
array_push($attrs, $attr);
}
$product->update(['attrs' => $attrs]);
$specs = [];
$product->specs()->delete();
foreach($spec_group->specs as $index => $item) {
$spec = ['name' => $item['title'], 'items' => []];
foreach(explode(PHP_EOL, $item['value']) as $value) {
array_push($spec['items'], ['name' => $value, 'value' => data_get($format_specs, "$index.values.$value")]);
}
array_push($specs, $spec);
}
$product->specs()->createMany($specs);
}
}
}
$reader->close();
}
protected function getImageUrlFromPath($dir)
{
$disk = Storage::disk();
$files = $disk->files($dir);
$images = [];
foreach($files as $filename) {
array_push($images, $disk->url($filename));
}
return $images;
}
protected function getSpec($name, $specs, $attrs)
{
if (Str::contains($specs, ':')) {
// 创建新的规格
$spec = ProductGroup::updateOrCreate([
'name' => $name
], [
'attrs' => $this->formatGroupAttr($this->formatAttr($attrs)),
'specs' => $this->formatGroupAttr($this->formatAttr($specs))
]);
} else {
$spec = ProductGroup::where('name', $specs)->firstOrFail();
}
return $spec;
}
/**
* 颜色[灰色:0,银色:0]
* 尺寸[13寸:0,14寸:500,16寸:1000]
* 处理器[M1:0,M1 Pro:1000,M1 Max:1500]
* 内存[8G:0,16G:1000,32G:2000]
* 硬盘[256G:0,512G:1000,1TB:2000]
*
* [
* ['name' => '颜色', 'values' => ['灰色' => 0, '银色' => 0]],
* ['name' => '尺寸', 'values' => ['13寸' => 0, '14寸' => 500, '16寸' => '1000']]
* ...
* ]
*/
protected function formatAttr($attrs)
{
$attr_data = [];
if (!Str::contains($attrs, ':')) {
return $attr_data;
}
foreach(explode("\n", $attrs) as $item) {
$name = explode('[', $item)[0];
$attr_values = [];
foreach(explode(',', Str::between($item, '[', ']')) as $item1) {
$item1_explode = explode(':', $item1);
$attr_values[$item1_explode[0]] = $item1_explode[1];
}
array_push($attr_data, ['name' => $name, 'values' => $attr_values]);
}
return $attr_data;
}
protected function formatGroupAttr($data)
{
$list = [];
foreach($data as $item) {
array_push($list, ['title' => $item['name'], 'value' => implode(PHP_EOL, array_keys($item['values']))]);
}
return $list;
}
}