erp/app/Listeners/BatchStockWarning.php

50 lines
1.2 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\GoodsSku;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class BatchStockWarning implements ShouldQueue
{
use InteractsWithQueue;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
public function handle(BatchStockUpdateEvent $event)
{
if (!$event->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]);
}
}
}