diff --git a/app/Events/StockUpdateEvent.php b/app/Events/StockUpdateEvent.php index 4caca63..5a81484 100644 --- a/app/Events/StockUpdateEvent.php +++ b/app/Events/StockUpdateEvent.php @@ -17,7 +17,7 @@ class StockUpdateEvent public $goodsSku; public $goodsSkus; - + public $isBatch; /** * Create a new event instance. @@ -26,8 +26,9 @@ class StockUpdateEvent * * @return void */ - public function __construct($data) + public function __construct($data, $isBatch = 0) { + $this->isBatch = $isBatch; if (is_array($data)) { // ids集合 $this->goodsSkus = GoodsSku::query()->whereIn('id', $data)->with(['goods:id,goods_code'])->get(); diff --git a/app/Imports/InventoryImport.php b/app/Imports/InventoryImport.php index ae433e2..4e087b4 100644 --- a/app/Imports/InventoryImport.php +++ b/app/Imports/InventoryImport.php @@ -73,6 +73,6 @@ class InventoryImport implements ToCollection, SkipsEmptyRows $goodsSku->stock = 0; $goodsSku->save(); } - event(new StockUpdateEvent($onSkuIds)); + event(new StockUpdateEvent($onSkuIds, 1)); } } diff --git a/app/Listeners/StockUpdateListener.php b/app/Listeners/StockUpdateListener.php index 9bf7b67..aa28787 100644 --- a/app/Listeners/StockUpdateListener.php +++ b/app/Listeners/StockUpdateListener.php @@ -43,7 +43,13 @@ class StockUpdateListener ->where('is_sync', 1) ->where('external_sku_id', $event->goodsSku->goods['goods_code'] . '_' . $event->goodsSku->sku_code) ->get(); - BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); + if ($event->isBatch) { + BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); + } else { + foreach ($businessGoodsSkus as $businessGoodsSku) { + BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false); + } + } } if (isset($event->goodsSkus)) { foreach ($event->goodsSkus as $goodsSku) { @@ -54,7 +60,13 @@ class StockUpdateListener ->where('is_sync', 1) ->where('external_sku_id', $goodsSku->goods['goods_code'] . '_' . $goodsSku->sku_code) ->get(); - BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); + if ($event->isBatch) { + BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); + } else { + foreach ($businessGoodsSkus as $businessGoodsSku) { + BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false); + } + } } } }