mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-12-01 14:40:44 +00:00
89 lines
2.6 KiB
PHP
89 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Events;
|
|
|
|
use App\Models\DailyStockRecord;
|
|
use App\Models\GoodsSku;
|
|
use App\Utils\DateTimeUtils;
|
|
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;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BusinessOrdersUpdate
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
public $businessGoodSku;
|
|
public $num;
|
|
public $goodsSku;
|
|
public $type = "BusinessOrdersUpdate";
|
|
|
|
public $combinationGoodsUpdate = true;
|
|
|
|
/**
|
|
* Create a new event instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($businessGoodSku, $num)
|
|
{
|
|
try {
|
|
$this->businessGoodSku = $businessGoodSku->toArray();
|
|
$this->num = $num;
|
|
$updateResult = false;
|
|
//暂时设定重试5次
|
|
for ($i = 0; $i < 5; $i++) {
|
|
if ($this->updateStock()) {
|
|
$updateResult = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!$updateResult) {
|
|
Log::error("sku 业务更新失败", (array)$this->businessGoodSku);
|
|
}
|
|
} catch (\Exception $exception) {
|
|
Log::error("sku 业务更新发生异常", ["error" => $exception->getMessage()]);
|
|
}
|
|
}
|
|
|
|
|
|
private function updateStock()
|
|
{
|
|
$this->goodsSku = GoodsSku::query()
|
|
->where('external_sku_id', $this->businessGoodSku['external_sku_id'])
|
|
->first();
|
|
if (is_null($this->goodsSku)) {
|
|
return true;
|
|
}
|
|
$oldStock = $this->goodsSku->stock;
|
|
$stock = $this->goodsSku->stock + $this->num;
|
|
$saleStock = $this->goodsSku->sale_stock + $this->num;
|
|
$updateParam = ["stock" => $stock, "sale_stock" => $saleStock];
|
|
if (0 >= $saleStock) {
|
|
$updateParam['status'] = GoodsSku::$STATUS_DOWN;
|
|
} else {
|
|
$updateParam['status'] = GoodsSku::$STATUS_ON_SALE;
|
|
}
|
|
|
|
Log::info("sku 业务订单库存更新", $updateParam);
|
|
//乐观锁更新
|
|
return GoodsSku::query()->where('external_sku_id', $this->businessGoodSku['external_sku_id'])->
|
|
where("stock", "=", $oldStock)->update($updateParam);
|
|
}
|
|
|
|
/**
|
|
* Get the channels the event should broadcast on.
|
|
*
|
|
* @return \Illuminate\Broadcasting\Channel|array
|
|
*/
|
|
public function broadcastOn()
|
|
{
|
|
return new PrivateChannel('channel-name');
|
|
}
|
|
}
|