From 3c0a65b08e5caa1e133435326c8fdc34aee05fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Fri, 11 Aug 2023 14:21:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#10000=20=E6=96=B0=E5=95=86=E5=93=81?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=AF=BC=E5=85=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Imports/GoodsSkusImport.php | 34 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app/Imports/GoodsSkusImport.php b/app/Imports/GoodsSkusImport.php index fedb61d..3a6b9e0 100644 --- a/app/Imports/GoodsSkusImport.php +++ b/app/Imports/GoodsSkusImport.php @@ -58,16 +58,16 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows 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'); + $types = GoodsType::query()->whereIn('name', $types)->pluck('id', 'name')->toArray(); + + $brands = GoodsBrand::query()->whereIn('name', $brands)->pluck('id', 'name')->toArray(); + + $hasGoods = Goods::query()->whereIn('goods_code', $goodsCodes)->pluck('id', 'goods_code')->toArray(); + $newGoods = $skus = []; foreach ($collection as $row) { $sku = [ - 'goods_id' => $row[3], + 'goods_code' => $row[3], 'title' => $row[4], 'sku_code' => $row[5], 'status' => $this->statusMap[$row[6]], @@ -75,15 +75,15 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows 'cost' => $row[8], ]; // 主商品已存在 - if (isset($hasGoods[$row[3]])) { - $sku['goods_id'] = $hasGoods[$row[3]]['id']; + if (isset($hasGoods[$sku['goods_code']])) { + $sku['goods_id'] = $hasGoods[$sku['goods_code']]; } else { // 新商品 - $newGoods[$row[3]] = [ + $newGoods[$sku['goods_code']] = [ 'title' => $row[0], - 'type_id' => $types[$row[1]]['id'], - 'brand_id' => $brands[$row[2]]['id'], - 'goods_code' => $row[3], + 'type_id' => $types[$row[1]], + 'brand_id' => $brands[$row[2]], + 'goods_code' => $sku['goods_code'], ]; } $skus[] = $sku; @@ -93,11 +93,11 @@ class GoodsSkusImport implements ToCollection, SkipsEmptyRows 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'); + $hasGoods = Goods::query()->whereIn('goods_code', array_column($newGoods, 'goods_code'))->pluck('id', 'goods_code')->toArray(); foreach ($skus as &$newGoodsSku) { - $newGoodsSku['goods_id'] = isset($hasGoods[$newGoodsSku['goods_id']]) ? $hasGoods[$newGoodsSku['goods_id']]['id'] : $newGoodsSku['goods_id']; - $newGoodsSku['external_sku_id'] = isset($hasGoods[$newGoodsSku['goods_id']]) ? $hasGoods[$newGoodsSku['goods_id']]['goods_code'] . '_' . $newGoodsSku['sku_code'] : ''; + $newGoodsSku['goods_id'] = $hasGoods[$newGoodsSku['goods_code']]; + $newGoodsSku['external_sku_id'] = $newGoodsSku['goods_code'] . '_' . $newGoodsSku['sku_code']; + unset($newGoodsSku['goods_code']); } unset($newGoodsSku); }