2024-07-27 16:51:00 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
|
|
|
|
|
use App\Events\BatchStockUpdateEvent;
|
|
|
|
|
use App\Events\BusinessOrdersUpdate;
|
|
|
|
|
use App\Http\Enum\CacheKeyEnum;
|
|
|
|
|
use App\Http\Enum\DevConfigKeyEnum;
|
|
|
|
|
use App\Http\Service\MessageService;
|
|
|
|
|
use App\Models\BusinessGoodsSku;
|
|
|
|
|
use App\Models\DailyStockRecord;
|
|
|
|
|
use App\Models\DeveloperConfig;
|
|
|
|
|
use App\Models\Shop;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use App\Services\Business\BusinessFactory;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-08-02 16:40:57 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-07-27 16:51:00 +08:00
|
|
|
|
|
|
|
|
class BusinessOrderUpdateListener implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use InteractsWithQueue;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create the event listener.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the event.
|
|
|
|
|
*
|
|
|
|
|
* @param BusinessOrdersUpdate $event
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function handle(BusinessOrdersUpdate $event)
|
|
|
|
|
{
|
2024-08-02 16:40:57 +08:00
|
|
|
try {
|
2024-08-06 13:53:22 +08:00
|
|
|
|
|
|
|
|
if(!empty($event->goodsSku)){
|
|
|
|
|
//查询库存是否满足告警规则
|
|
|
|
|
$skuId = $event->goodsSku->id;
|
|
|
|
|
$nowTime = Carbon::now()->toDateTimeString();
|
|
|
|
|
//查找最后一次盘点数据
|
|
|
|
|
$dailyStockRecord = DailyStockRecord::query()->where("sku_id", '=', $skuId)->
|
|
|
|
|
where("inventory_time", '<', $nowTime)->orderByDesc('id')->first();
|
|
|
|
|
|
|
|
|
|
$inventory = $dailyStockRecord['inventory'] ?? 0;
|
|
|
|
|
$expireTime = Carbon::now()->addMinutes(30)->toDateTimeString();
|
|
|
|
|
$proportion = Cache::remember(CacheKeyEnum::STOCK_RULE_PROPORTION, $expireTime, function () {
|
|
|
|
|
$developerConfig = DeveloperConfig::query()->where("key", "=", DevConfigKeyEnum::STOCK_RULE_PROPORTION)->first();
|
|
|
|
|
return $developerConfig['value'] ?? DevConfigKeyEnum::DEFAULT_STOCK_RULE_PROPORTION;
|
|
|
|
|
});
|
|
|
|
|
//库存比例小于最近盘点多少告警 一天也只是告警一次
|
|
|
|
|
if ($inventory > 10 && $inventory * $proportion > $event->goodsSku->stock) {
|
|
|
|
|
$messageService = new MessageService();
|
|
|
|
|
$messageService->createLowerStockNoticeMessage($inventory, $event->goodsSku->toArray());
|
|
|
|
|
}
|
2024-08-02 16:40:57 +08:00
|
|
|
}
|
|
|
|
|
}catch(\Exception $exception) {
|
|
|
|
|
Log::error("库存告警发生异常",["error"=>$exception->getMessage()]);
|
2024-07-27 16:51:00 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|