erp/app/Events/BusinessOrdersUpdate.php

61 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Events;
use App\Models\GoodsSku;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
2022-08-30 23:11:48 +08:00
use Illuminate\Support\Facades\Log;
class BusinessOrdersUpdate
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $num;
2022-09-15 17:36:36 +08:00
public $businessGoods;
public $goodsSku;
/**
* Create a new event instance.
*
* @return void
*/
2022-09-15 17:36:36 +08:00
public function __construct($item, $num)
{
2023-04-21 17:53:35 +08:00
$this->businessGoods = $item->toArray();
$this->num = $num;
$this->updateStock();
}
private function updateStock()
{
2022-08-19 10:54:13 +08:00
try {
2023-04-17 18:56:59 +08:00
$this->goodsSku = GoodsSku::query()
->where('external_sku_id', $this->businessGoods['external_sku_id'])
2022-08-19 10:54:13 +08:00
->first();
} catch (\Exception $e) {
2022-08-30 23:11:48 +08:00
Log::error('事件库存更新失败: ' . $e->getMessage());
2022-08-19 10:54:13 +08:00
return false;
}
if ($this->goodsSku) {
2022-08-22 19:55:21 +08:00
$this->goodsSku->stock += $this->num;
$this->goodsSku->save();
}
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}