erp/app/Listeners/BatchStockUpdateListener.php

53 lines
1.5 KiB
PHP
Raw Normal View History

2023-11-18 14:35:19 +08:00
<?php
namespace App\Listeners;
use App\Events\BatchStockUpdateEvent;
use App\Models\BusinessGoodsSku;
use App\Models\Shop;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Services\Business\BusinessFactory;
2024-03-23 11:28:58 +08:00
class BatchStockUpdateListener implements ShouldQueue
2023-11-18 14:35:19 +08:00
{
2024-03-23 11:28:58 +08:00
use InteractsWithQueue;
2023-11-18 14:35:19 +08:00
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param BatchStockUpdateEvent $event
* @return void
*/
public function handle(BatchStockUpdateEvent $event)
{
$shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']);
if (empty($shops)) {
2024-03-23 11:28:58 +08:00
return;
2023-11-18 14:35:19 +08:00
}
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);
2024-03-23 15:05:15 +08:00
usleep(140);
2023-11-18 14:35:19 +08:00
}
}
}
}