erp/app/Console/Commands/BusinessGoodsSkuToLocal.php

104 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use App\Models\BusinessGoodsSku;
use App\Models\GoodsBrand;
use App\Models\GoodsType;
use App\Models\Goods;
use App\Models\GoodsSku;
use Illuminate\Console\Command;
class BusinessGoodsSkuToLocal extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'goods_sku:to_local';
/**
* The console command description.
*
* @var string
*/
protected $description = '将已下载的 花富贵儿 的商品保存到本地商品列表(注意: 先在店铺列表页面下载商品)';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$num = 0;
BusinessGoodsSku::chunk(100, function ($businessGoodsSkus) use ($num) {
foreach ($businessGoodsSkus as $businessGoodsSku) {
if (empty($businessGoodsSku['goods_name']) || empty($businessGoodsSku['external_sku_id'])) {
continue;
}
$businessGoodsName = explode(' ', $businessGoodsSku['goods_name']);
$count = count($businessGoodsName);
if (1 === $count) {
continue;
} elseif (2 === $count) {
// 商品名称 规格名称
$brand = 0;
$type = '未分类';
$goodsName = $businessGoodsName[0];
$skuName = $businessGoodsName[1];
} elseif (3 === $count) {
// 种类 商品名称 规格名称
$brand = 0;
$type = $businessGoodsName[0];
$goodsName = $businessGoodsName[1];
$skuName = $businessGoodsName[2];
} else {
// 品牌 种类 商品名称 规格名称
$brand = $businessGoodsName[0];
$type = $businessGoodsName[1];
$goodsName = $businessGoodsName[2];
$skuName = $businessGoodsName[3];
foreach ($businessGoodsName as $k => $v) {
if ($k > 3) {
$skuName .= $v;
}
}
}
if ($brand) {
$goodsBrand = GoodsBrand::firstOrCreate(['name' => $brand]);
$brand = $goodsBrand->id;
}
$goodsType = GoodsType::firstOrCreate(['name' => $type]);
[$goodsCode, $skuCode] = explode('_', $businessGoodsSku['external_sku_id']);
$goods = [
'title' => $goodsName,
'type_id' => $goodsType->id,
'brand_id' => $brand,
'goods_code' => $goodsCode,
];
$goods = Goods::firstOrCreate($goods);
$goodsSku = [
'goods_id' => $goods->id,
'title' => $skuName,
'sku_code' => $skuCode,
];
GoodsSku::firstOrCreate($goodsSku);
++$num;
}
});
$this->info('导入成功: ' . $num);
}
}