feat: #10000 更新库存增加事件监听和任务队列
This commit is contained in:
parent
804162083b
commit
1a22edfd05
58
app/Events/BusinessOrdersUpdate.php
Normal file
58
app/Events/BusinessOrdersUpdate.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\BusinessOrderItem;
|
||||
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;
|
||||
|
||||
class BusinessOrdersUpdate
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $num;
|
||||
public $businessOrderItem;
|
||||
public $goodsSku;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(BusinessOrderItem $item, $num)
|
||||
{
|
||||
$this->businessOrderItem = $item;
|
||||
$this->num = $num;
|
||||
$this->updateStock();
|
||||
}
|
||||
|
||||
private function updateStock()
|
||||
{
|
||||
[$goodsCode, $skuCode] = explode('_', $this->businessOrderItem->external_sku_id);
|
||||
$this->goodsSku = GoodsSku::query()->where('sku_code', $skuCode)
|
||||
->with(['goods' => function ($query) use ($goodsCode) {
|
||||
$query->where('goods_code', $goodsCode);
|
||||
}])
|
||||
->first();
|
||||
if ($this->goodsSku) {
|
||||
$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');
|
||||
}
|
||||
}
|
||||
@ -3,8 +3,6 @@
|
||||
namespace App\Http\Controllers\Shop;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Listeners\BindBusinessGoods;
|
||||
use App\Listeners\UpdateBusinessGoodsStock;
|
||||
use App\Models\Shop;
|
||||
use App\Http\Resources\ShopsResource;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
45
app/Jobs/BusinessGoodsSkuIncrQuantity.php
Normal file
45
app/Jobs/BusinessGoodsSkuIncrQuantity.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\BusinessOrderItem;
|
||||
use App\Models\GoodsSku;
|
||||
use App\Services\Business\BusinessFactory;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class BusinessGoodsSkuIncrQuantity implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $shop;
|
||||
public $businessOrderItem;
|
||||
public $num;
|
||||
public $goodsSku;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($shop, BusinessOrderItem $businessOrderItem, $num, GoodsSku $goodsSku)
|
||||
{
|
||||
$this->shop = $shop;
|
||||
$this->businessOrderItem = $businessOrderItem;
|
||||
$this->num = $num;
|
||||
$this->goodsSku = $goodsSku;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
BusinessFactory::init()->make($this->shop['plat_id'])->setShopId($this->shop['id'])->incrQuantity($this->businessOrderItem, $this->num, true, $this->goodsSku);
|
||||
}
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ProcessPodcast implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@ -2,12 +2,14 @@
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use App\Events\BusinessOrdersUpdate;
|
||||
|
||||
class SendDatabaseNotification
|
||||
class SendDatabaseNotification implements ShouldQueue
|
||||
{
|
||||
use InteractsWithQueue;
|
||||
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
@ -21,11 +23,13 @@ class SendDatabaseNotification
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Registered $event
|
||||
* @param BusinessOrdersUpdate $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Registered $event)
|
||||
public function handle(BusinessOrdersUpdate $event)
|
||||
{
|
||||
//
|
||||
if (5 >= $event->goodsSku->stock) {
|
||||
// 发送通知给管理员
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,65 +2,55 @@
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Models\GoodsSku;
|
||||
use App\Models\Log;
|
||||
use App\Models\Shop;
|
||||
use App\Services\Business\BusinessFactory;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use App\Models\BusinessOrderItem;
|
||||
use App\Events\BusinessOrdersUpdate;
|
||||
use App\Jobs\BusinessGoodsSkuIncrQuantity;
|
||||
|
||||
class UpdateBusinessGoodsStock implements ShouldQueue
|
||||
{
|
||||
protected $num;
|
||||
protected $businessOrderItem;
|
||||
use InteractsWithQueue;
|
||||
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(BusinessOrderItem $item, $num)
|
||||
public function __construct()
|
||||
{
|
||||
$this->businessOrderItem = $item;
|
||||
$this->num = $num;
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Registered $event
|
||||
* @param BusinessOrdersUpdate $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Registered $event)
|
||||
public function handle(BusinessOrdersUpdate $event)
|
||||
{
|
||||
$shops = Shop::query()->where('id', '<>', $this->businessOrderItem->shop_id)->where('status', 1)->get(['id', 'plat_id']);
|
||||
if (empty($shops)) {
|
||||
return;
|
||||
}
|
||||
[$goodsCode, $skuCode] = explode('_', $this->businessOrderItem->external_sku_id);
|
||||
$goodsSku = GoodsSku::query()->where('sku_code', $skuCode)
|
||||
->with(['goods' => function ($query) use ($goodsCode) {
|
||||
$query->where('goods_code', $goodsCode);
|
||||
}])
|
||||
->first();
|
||||
if ($goodsSku) {
|
||||
$goodsSku->stock += $this->num;
|
||||
$goodsSku->save();
|
||||
foreach ($shops as $shop) {
|
||||
BusinessFactory::init()->make($shop['plat_id'])->setShopId($shop['id'])->incrQuantity($this->businessOrderItem, $this->num, true, $goodsSku);
|
||||
}
|
||||
} else {
|
||||
if (empty($event->goodsSku)) {
|
||||
$log = new Log();
|
||||
$log->module = 'goods';
|
||||
$log->action = 'PATCH';
|
||||
$log->target_type = 'goods_sku';
|
||||
$log->target_id = $goodsSku->id;
|
||||
$log->target_id = $event->goodsSku->id;
|
||||
$log->target_field = 'stock';
|
||||
$log->user_id = $this->businessOrderItem->shop_id;
|
||||
$log->message = ($this->businessOrderItem->external_sku_id ?: '商品') . '未找到';
|
||||
$log->user_id = $event->businessOrderItem->shop_id;
|
||||
$log->message = ($event->businessOrderItem->external_sku_id ?: '商品') . '未找到';
|
||||
$log->save();
|
||||
return;
|
||||
}
|
||||
$shops = Shop::query()->where('id', '<>', $event->businessOrderItem->shop_id)->where('status', 1)->get(['id', 'plat_id']);
|
||||
if (empty($shops)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($shops as $shop) {
|
||||
BusinessGoodsSkuIncrQuantity::dispatch($shop, $event->businessOrderItem, $event->num, $event->goodsSku);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,12 +2,9 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use App\Listeners\SendDatabaseNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use App\Listeners\SendDatabaseNotification;
|
||||
use App\Listeners\BindBusinessGoods;
|
||||
use App\Listeners\UpdateBusinessGoodsStock;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
@ -18,11 +15,9 @@ class EventServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
Registered::class => [
|
||||
SendEmailVerificationNotification::class,
|
||||
SendDatabaseNotification::class,
|
||||
BindBusinessGoods::class,
|
||||
'App\Events\BusinessOrdersUpdate' => [
|
||||
UpdateBusinessGoodsStock::class,
|
||||
SendDatabaseNotification::class,
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Services\Business;
|
||||
|
||||
use App\Listeners\UpdateBusinessGoodsStock;
|
||||
use App\Events\BusinessOrdersUpdate;
|
||||
use App\Models\BusinessGoodsSku;
|
||||
use App\Models\BusinessOrder;
|
||||
use App\Models\BusinessOrderItem;
|
||||
@ -71,7 +71,7 @@ abstract class BusinessClient
|
||||
}
|
||||
// 增量更新库存
|
||||
if ($num) {
|
||||
event(new UpdateBusinessGoodsStock($orderItem, $num));
|
||||
event(new BusinessOrdersUpdate($orderItem, $num));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1106
composer.lock
generated
1106
composer.lock
generated
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user