From b2b3def74004700efdb2f6b95123778900effc30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Sat, 18 Nov 2023 14:35:19 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=BA=93=E5=AD=98=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Events/BatchStockUpdateEvent.php | 45 +++++++++++++++++ app/Events/BusinessOrdersUpdate.php | 21 +++----- app/Events/StockUpdateEvent.php | 21 ++------ .../Goods/GoodsCombinationController.php | 2 - .../Controllers/Goods/GoodsSkusController.php | 4 +- app/Imports/CombinationGoodsImport.php | 3 -- app/Imports/InventoryImport.php | 5 +- app/Imports/NewSetImport.php | 6 +-- app/Listeners/BatchStockUpdateListener.php | 50 +++++++++++++++++++ app/Listeners/BatchStockWarning.php | 49 ++++++++++++++++++ .../CombinationGoodsStockUpdateListener.php | 15 +++--- app/Listeners/StockUpdateListener.php | 43 +++------------- app/Listeners/StockWarning.php | 35 +++---------- app/Listeners/UpdateBusinessGoodsStock.php | 12 +++-- app/Providers/EventServiceProvider.php | 11 +++- 15 files changed, 202 insertions(+), 120 deletions(-) create mode 100644 app/Events/BatchStockUpdateEvent.php create mode 100644 app/Listeners/BatchStockUpdateListener.php create mode 100644 app/Listeners/BatchStockWarning.php diff --git a/app/Events/BatchStockUpdateEvent.php b/app/Events/BatchStockUpdateEvent.php new file mode 100644 index 0000000..0036b31 --- /dev/null +++ b/app/Events/BatchStockUpdateEvent.php @@ -0,0 +1,45 @@ +goodsSkus = GoodsSku::query()->whereIn('id', $goodsSkuIds)->get(); + $this->stockWarning = $stockWarning; + $this->combinationGoodsUpdate = $combinationGoodsUpdate; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Events/BusinessOrdersUpdate.php b/app/Events/BusinessOrdersUpdate.php index c07a90e..3556d88 100644 --- a/app/Events/BusinessOrdersUpdate.php +++ b/app/Events/BusinessOrdersUpdate.php @@ -16,35 +16,28 @@ class BusinessOrdersUpdate { use Dispatchable, InteractsWithSockets, SerializesModels; + public $businessGoodSku; public $num; - public $businessGoods; public $goodsSku; - public $goodsSkus; - public $combinationGoodsUpdate; + public $combinationGoodsUpdate = true; /** * Create a new event instance. * * @return void */ - public function __construct($item, $num) + public function __construct($businessGoodSku, $num) { - $this->combinationGoodsUpdate = false; - $this->businessGoods = $item->toArray(); + $this->businessGoodSku = $businessGoodSku->toArray(); $this->num = $num; $this->updateStock(); } private function updateStock() { - try { - $this->goodsSku = GoodsSku::query() - ->where('external_sku_id', $this->businessGoods['external_sku_id']) - ->first(); - } catch (\Exception $e) { - Log::error('事件库存更新失败: ' . $e->getMessage()); - return false; - } + $this->goodsSku = GoodsSku::query() + ->where('external_sku_id', $this->businessGoodSku['external_sku_id']) + ->first(); if ($this->goodsSku) { $this->goodsSku->stock += $this->num; $this->goodsSku->save(); diff --git a/app/Events/StockUpdateEvent.php b/app/Events/StockUpdateEvent.php index fb8acf8..f29af60 100644 --- a/app/Events/StockUpdateEvent.php +++ b/app/Events/StockUpdateEvent.php @@ -2,7 +2,6 @@ namespace App\Events; -use App\Models\GoodsSku; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; @@ -14,30 +13,16 @@ use Illuminate\Queue\SerializesModels; class StockUpdateEvent { use Dispatchable, InteractsWithSockets, SerializesModels; - public $goodsSku; - public $goodsSkus; - public $isBatch; - public $combinationGoodsUpdate; /** * Create a new event instance. * - * @param $data array|object - * - * @return void + * @param $goodsSku */ - public function __construct($data, $isBatch = 0, $combinationGoodsUpdate = false) + public function __construct($goodsSku) { - $this->isBatch = $isBatch; - $this->combinationGoodsUpdate = $combinationGoodsUpdate; - if (is_array($data)) { - // ids集合 - $this->goodsSkus = GoodsSku::query()->whereIn('id', $data)->get(); - } else { - // GoodsSku Elo模型对象 - $this->goodsSku = $data; - } + $this->goodsSku = $goodsSku; } /** diff --git a/app/Http/Controllers/Goods/GoodsCombinationController.php b/app/Http/Controllers/Goods/GoodsCombinationController.php index 7cbfb92..f81afdb 100644 --- a/app/Http/Controllers/Goods/GoodsCombinationController.php +++ b/app/Http/Controllers/Goods/GoodsCombinationController.php @@ -2,7 +2,6 @@ namespace App\Http\Controllers\Goods; -use App\Events\StockUpdateEvent; use App\Http\Controllers\Controller; use App\Http\Resources\GoodsSkuResource; use App\Imports\CombinationGoodsImport; @@ -141,7 +140,6 @@ class GoodsCombinationController extends Controller CombinationGood::query()->create(['goods_sku_id' => $sku->id, 'item_id' => $item['item_id'], 'item_num' => $item['item_num']]); } DB::commit(); -// event(new StockUpdateEvent($sku, 0, true)); } catch (\Exception $exception) { DB::rollBack(); $this->res = [ diff --git a/app/Http/Controllers/Goods/GoodsSkusController.php b/app/Http/Controllers/Goods/GoodsSkusController.php index 25d7360..c34032f 100644 --- a/app/Http/Controllers/Goods/GoodsSkusController.php +++ b/app/Http/Controllers/Goods/GoodsSkusController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Goods; +use App\Events\BatchStockUpdateEvent; use App\Events\StockUpdateEvent; use App\Exports\GoodsSkusExport; use App\Http\Controllers\Controller; @@ -234,7 +235,8 @@ class GoodsSkusController extends Controller $log = new LogModel(); $log->batchInsert($logs); DB::commit(); - event(new StockUpdateEvent(array_column($request->skus, 'id'))); + // 批量更新 + event(new BatchStockUpdateEvent(array_column($request->skus, 'id'))); } catch (\Exception $exception) { DB::rollBack(); $this->res = [ diff --git a/app/Imports/CombinationGoodsImport.php b/app/Imports/CombinationGoodsImport.php index e836795..f56a137 100644 --- a/app/Imports/CombinationGoodsImport.php +++ b/app/Imports/CombinationGoodsImport.php @@ -2,10 +2,8 @@ namespace App\Imports; -use App\Events\StockUpdateEvent; use App\Models\CombinationGood; use App\Models\GoodsSku; -use App\Models\GoodsSkuLocation; use App\Utils\ArrayUtils; use Illuminate\Support\Facades\DB; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; @@ -72,7 +70,6 @@ class CombinationGoodsImport implements ToArray, SkipsEmptyRows, WithStartRow CombinationGood::query()->create(['goods_sku_id' => $sku->id, 'item_id' => $skus[$item['item_code']]['id'], 'item_num' => $item['item_num']]); } DB::commit(); -// event(new StockUpdateEvent($sku, 0, true)); } catch (\Exception $exception) { DB::rollBack(); } diff --git a/app/Imports/InventoryImport.php b/app/Imports/InventoryImport.php index 74b2d37..5d1e7aa 100644 --- a/app/Imports/InventoryImport.php +++ b/app/Imports/InventoryImport.php @@ -2,6 +2,7 @@ namespace App\Imports; +use App\Events\BatchImportStockEvent; use App\Jobs\SyncCostToMiaoXuan; use App\Models\DailyStockRecord; use App\Models\GoodsSku; @@ -11,7 +12,6 @@ use Exception; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\ToArray; use App\Utils\ArrayUtils; -use App\Events\StockUpdateEvent; class InventoryImport implements ToArray, SkipsEmptyRows { @@ -98,6 +98,7 @@ class InventoryImport implements ToArray, SkipsEmptyRows } } sleep(2); - event(new StockUpdateEvent($onSkuIds, 1)); + // 批量更新 + event(new BatchImportStockEvent($onSkuIds)); } } diff --git a/app/Imports/NewSetImport.php b/app/Imports/NewSetImport.php index 0320a2d..37f7848 100644 --- a/app/Imports/NewSetImport.php +++ b/app/Imports/NewSetImport.php @@ -2,16 +2,15 @@ namespace App\Imports; +use App\Events\BatchImportStockEvent; use App\Jobs\SyncCostToMiaoXuan; use App\Models\DailyStockRecord; use App\Models\GoodsSku; -use App\Models\TodayPrice; use App\Utils\DateTimeUtils; use Exception; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\ToArray; use App\Utils\ArrayUtils; -use App\Events\StockUpdateEvent; class NewSetImport implements ToArray, SkipsEmptyRows { @@ -59,6 +58,7 @@ class NewSetImport implements ToArray, SkipsEmptyRows $record->save(); } sleep(2); - event(new StockUpdateEvent($updateIds, 1)); + // 批量更新 + event(new BatchImportStockEvent($updateIds)); } } diff --git a/app/Listeners/BatchStockUpdateListener.php b/app/Listeners/BatchStockUpdateListener.php new file mode 100644 index 0000000..79c233b --- /dev/null +++ b/app/Listeners/BatchStockUpdateListener.php @@ -0,0 +1,50 @@ +whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']); + if (empty($shops)) { + return; + } + foreach ($shops as $shop) { + foreach ($event->goodsSkus as $goodsSku) { + $num = $goodsSku->stock; + $businessGoodsSkus = BusinessGoodsSku::query() + ->select(['goods_id', 'sku_id', 'external_sku_id']) + ->where('shop_id', $shop->id) + ->where('is_sync', 1) + ->where('external_sku_id', $goodsSku->external_sku_id) + ->get(); + BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); + } + } + } +} diff --git a/app/Listeners/BatchStockWarning.php b/app/Listeners/BatchStockWarning.php new file mode 100644 index 0000000..64d650c --- /dev/null +++ b/app/Listeners/BatchStockWarning.php @@ -0,0 +1,49 @@ +stockWarning) { + return; + } + $warningIds = $normalIds = $downIds = []; + foreach ($event->goodsSkus as $goodsSku) { + if (0 >= $goodsSku['stock']) { + $downIds[] = $goodsSku['id']; + } elseif (5 < $goodsSku['stock']) { + $normalIds[] = $goodsSku['id']; + } else { + $warningIds[] = $goodsSku['id']; + } + } + if ($warningIds) { + GoodsSku::query()->whereIn('id', $warningIds)->update(['status' => 2]); + } + if ($normalIds) { + GoodsSku::query()->whereIn('id', $normalIds)->update(['status' => 1]); + } + if ($downIds) { + GoodsSku::query()->whereIn('id', $downIds)->update(['status' => 0]); + } + } +} diff --git a/app/Listeners/CombinationGoodsStockUpdateListener.php b/app/Listeners/CombinationGoodsStockUpdateListener.php index f77a8f8..10f7c9b 100644 --- a/app/Listeners/CombinationGoodsStockUpdateListener.php +++ b/app/Listeners/CombinationGoodsStockUpdateListener.php @@ -6,7 +6,7 @@ use App\Models\CombinationGood; use App\Models\GoodsSku; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; -use App\Events\StockUpdateEvent; +use App\Events\BatchStockUpdateEvent; class CombinationGoodsStockUpdateListener { @@ -28,18 +28,18 @@ class CombinationGoodsStockUpdateListener */ public function handle($event) { - if ($event->combinationGoodsUpdate) { - return false; + if (!$event->combinationGoodsUpdate) { + return; } $updateIds = $combinationGoodsIds = $combinationGoodsItemIds = []; - if ($event->goodsSku) { + if (!empty($event->goodsSku)) { if ($event->goodsSku->is_combination) { $combinationGoodsIds[] = $event->goodsSku->id; } else { $combinationGoodsItemIds[] = $event->goodsSku->id; } } - if ($event->goodsSkus) { + if (!empty($event->goodsSkus)) { foreach ($event->goodsSkus as $sku) { if ($sku->is_combination) { $combinationGoodsIds[] = $sku->id; @@ -68,7 +68,7 @@ class CombinationGoodsStockUpdateListener ->pluck('goods_sku_id') ->toArray(); if (empty($goodsSkuIds)) { - return false; + return; } foreach ($goodsSkuIds as $goodsSkuId) { $combinationGoods = CombinationGood::query() @@ -86,7 +86,8 @@ class CombinationGoodsStockUpdateListener } if ($updateIds) { $updateIds = array_unique($updateIds); - event(new StockUpdateEvent($updateIds, 1, true)); + // 批量更新 + event(new BatchStockUpdateEvent($updateIds, false)); } } } diff --git a/app/Listeners/StockUpdateListener.php b/app/Listeners/StockUpdateListener.php index ecd1ea4..ab658c3 100644 --- a/app/Listeners/StockUpdateListener.php +++ b/app/Listeners/StockUpdateListener.php @@ -3,7 +3,6 @@ namespace App\Listeners; use App\Events\StockUpdateEvent; -use App\Jobs\BusinessGoodsSkuIncrQuantity; use App\Models\BusinessGoodsSku; use App\Models\Shop; use Illuminate\Contracts\Queue\ShouldQueue; @@ -35,40 +34,14 @@ class StockUpdateListener return; } foreach ($shops as $shop) { - if (isset($event->goodsSku)) { - $num = $event->goodsSku->stock; - $businessGoodsSkus = BusinessGoodsSku::query() - ->select(['goods_id', 'sku_id', 'external_sku_id']) - ->where('shop_id', $shop->id) - ->where('is_sync', 1) - ->where('external_sku_id', $event->goodsSku->external_sku_id) - ->get(); - 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) { - $num = $goodsSku->stock; - $businessGoodsSkus = BusinessGoodsSku::query() - ->select(['goods_id', 'sku_id', 'external_sku_id']) - ->where('shop_id', $shop->id) - ->where('is_sync', 1) - ->where('external_sku_id', $goodsSku->external_sku_id) - ->get(); - 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); - } - } - } - } + $num = $event->goodsSku->stock; + $businessGoodsSkus = BusinessGoodsSku::query() + ->select(['goods_id', 'sku_id', 'external_sku_id']) + ->where('shop_id', $shop->id) + ->where('is_sync', 1) + ->where('external_sku_id', $event->goodsSku->external_sku_id) + ->get(); + BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false); } } } diff --git a/app/Listeners/StockWarning.php b/app/Listeners/StockWarning.php index 3d5d52a..4cf2095 100644 --- a/app/Listeners/StockWarning.php +++ b/app/Listeners/StockWarning.php @@ -22,36 +22,13 @@ class StockWarning implements ShouldQueue public function handle($event) { - if (isset($event->goodsSku)) { - $event->goodsSku->status = 2; - if (0 >= $event->goodsSku->stock) { - $event->goodsSku->status = 0; - } - if (5 < $event->goodsSku->stock) { - $event->goodsSku->status = 1; - } - $event->goodsSku->save(); + $event->goodsSku->status = 2; + if (0 >= $event->goodsSku->stock) { + $event->goodsSku->status = 0; } - if (isset($event->goodsSkus)) { - $warningIds = $normalIds = $downIds = []; - foreach ($event->goodsSkus as $goodsSku) { - if (0 >= $goodsSku['stock']) { - $downIds[] = $goodsSku['id']; - } elseif (5 < $goodsSku['stock']) { - $normalIds[] = $goodsSku['id']; - } else { - $warningIds[] = $goodsSku['id']; - } - } - if ($warningIds) { - GoodsSku::query()->whereIn('id', $warningIds)->update(['status' => 2]); - } - if ($normalIds) { - GoodsSku::query()->whereIn('id', $normalIds)->update(['status' => 1]); - } - if ($downIds) { - GoodsSku::query()->whereIn('id', $downIds)->update(['status' => 0]); - } + if (5 < $event->goodsSku->stock) { + $event->goodsSku->status = 1; } + $event->goodsSku->save(); } } diff --git a/app/Listeners/UpdateBusinessGoodsStock.php b/app/Listeners/UpdateBusinessGoodsStock.php index a8f436f..b4eb240 100644 --- a/app/Listeners/UpdateBusinessGoodsStock.php +++ b/app/Listeners/UpdateBusinessGoodsStock.php @@ -29,7 +29,7 @@ class UpdateBusinessGoodsStock implements ShouldQueue * Handle the event. * * @param BusinessOrdersUpdate $event - * @return void + * @return false|void */ public function handle(BusinessOrdersUpdate $event) { @@ -41,17 +41,19 @@ class UpdateBusinessGoodsStock implements ShouldQueue $log->target_id = $event->goodsSku->id ?? 0; $log->target_field = 'stock'; $log->user_id = 999; - $log->message = '未找到' . json_encode($event->businessGoods, 256); + $log->message = '未找到' . json_encode($event->businessGoodSku, 256); $log->save(); return false; } + $builder = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP]); // 非订单影响库存变更,只更新本店铺下商品 - if (!isset($event->businessGoods['business_order_id'])) { - $builder->where('id', $event->businessGoods['shop_id']); + if (!isset($event->businessGoodSku['business_order_id'])) { + $builder->where('id', $event->businessGoodSku['shop_id']); } $shops = $builder->get(['id', 'plat_id']); + if (empty($shops)) { LogFile::info('可操作店铺为空'); return false; @@ -59,7 +61,7 @@ class UpdateBusinessGoodsStock implements ShouldQueue foreach ($shops as $shop) { $num = $event->goodsSku->stock; - $businessGoodsSkus = BusinessGoodsSku::query()->where('shop_id', $shop->id)->where('is_sync', 1)->where('external_sku_id', $event->businessGoods['external_sku_id'])->get(); + $businessGoodsSkus = BusinessGoodsSku::query()->where('shop_id', $shop->id)->where('is_sync', 1)->where('external_sku_id', $event->businessGoodSku['external_sku_id'])->get(); foreach ($businessGoodsSkus as $businessGoodsSku) { BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false); } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 4a0b710..97a1b5a 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,9 +2,13 @@ namespace App\Providers; +use App\Events\BatchImportStockEvent; use App\Events\BusinessOrdersUpdate; -use App\Events\GroupSetEvent; use App\Events\StockUpdateEvent; +use App\Events\GroupSetEvent; +use App\Events\BatchStockUpdateEvent; +use App\Listeners\BatchStockUpdateListener; +use App\Listeners\BatchStockWarning; use App\Listeners\CreateLogisticListener; use App\Listeners\GroupQueryListener; use App\Listeners\StockUpdateListener; @@ -29,6 +33,11 @@ class EventServiceProvider extends ServiceProvider StockWarning::class, CombinationGoodsStockUpdateListener::class, ], + BatchStockUpdateEvent::class => [ + BatchStockUpdateListener::class, + BatchStockWarning::class, + CombinationGoodsStockUpdateListener::class, + ], StockUpdateEvent::class => [ StockUpdateListener::class, StockWarning::class, From b242208992ec316226d8ebd33a6ec80b33b931ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Tue, 21 Nov 2023 16:27:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=BA=93=E5=AD=98=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Events/BatchStockUpdateEvent.php | 5 +- app/Events/BusinessOrdersUpdate.php | 27 +++++++++-- app/Events/StockUpdateEvent.php | 26 ++++++++++ app/Imports/InventoryImport.php | 4 +- app/Imports/NewSetImport.php | 8 ++-- app/Jobs/BusinessGoodsSkuIncrQuantity.php | 10 ++-- app/Listeners/BatchStockUpdateListener.php | 2 +- app/Listeners/BatchStockWarning.php | 20 +++++--- .../CombinationGoodsStockUpdateListener.php | 47 ++++++++++++++----- app/Listeners/StockUpdateListener.php | 2 +- app/Listeners/StockWarning.php | 34 -------------- app/Listeners/UpdateBusinessGoodsStock.php | 6 ++- app/Models/GoodsSku.php | 3 ++ app/Providers/EventServiceProvider.php | 6 --- 14 files changed, 122 insertions(+), 78 deletions(-) delete mode 100644 app/Listeners/StockWarning.php diff --git a/app/Events/BatchStockUpdateEvent.php b/app/Events/BatchStockUpdateEvent.php index 0036b31..a9f2778 100644 --- a/app/Events/BatchStockUpdateEvent.php +++ b/app/Events/BatchStockUpdateEvent.php @@ -16,20 +16,17 @@ class BatchStockUpdateEvent use Dispatchable, InteractsWithSockets, SerializesModels; public $goodsSkus; - public $stockWarning; public $combinationGoodsUpdate; /** * Create a new event instance. * * @param $goodsSkuIds - * @param bool $stockWarning * @param bool $combinationGoodsUpdate */ - public function __construct($goodsSkuIds, bool $stockWarning = true, bool $combinationGoodsUpdate = true) + public function __construct($goodsSkuIds, bool $combinationGoodsUpdate = true) { $this->goodsSkus = GoodsSku::query()->whereIn('id', $goodsSkuIds)->get(); - $this->stockWarning = $stockWarning; $this->combinationGoodsUpdate = $combinationGoodsUpdate; } diff --git a/app/Events/BusinessOrdersUpdate.php b/app/Events/BusinessOrdersUpdate.php index 3556d88..1e78768 100644 --- a/app/Events/BusinessOrdersUpdate.php +++ b/app/Events/BusinessOrdersUpdate.php @@ -2,7 +2,9 @@ namespace App\Events; +use App\Models\DailyStockRecord; use App\Models\GoodsSku; +use App\Utils\DateTimeUtils; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; @@ -38,10 +40,29 @@ class BusinessOrdersUpdate $this->goodsSku = GoodsSku::query() ->where('external_sku_id', $this->businessGoodSku['external_sku_id']) ->first(); - if ($this->goodsSku) { - $this->goodsSku->stock += $this->num; - $this->goodsSku->save(); + if (is_null($this->goodsSku)) { + return false; } + $stock = $this->goodsSku->stock + $this->num; + + if (0 >= $stock) { + $this->goodsSku->status = GoodsSku::$STATUS_DOWN; + } else { + $this->goodsSku->status = GoodsSku::$STATUS_ON_SALE; + } + + // 今日到货 + 1T 大于20,且当前剩余库存小于4时 直接下架 + $arrivedTodayNum = DailyStockRecord::query() + ->where('day', DateTimeUtils::getToday()) + ->where('sku_id', $this->goodsSku->id) + ->value('arrived_today_num'); + if (20 < $arrivedTodayNum + $this->goodsSku->yesterday_num && 4 > $stock) { + $this->goodsSku->status = GoodsSku::$STATUS_DOWN; + $stock = 0; + } + + $this->goodsSku->stock = $stock; + $this->goodsSku->save(); } /** diff --git a/app/Events/StockUpdateEvent.php b/app/Events/StockUpdateEvent.php index f29af60..b6daf06 100644 --- a/app/Events/StockUpdateEvent.php +++ b/app/Events/StockUpdateEvent.php @@ -2,6 +2,9 @@ namespace App\Events; +use App\Models\DailyStockRecord; +use App\Models\GoodsSku; +use App\Utils\DateTimeUtils; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; @@ -13,6 +16,7 @@ use Illuminate\Queue\SerializesModels; class StockUpdateEvent { use Dispatchable, InteractsWithSockets, SerializesModels; + public $goodsSku; /** @@ -23,6 +27,28 @@ class StockUpdateEvent public function __construct($goodsSku) { $this->goodsSku = $goodsSku; + $this->checkStatusAndStock($goodsSku); + } + + private function checkStatusAndStock($goodsSku) + { + $stock = $goodsSku->stock; + if (0 >= $goodsSku->stock) { + $status = GoodsSku::$STATUS_DOWN; + } else { + $status = GoodsSku::$STATUS_ON_SALE; + } + $arrivedTodayNum = DailyStockRecord::query() + ->where('day', DateTimeUtils::getToday()) + ->where('sku_id', $goodsSku->id) + ->value('arrived_today_num'); + if (20 < $arrivedTodayNum + $goodsSku->yesterday_num && 4 > $goodsSku->stock) { + $status = GoodsSku::$STATUS_DOWN; + $stock = 0; + } + $goodsSku->status = $status; + $goodsSku->stock = $stock; + $goodsSku->save(); } /** diff --git a/app/Imports/InventoryImport.php b/app/Imports/InventoryImport.php index 5d1e7aa..d2e2779 100644 --- a/app/Imports/InventoryImport.php +++ b/app/Imports/InventoryImport.php @@ -2,7 +2,7 @@ namespace App\Imports; -use App\Events\BatchImportStockEvent; +use App\Events\BatchStockUpdateEvent; use App\Jobs\SyncCostToMiaoXuan; use App\Models\DailyStockRecord; use App\Models\GoodsSku; @@ -99,6 +99,6 @@ class InventoryImport implements ToArray, SkipsEmptyRows } sleep(2); // 批量更新 - event(new BatchImportStockEvent($onSkuIds)); + event(new BatchStockUpdateEvent($onSkuIds)); } } diff --git a/app/Imports/NewSetImport.php b/app/Imports/NewSetImport.php index 37f7848..e6e65ab 100644 --- a/app/Imports/NewSetImport.php +++ b/app/Imports/NewSetImport.php @@ -2,7 +2,7 @@ namespace App\Imports; -use App\Events\BatchImportStockEvent; +use App\Events\BatchStockUpdateEvent; use App\Jobs\SyncCostToMiaoXuan; use App\Models\DailyStockRecord; use App\Models\GoodsSku; @@ -53,12 +53,14 @@ class NewSetImport implements ToArray, SkipsEmptyRows SyncCostToMiaoXuan::dispatch($row[0], $row[3]); $updateIds[] = $goodsSku['id']; // 今日到货 - $record = DailyStockRecord::query()->where('sku_id', $goodsSku['id'])->where('day', $day)->first(['id', 'arrived_today_num']); + $record = DailyStockRecord::query() + ->where('sku_id', $goodsSku['id']) + ->where('day', $day)->first(['id', 'arrived_today_num']); $record->arrived_today_num += $row[2]; $record->save(); } sleep(2); // 批量更新 - event(new BatchImportStockEvent($updateIds)); + event(new BatchStockUpdateEvent($updateIds)); } } diff --git a/app/Jobs/BusinessGoodsSkuIncrQuantity.php b/app/Jobs/BusinessGoodsSkuIncrQuantity.php index 2cef28c..3a8ed17 100644 --- a/app/Jobs/BusinessGoodsSkuIncrQuantity.php +++ b/app/Jobs/BusinessGoodsSkuIncrQuantity.php @@ -14,7 +14,7 @@ class BusinessGoodsSkuIncrQuantity implements ShouldQueue use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $shop; - public $businessGoods; + public $businessGoodsSku; public $num; public $isIncremental = true; @@ -23,10 +23,10 @@ class BusinessGoodsSkuIncrQuantity implements ShouldQueue * * @return void */ - public function __construct($shop, $businessGoods, $num, $isIncremental) + public function __construct($shop, $businessGoodsSku, $num, $isIncremental) { $this->shop = $shop; - $this->businessGoods = $businessGoods; + $this->businessGoodsSku = $businessGoodsSku; $this->num = $num; $this->isIncremental = $isIncremental; } @@ -38,8 +38,8 @@ class BusinessGoodsSkuIncrQuantity implements ShouldQueue */ public function handle() { - if ($this->businessGoods) { - BusinessFactory::init()->make($this->shop['plat_id'])->setShopWithId($this->shop['id'])->incrQuantity($this->businessGoods, $this->num, $this->isIncremental); + if ($this->businessGoodsSku) { + BusinessFactory::init()->make($this->shop['plat_id'])->setShopWithId($this->shop['id'])->incrQuantity($this->businessGoodsSku, $this->num, $this->isIncremental); } } } diff --git a/app/Listeners/BatchStockUpdateListener.php b/app/Listeners/BatchStockUpdateListener.php index 79c233b..e80ebd4 100644 --- a/app/Listeners/BatchStockUpdateListener.php +++ b/app/Listeners/BatchStockUpdateListener.php @@ -32,7 +32,7 @@ class BatchStockUpdateListener { $shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']); if (empty($shops)) { - return; + return false; } foreach ($shops as $shop) { foreach ($event->goodsSkus as $goodsSku) { diff --git a/app/Listeners/BatchStockWarning.php b/app/Listeners/BatchStockWarning.php index 64d650c..b1a5d1a 100644 --- a/app/Listeners/BatchStockWarning.php +++ b/app/Listeners/BatchStockWarning.php @@ -3,7 +3,9 @@ namespace App\Listeners; use App\Events\BatchStockUpdateEvent; +use App\Models\DailyStockRecord; use App\Models\GoodsSku; +use App\Utils\DateTimeUtils; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; @@ -26,18 +28,22 @@ class BatchStockWarning implements ShouldQueue if (!$event->stockWarning) { return; } - $warningIds = $normalIds = $downIds = []; + $dailyStockRecord = DailyStockRecord::query() + ->where('day', DateTimeUtils::getToday()) + ->whereIn('sku_id', $event->goodsSkus->pluck('id')) + ->pluck('arrived_today_num', 'sku_id') + ->toArray(); + + $normalIds = $downIds = []; foreach ($event->goodsSkus as $goodsSku) { if (0 >= $goodsSku['stock']) { $downIds[] = $goodsSku['id']; - } elseif (5 < $goodsSku['stock']) { - $normalIds[] = $goodsSku['id']; } else { - $warningIds[] = $goodsSku['id']; + $normalIds[] = $goodsSku['id']; + } + if (20 < $dailyStockRecord[$goodsSku['id']] + $goodsSku->yesterday_num && 4 > $goodsSku['stock']) { + $downIds[] = $goodsSku['id']; } - } - if ($warningIds) { - GoodsSku::query()->whereIn('id', $warningIds)->update(['status' => 2]); } if ($normalIds) { GoodsSku::query()->whereIn('id', $normalIds)->update(['status' => 1]); diff --git a/app/Listeners/CombinationGoodsStockUpdateListener.php b/app/Listeners/CombinationGoodsStockUpdateListener.php index 10f7c9b..8456450 100644 --- a/app/Listeners/CombinationGoodsStockUpdateListener.php +++ b/app/Listeners/CombinationGoodsStockUpdateListener.php @@ -3,7 +3,9 @@ namespace App\Listeners; use App\Models\CombinationGood; +use App\Models\DailyStockRecord; use App\Models\GoodsSku; +use App\Utils\DateTimeUtils; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use App\Events\BatchStockUpdateEvent; @@ -29,7 +31,7 @@ class CombinationGoodsStockUpdateListener public function handle($event) { if (!$event->combinationGoodsUpdate) { - return; + return false; } $updateIds = $combinationGoodsIds = $combinationGoodsItemIds = []; if (!empty($event->goodsSku)) { @@ -39,6 +41,7 @@ class CombinationGoodsStockUpdateListener $combinationGoodsItemIds[] = $event->goodsSku->id; } } + if (!empty($event->goodsSkus)) { foreach ($event->goodsSkus as $sku) { if ($sku->is_combination) { @@ -55,21 +58,20 @@ class CombinationGoodsStockUpdateListener ->whereIn('goods_sku_id', $combinationGoodsIds) ->get(); foreach ($combinationGoods as $item) { - $sku = GoodsSku::query()->find($item['item_id']); - $sku->stock -= $item['item_num']; - $sku->save(); - $updateIds[] = $sku->id; + $goodsSku = GoodsSku::query()->find($item['item_id']); + $stock = $goodsSku->stock - $item['item_num']; + [$status, $stock] = $this->checkStatusAndStock($goodsSku, $stock); + $goodsSku->status = $status; + $goodsSku->stock = $stock; + $goodsSku->save(); + $updateIds[] = $goodsSku->id; } } // 计算主商品库存 if ($combinationGoodsItemIds) { $goodsSkuIds = CombinationGood::query() ->whereIn('item_id', $combinationGoodsItemIds) - ->pluck('goods_sku_id') - ->toArray(); - if (empty($goodsSkuIds)) { - return; - } + ->pluck('goods_sku_id'); foreach ($goodsSkuIds as $goodsSkuId) { $combinationGoods = CombinationGood::query() ->with('goodsSkuItem:id,stock') @@ -80,7 +82,11 @@ class CombinationGoodsStockUpdateListener $stock[] = (int)($goods['goodsSkuItem']['stock'] / $goods['item_num']); } $stock = min($stock); - GoodsSku::query()->where('id', $goodsSkuId)->update(['stock' => $stock]); + $goodsSku = GoodsSku::query()->find($goodsSkuId); + [$status, $stock] = $this->checkStatusAndStock($goodsSku, $stock); + $goodsSku->status = $status; + $goodsSku->stock = $stock; + $goodsSku->save(); $updateIds[] = $goodsSkuId; } } @@ -90,4 +96,23 @@ class CombinationGoodsStockUpdateListener event(new BatchStockUpdateEvent($updateIds, false)); } } + + private function checkStatusAndStock($goodsSku, $stock) + { + if (0 >= $stock) { + $status = GoodsSku::$STATUS_DOWN; + } else { + $status = GoodsSku::$STATUS_ON_SALE; + } + $arrivedTodayNum = DailyStockRecord::query() + ->where('day', DateTimeUtils::getToday()) + ->where('sku_id', $goodsSku->id) + ->value('arrived_today_num'); + if (20 < $arrivedTodayNum + $goodsSku->yesterday_num && 4 > $stock) { + $status = GoodsSku::$STATUS_DOWN; + $stock = 0; + } + + return [$status, $stock]; + } } diff --git a/app/Listeners/StockUpdateListener.php b/app/Listeners/StockUpdateListener.php index ab658c3..4908e78 100644 --- a/app/Listeners/StockUpdateListener.php +++ b/app/Listeners/StockUpdateListener.php @@ -31,7 +31,7 @@ class StockUpdateListener { $shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']); if (empty($shops)) { - return; + return false; } foreach ($shops as $shop) { $num = $event->goodsSku->stock; diff --git a/app/Listeners/StockWarning.php b/app/Listeners/StockWarning.php deleted file mode 100644 index 4cf2095..0000000 --- a/app/Listeners/StockWarning.php +++ /dev/null @@ -1,34 +0,0 @@ -goodsSku->status = 2; - if (0 >= $event->goodsSku->stock) { - $event->goodsSku->status = 0; - } - if (5 < $event->goodsSku->stock) { - $event->goodsSku->status = 1; - } - $event->goodsSku->save(); - } -} diff --git a/app/Listeners/UpdateBusinessGoodsStock.php b/app/Listeners/UpdateBusinessGoodsStock.php index b4eb240..0e24f71 100644 --- a/app/Listeners/UpdateBusinessGoodsStock.php +++ b/app/Listeners/UpdateBusinessGoodsStock.php @@ -61,7 +61,11 @@ class UpdateBusinessGoodsStock implements ShouldQueue foreach ($shops as $shop) { $num = $event->goodsSku->stock; - $businessGoodsSkus = BusinessGoodsSku::query()->where('shop_id', $shop->id)->where('is_sync', 1)->where('external_sku_id', $event->businessGoodSku['external_sku_id'])->get(); + $businessGoodsSkus = BusinessGoodsSku::query() + ->where('shop_id', $shop->id) + ->where('is_sync', 1) + ->where('external_sku_id', $event->businessGoodSku['external_sku_id']) + ->get(); foreach ($businessGoodsSkus as $businessGoodsSku) { BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false); } diff --git a/app/Models/GoodsSku.php b/app/Models/GoodsSku.php index 74c1383..a846f43 100644 --- a/app/Models/GoodsSku.php +++ b/app/Models/GoodsSku.php @@ -36,6 +36,9 @@ class GoodsSku extends Model protected $hidden = ['created_at']; + public static $STATUS_ON_SALE = 1; + public static $STATUS_DOWN = 0; + /** * 获取状态 * diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 97a1b5a..dceb5f5 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,17 +2,14 @@ namespace App\Providers; -use App\Events\BatchImportStockEvent; use App\Events\BusinessOrdersUpdate; use App\Events\StockUpdateEvent; use App\Events\GroupSetEvent; use App\Events\BatchStockUpdateEvent; use App\Listeners\BatchStockUpdateListener; -use App\Listeners\BatchStockWarning; use App\Listeners\CreateLogisticListener; use App\Listeners\GroupQueryListener; use App\Listeners\StockUpdateListener; -use App\Listeners\StockWarning; use App\Listeners\CombinationGoodsStockUpdateListener; use App\Listeners\UpdateBusinessGoodsStock; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -30,17 +27,14 @@ class EventServiceProvider extends ServiceProvider protected $listen = [ BusinessOrdersUpdate::class => [ UpdateBusinessGoodsStock::class, - StockWarning::class, CombinationGoodsStockUpdateListener::class, ], BatchStockUpdateEvent::class => [ BatchStockUpdateListener::class, - BatchStockWarning::class, CombinationGoodsStockUpdateListener::class, ], StockUpdateEvent::class => [ StockUpdateListener::class, - StockWarning::class, CombinationGoodsStockUpdateListener::class, ], GroupSetEvent::class => [ From 7dc861db0dd23d1c83185d7b9158168b3477abee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Tue, 21 Nov 2023 16:33:34 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=BA=93=E5=AD=98=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Events/StockUpdateEvent.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Events/StockUpdateEvent.php b/app/Events/StockUpdateEvent.php index b6daf06..ca84859 100644 --- a/app/Events/StockUpdateEvent.php +++ b/app/Events/StockUpdateEvent.php @@ -18,6 +18,7 @@ class StockUpdateEvent use Dispatchable, InteractsWithSockets, SerializesModels; public $goodsSku; + public $combinationGoodsUpdate = true; /** * Create a new event instance. From c6e7f7edb65c8aadb2563eff8a0d08b7cdb7d932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Tue, 21 Nov 2023 16:35:57 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=BA=93=E5=AD=98=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Listeners/BatchStockWarning.php | 55 ----------------------------- 1 file changed, 55 deletions(-) delete mode 100644 app/Listeners/BatchStockWarning.php diff --git a/app/Listeners/BatchStockWarning.php b/app/Listeners/BatchStockWarning.php deleted file mode 100644 index b1a5d1a..0000000 --- a/app/Listeners/BatchStockWarning.php +++ /dev/null @@ -1,55 +0,0 @@ -stockWarning) { - return; - } - $dailyStockRecord = DailyStockRecord::query() - ->where('day', DateTimeUtils::getToday()) - ->whereIn('sku_id', $event->goodsSkus->pluck('id')) - ->pluck('arrived_today_num', 'sku_id') - ->toArray(); - - $normalIds = $downIds = []; - foreach ($event->goodsSkus as $goodsSku) { - if (0 >= $goodsSku['stock']) { - $downIds[] = $goodsSku['id']; - } else { - $normalIds[] = $goodsSku['id']; - } - if (20 < $dailyStockRecord[$goodsSku['id']] + $goodsSku->yesterday_num && 4 > $goodsSku['stock']) { - $downIds[] = $goodsSku['id']; - } - } - if ($normalIds) { - GoodsSku::query()->whereIn('id', $normalIds)->update(['status' => 1]); - } - if ($downIds) { - GoodsSku::query()->whereIn('id', $downIds)->update(['status' => 0]); - } - } -}