mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
92 lines
3.4 KiB
PHP
92 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\Goods;
|
|
use App\Models\GoodsBrand;
|
|
use App\Models\GoodsSku;
|
|
use App\Models\GoodsType;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Utils\ArrayUtils;
|
|
|
|
class GoodsSkusImport implements ToCollection
|
|
{
|
|
private $statusMap = [
|
|
'下架' => 0,
|
|
'在售' => 1,
|
|
'预警' => 2,
|
|
];
|
|
|
|
|
|
/**
|
|
* @throws ValidationException
|
|
*/
|
|
public function collection(Collection $collection)
|
|
{
|
|
unset($collection[0], $collection[1]);
|
|
$arr = $collection->toArray();
|
|
$validator = Validator::make($arr, [
|
|
'*.0' => ['required', 'string', 'max:255'],
|
|
'*.1' => ['required', 'string', 'max:255', 'exists:goods_types,name'],
|
|
'*.2' => ['string', 'max:255', 'exists:goods_brands,name'],
|
|
'*.3' => ['required', 'alpha_dash', 'max:32', 'unique:goods,goods_code'],
|
|
'*.4' => ['required', 'string', 'max:255'],
|
|
'*.5' => ['required', 'distinct', 'alpha_dash', 'max:32'],
|
|
'*.6' => ['required', 'string', Rule::in(['下架', '在售', '预警'])],
|
|
'*.7' => ['required', 'max:10'],
|
|
'*.8' => ['required', 'max:10'],
|
|
]);
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
$types = array_column($arr, 1);
|
|
$types = GoodsType::query()->whereIn('name', $types)->get(['id', 'name'])->toArray();
|
|
$types = ArrayUtils::index($types, 'name');
|
|
$brands = array_column($arr, 2);
|
|
$brands = GoodsBrand::query()->whereIn('name', $brands)->get(['id', 'name'])->toArray();
|
|
$brands = ArrayUtils::index($brands, 'name');
|
|
$goodsCodes = array_column($arr, 3);
|
|
$hasGoods = Goods::query()->whereIn('goods_code', $goodsCodes)->get(['id', 'goods_code'])->toArray();
|
|
$hasGoods = ArrayUtils::index($hasGoods, 'goods_code');
|
|
$newGoods = $skus = [];
|
|
foreach ($arr as $item) {
|
|
$sku = [
|
|
'goods_id' => $item[3],
|
|
'title' => $item[4],
|
|
'sku_code' => $item[5],
|
|
'status' => $this->statusMap[$item[6]],
|
|
'num' => $item[7],
|
|
'cost' => $item[8],
|
|
];
|
|
// 主商品已存在
|
|
if (isset($hasGoods[$item[3]])) {
|
|
$sku['goods_id'] = $hasGoods[$item[3]]['id'];
|
|
} else {
|
|
// 新商品
|
|
$newGoods[$item[3]] = [
|
|
'title' => $item[0],
|
|
'type_id' => $types[$item[1]]['id'],
|
|
'brand_id' => $brands[$item[2]]['id'],
|
|
'goods_code' => $item[3],
|
|
];
|
|
}
|
|
$skus[] = $sku;
|
|
}
|
|
if ($newGoods) {
|
|
$goods = new Goods();
|
|
$goods->batchInsert(array_values($newGoods));
|
|
$hasGoods = Goods::query()->whereIn('goods_code', array_column($newGoods, 'goods_code'))->get(['id', 'goods_code'])->toArray();
|
|
$hasGoods = ArrayUtils::index($hasGoods, 'goods_code');
|
|
foreach ($skus as &$sku) {
|
|
$sku['goods_id'] = isset($hasGoods[$sku['goods_id']]) ? $hasGoods[$sku['goods_id']]['id'] : $sku['goods_id'];
|
|
}
|
|
}
|
|
$goodsSku = new GoodsSku();
|
|
$goodsSku->batchInsert(array_values($skus));
|
|
}
|
|
}
|