2022-08-01 04:18:07 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Imports;
|
|
|
|
|
|
2022-08-09 16:56:52 +08:00
|
|
|
use App\Models\DailyStockRecord;
|
2022-08-01 04:18:07 +08:00
|
|
|
use App\Models\Goods;
|
|
|
|
|
use App\Models\GoodsBrand;
|
|
|
|
|
use App\Models\GoodsSku;
|
|
|
|
|
use App\Models\GoodsType;
|
2022-08-09 16:56:52 +08:00
|
|
|
use App\Utils\DateTimeUtils;
|
2022-08-19 22:52:04 +08:00
|
|
|
use Exception;
|
2022-08-01 04:18:07 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2022-08-09 16:56:52 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-08-01 04:18:07 +08:00
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
2022-08-01 17:06:43 +08:00
|
|
|
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
2022-08-01 04:18:07 +08:00
|
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use App\Utils\ArrayUtils;
|
|
|
|
|
|
2022-08-01 17:06:43 +08:00
|
|
|
class GoodsSkusImport implements ToCollection, SkipsEmptyRows
|
2022-08-01 04:18:07 +08:00
|
|
|
{
|
|
|
|
|
private $statusMap = [
|
|
|
|
|
'下架' => 0,
|
|
|
|
|
'在售' => 1,
|
|
|
|
|
'预警' => 2,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws ValidationException
|
2022-08-19 22:52:04 +08:00
|
|
|
* @throws Exception
|
2022-08-01 04:18:07 +08:00
|
|
|
*/
|
2022-08-19 22:52:04 +08:00
|
|
|
public function collection(Collection $rows)
|
2022-08-01 04:18:07 +08:00
|
|
|
{
|
2022-08-19 22:52:04 +08:00
|
|
|
unset($rows[0], $rows[1]);
|
|
|
|
|
$rows = $rows->toArray();
|
|
|
|
|
$types = $brands = $goodsCodes = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
$row = array_map(static function ($v) {
|
|
|
|
|
return trim($v);
|
|
|
|
|
}, $row);
|
|
|
|
|
$types[] = $row[1];
|
|
|
|
|
$brands[] = $row[2];
|
|
|
|
|
$goodsCodes[] = $row[3];
|
|
|
|
|
}
|
|
|
|
|
$validator = Validator::make($rows, [
|
2022-08-09 16:56:52 +08:00
|
|
|
'*.0' => ['required', 'string', 'max:191'],
|
|
|
|
|
'*.1' => ['required', 'string', 'max:191', 'exists:goods_types,name'],
|
|
|
|
|
'*.2' => ['string', 'max:191', 'exists:goods_brands,name'],
|
2022-08-09 10:57:03 +08:00
|
|
|
'*.3' => ['required', 'alpha_dash', 'max:32'],
|
2022-08-09 16:56:52 +08:00
|
|
|
'*.4' => ['required', 'string', 'max:191'],
|
2022-08-17 16:01:46 +08:00
|
|
|
'*.5' => ['required', 'alpha_dash', 'max:32'],
|
2022-08-01 04:18:07 +08:00
|
|
|
'*.6' => ['required', 'string', Rule::in(['下架', '在售', '预警'])],
|
|
|
|
|
'*.7' => ['required', 'max:10'],
|
|
|
|
|
'*.8' => ['required', 'max:10'],
|
|
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
throw new ValidationException($validator);
|
|
|
|
|
}
|
|
|
|
|
$types = GoodsType::query()->whereIn('name', $types)->get(['id', 'name'])->toArray();
|
|
|
|
|
$types = ArrayUtils::index($types, 'name');
|
|
|
|
|
$brands = GoodsBrand::query()->whereIn('name', $brands)->get(['id', 'name'])->toArray();
|
|
|
|
|
$brands = ArrayUtils::index($brands, 'name');
|
|
|
|
|
$hasGoods = Goods::query()->whereIn('goods_code', $goodsCodes)->get(['id', 'goods_code'])->toArray();
|
|
|
|
|
$hasGoods = ArrayUtils::index($hasGoods, 'goods_code');
|
|
|
|
|
$newGoods = $skus = [];
|
2022-08-19 22:52:04 +08:00
|
|
|
foreach ($rows as $row) {
|
2022-08-01 04:18:07 +08:00
|
|
|
$sku = [
|
2022-08-19 22:52:04 +08:00
|
|
|
'goods_id' => $row[3],
|
|
|
|
|
'title' => $row[4],
|
|
|
|
|
'sku_code' => $row[5],
|
|
|
|
|
'status' => $this->statusMap[$row[6]],
|
|
|
|
|
'num' => $row[7],
|
|
|
|
|
'cost' => $row[8],
|
2022-08-01 04:18:07 +08:00
|
|
|
];
|
|
|
|
|
// 主商品已存在
|
2022-08-19 22:52:04 +08:00
|
|
|
if (isset($hasGoods[$row[3]])) {
|
|
|
|
|
$sku['goods_id'] = $hasGoods[$row[3]]['id'];
|
2022-08-01 04:18:07 +08:00
|
|
|
} else {
|
|
|
|
|
// 新商品
|
2022-08-19 22:52:04 +08:00
|
|
|
$newGoods[$row[3]] = [
|
|
|
|
|
'title' => $row[0],
|
|
|
|
|
'type_id' => $types[$row[1]]['id'],
|
|
|
|
|
'brand_id' => $brands[$row[2]]['id'],
|
|
|
|
|
'goods_code' => $row[3],
|
2022-08-01 04:18:07 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
$skus[] = $sku;
|
|
|
|
|
}
|
2022-08-09 16:56:52 +08:00
|
|
|
DB::beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
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 &$newGoodsSku) {
|
|
|
|
|
$newGoodsSku['goods_id'] = isset($hasGoods[$newGoodsSku['goods_id']]) ? $hasGoods[$newGoodsSku['goods_id']]['id'] : $newGoodsSku['goods_id'];
|
|
|
|
|
}
|
|
|
|
|
unset($newGoodsSku);
|
2022-08-01 04:18:07 +08:00
|
|
|
}
|
2022-08-09 16:56:52 +08:00
|
|
|
$goodsSku = new GoodsSku();
|
|
|
|
|
$goodsSku->batchInsert(array_values($skus));
|
2022-08-19 22:52:04 +08:00
|
|
|
$goodsIds = Goods::query()->whereIn('goods_code', $goodsCodes)->pluck('id')->toArray();
|
|
|
|
|
$skuIds = GoodsSku::query()->whereIn('goods_id', $goodsIds)->pluck('id')->toArray();
|
2022-08-09 16:56:52 +08:00
|
|
|
$newRecords = [];
|
2022-08-19 22:52:04 +08:00
|
|
|
$day = DateTimeUtils::getToday();
|
|
|
|
|
foreach ($skuIds as $skuId) {
|
2022-08-09 16:56:52 +08:00
|
|
|
$newRecords[] = [
|
2022-08-19 22:52:04 +08:00
|
|
|
'sku_id' => $skuId,
|
|
|
|
|
'day' => $day,
|
2022-08-09 16:56:52 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
$record = new DailyStockRecord();
|
|
|
|
|
$record->batchInsert($newRecords);
|
|
|
|
|
DB::commit();
|
2022-08-19 22:52:04 +08:00
|
|
|
} catch (Exception $exception) {
|
2022-08-09 16:56:52 +08:00
|
|
|
DB::rollBack();
|
2022-08-19 22:52:04 +08:00
|
|
|
// 返回错误
|
|
|
|
|
throw $exception;
|
2022-08-01 04:18:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|