2023-11-18 14:35:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
|
|
|
|
|
use App\Events\BatchStockUpdateEvent;
|
2023-11-21 16:27:14 +08:00
|
|
|
use App\Models\DailyStockRecord;
|
2023-11-18 14:35:19 +08:00
|
|
|
use App\Models\GoodsSku;
|
2023-11-21 16:27:14 +08:00
|
|
|
use App\Utils\DateTimeUtils;
|
2023-11-18 14:35:19 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2023-11-21 16:27:14 +08:00
|
|
|
$dailyStockRecord = DailyStockRecord::query()
|
|
|
|
|
->where('day', DateTimeUtils::getToday())
|
|
|
|
|
->whereIn('sku_id', $event->goodsSkus->pluck('id'))
|
|
|
|
|
->pluck('arrived_today_num', 'sku_id')
|
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
|
|
$normalIds = $downIds = [];
|
2023-11-18 14:35:19 +08:00
|
|
|
foreach ($event->goodsSkus as $goodsSku) {
|
|
|
|
|
if (0 >= $goodsSku['stock']) {
|
|
|
|
|
$downIds[] = $goodsSku['id'];
|
|
|
|
|
} else {
|
2023-11-21 16:27:14 +08:00
|
|
|
$normalIds[] = $goodsSku['id'];
|
|
|
|
|
}
|
|
|
|
|
if (20 < $dailyStockRecord[$goodsSku['id']] + $goodsSku->yesterday_num && 4 > $goodsSku['stock']) {
|
|
|
|
|
$downIds[] = $goodsSku['id'];
|
2023-11-18 14:35:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($normalIds) {
|
|
|
|
|
GoodsSku::query()->whereIn('id', $normalIds)->update(['status' => 1]);
|
|
|
|
|
}
|
|
|
|
|
if ($downIds) {
|
|
|
|
|
GoodsSku::query()->whereIn('id', $downIds)->update(['status' => 0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|