商品导入
parent
dca26e9da2
commit
f013e73d61
|
|
@ -6,6 +6,11 @@ use Dcat\Admin\Widgets\Form;
|
|||
use Dcat\EasyExcel\Excel;
|
||||
use Dcat\EasyExcel\Support\SheetCollection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Peidikeji\Goods\Models\Goods;
|
||||
use Peidikeji\Goods\Models\GoodsBrand;
|
||||
use Peidikeji\Goods\Models\GoodsCategory;
|
||||
use Peidikeji\Goods\Models\GoodsType;
|
||||
|
||||
class ImportForm extends Form
|
||||
{
|
||||
|
|
@ -14,16 +19,103 @@ class ImportForm extends Form
|
|||
public function handle(array $input)
|
||||
{
|
||||
$disk = Storage::disk('public');
|
||||
Excel::import($disk->path($input['file']))->headings(false)->first()->chunk(500, function (SheetCollection $collection) {
|
||||
$rows = $collection->toArray();
|
||||
// dd($rows);
|
||||
});
|
||||
|
||||
return $this->response()->success('导入成功');
|
||||
Excel::import($disk->path($input['file']))->headings(false)->first()->chunk(500, fn(SheetCollection $collection) => $this->mapGoods($collection->toArray()));
|
||||
|
||||
return $this->response()->success('导入成功')->refresh();
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
$this->file('file')->autoUpload()->uniqueName()->move('goods/import')->accept('xlsx,xls')->disk('public');
|
||||
}
|
||||
|
||||
protected function mapGoods($rows)
|
||||
{
|
||||
array_shift($rows);
|
||||
foreach ($rows as $row) {
|
||||
$index = 0;
|
||||
$sn = $row[$index];
|
||||
$category = $row[++$index];
|
||||
$brand = $row[++$index];
|
||||
$type = $row[++$index];
|
||||
$name = $row[++$index];
|
||||
$description = $row[++$index];
|
||||
$cover = $row[++$index];
|
||||
$images = $row[++$index];
|
||||
$content = $row[++$index];
|
||||
$price = $row[++$index];
|
||||
$stock = $row[++$index];
|
||||
$attr = $row[++$index];
|
||||
$spec = $row[++$index];
|
||||
$part = $row[++$index];
|
||||
|
||||
if (!$sn) {
|
||||
throw new \Exception('编号必填');
|
||||
}
|
||||
if (!$name) {
|
||||
throw new \Exception('名称必填');
|
||||
}
|
||||
if (!$category) {
|
||||
throw new \Exception('分类必填');
|
||||
}
|
||||
$category = GoodsCategory::firstOrCreate(['name' => $category]);
|
||||
if ($brand) {
|
||||
$brand = GoodsBrand::firstOrCreate(['name' => $brand]);
|
||||
}
|
||||
if ($type) {
|
||||
$type = GoodsType::firstOrCreate(['name' => $type]);
|
||||
}
|
||||
$attributes = [
|
||||
'category_id' => $category->id,
|
||||
'brand_id' => $brand?->id,
|
||||
'type_id' => $type?->id,
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
'cover_image' => $cover,
|
||||
'images' => $images,
|
||||
'content' => $content,
|
||||
'price' => $price,
|
||||
'stock' => $stock,
|
||||
'attr' => $this->parseAttr($attr),
|
||||
'spec' => $this->parseAttr($spec),
|
||||
'part' => $this->parseAttr($part),
|
||||
];
|
||||
|
||||
$goods = Goods::where('goods_sn', $sn)->first();
|
||||
if (!$goods) {
|
||||
$goods = new Goods();
|
||||
}
|
||||
$goods->updateQuietly($attributes);
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseAttr($str = '')
|
||||
{
|
||||
if (!$str) {
|
||||
return null;
|
||||
}
|
||||
$attr = [];
|
||||
if (Str::contains($str, '\\n')) {
|
||||
preg_match_all('/(.*)\n(.*)/', $str, $matches);
|
||||
array_shift($matches);
|
||||
} else {
|
||||
$matches = [$str];
|
||||
}
|
||||
foreach($matches as $item) {
|
||||
preg_match_all('/(.*)\[(.*)\]/', $item[0], $itemMatch);
|
||||
$name = data_get($itemMatch, '1.0');
|
||||
$values = [];
|
||||
foreach(explode(',', data_get($itemMatch, '2.0')) as $k) {
|
||||
$i = explode(':', $k);
|
||||
array_push($values, ['name' => data_get($i, '0'), 'value' => data_get($i, '1')]);
|
||||
}
|
||||
array_push($attr, [
|
||||
'name' => $name,
|
||||
'values' => $values,
|
||||
]);
|
||||
}
|
||||
|
||||
return $attr;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class Goods extends Model
|
|||
|
||||
protected $fillable = [
|
||||
'category_id', 'type_id', 'brand_id',
|
||||
'goods_sn', 'name', 'cover_image', 'description', 'content',
|
||||
'goods_sn', 'name', 'cover_image', 'images', 'description', 'content',
|
||||
'price', 'on_sale', 'is_recommend', 'sold_count', 'stock',
|
||||
'attr', 'part', 'spec',
|
||||
];
|
||||
|
|
|
|||
Loading…
Reference in New Issue