From 1c4e9cc040338728302eac1978803948fad75da4 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, 27 Jan 2023 23:03:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#10000=20=E7=9B=98=E7=82=B9=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Imports/InventoryImport.php | 52 ++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/app/Imports/InventoryImport.php b/app/Imports/InventoryImport.php index 1feb3e6..e84ee28 100644 --- a/app/Imports/InventoryImport.php +++ b/app/Imports/InventoryImport.php @@ -32,46 +32,64 @@ class InventoryImport implements ToCollection, SkipsEmptyRows } unset($row); $hasGoods = Goods::query()->whereIn('goods_code', $goodsCodes)->get(['id', 'goods_code'])->toArray(); + $hasGoodsIds = array_column($hasGoods, 'id'); $hasGoods = ArrayUtils::index($hasGoods, 'goods_code'); $updateIds = []; $day = DateTimeUtils::getToday(); $dateTime = date('Y-m-d H:i:s'); + $hasGoodsSkus = GoodsSku::query() + ->whereIn('goods_id', $hasGoodsIds) + ->get(['id', 'status', 'stock', 'cost', 'sku_code', 'goods_id']) + ->toArray(); foreach ($collection as $row) { if (!isset($hasGoods[$row[0]])) { continue; } - $goodsSku = GoodsSku::query() - ->select(['id', 'status', 'stock', 'cost']) - ->where('goods_id', $hasGoods[$row[0]]['id']) - ->where('sku_code', $row[4]) - ->first(); + $goodsId = $hasGoods[$row[0]]['id']; + $goodsSku = []; + foreach ($hasGoodsSkus as $item) { + if ($item['sku_code'] === $row[4] && $item['goods_id'] === $goodsId) { + $goodsSku = $item; + break; + } + } if (empty($goodsSku)) { Log::warning(json_encode($row, 256) . '=====库存导入未找到'); continue; } - $goodsSku->stock = $row[6] + $row[7]; - if ('下架' === $goodsSku->status) { - $goodsSku->status = 1; + if ('下架' === $goodsSku['status']) { + GoodsSku::query()->where('id', $goodsSku['id'])->update([ + 'stock' => $row[6] + $row[7], + 'cost' => $row[8], + 'status' => 1, + ]); + } else { + GoodsSku::query()->where('id', $goodsSku['id'])->update([ + 'stock' => $row[6] + $row[7], + 'cost' => $row[8], + ]); } - $goodsSku->cost = $row[8]; - $goodsSku->save(); - $updateIds[] = $goodsSku->id; - DailyStockRecord::query()->where('sku_id', $goodsSku->id)->where('day', $day)->update([ + $updateIds[] = $goodsSku['id']; + DailyStockRecord::query()->where('sku_id', $goodsSku['id'])->where('day', $day)->update([ 'arrived_today_num' => $row[7], 'inventory' => $row[6], 'inventory_time' => $dateTime ]); } + sleep(2); $onSkuIds = GoodsSku::query() ->where('status', '>', 0) ->pluck('id') ->toArray(); $downSkuIds = array_diff($onSkuIds, $updateIds); - foreach ($downSkuIds as $downSkuId) { - $goodsSku = GoodsSku::query()->select(['id', 'yesterday_num', 'stock'])->find($downSkuId); - $goodsSku->yesterday_num -= $goodsSku->stock; - $goodsSku->stock = 0; - $goodsSku->save(); + $goodsSkus = GoodsSku::query()->whereIn('id', $downSkuIds) + ->get(['id', 'yesterday_num', 'stock']) + ->toArray(); + foreach ($goodsSkus as $goodsSku) { + GoodsSku::query()->where('id', $goodsSku['id'])->update([ + 'yesterday_num' => $goodsSku['yesterday_num'] - $goodsSku['stock'], + 'stock' => 0, + ]); } sleep(2); event(new StockUpdateEvent($onSkuIds, 1));