2023-04-21 19:43:17 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
|
|
|
|
|
use App\Models\CombinationGood;
|
|
|
|
|
use App\Models\GoodsSku;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use App\Events\StockUpdateEvent;
|
|
|
|
|
|
|
|
|
|
class CombinationGoodsStockUpdateListener
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Create the event listener.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the event.
|
|
|
|
|
*
|
|
|
|
|
* @param $event
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function handle($event)
|
|
|
|
|
{
|
|
|
|
|
if ($event->combinationGoodsUpdate) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$updateIds = $combinationGoodsIds = $combinationGoodsItemIds = [];
|
|
|
|
|
if ($event->goodsSku) {
|
|
|
|
|
if ($event->goodsSku->is_combination) {
|
|
|
|
|
$combinationGoodsIds[] = $event->goodsSku->id;
|
|
|
|
|
} else {
|
|
|
|
|
$combinationGoodsItemIds[$event->goodsSku->id] = $event->goodsSku->stock;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($event->goodsSkus) {
|
|
|
|
|
foreach ($event->goodsSkus as $sku) {
|
|
|
|
|
if ($sku->is_combination) {
|
|
|
|
|
$combinationGoodsIds[] = $sku->id;
|
|
|
|
|
} else {
|
|
|
|
|
$combinationGoodsItemIds[$sku->id] = $sku->stock;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 减子商品库存
|
|
|
|
|
if ($combinationGoodsIds) {
|
|
|
|
|
$combinationGoods = CombinationGood::query()
|
|
|
|
|
->with('goodsSku:id,stock')
|
|
|
|
|
->whereIn('goods_sku_id', $combinationGoodsIds)
|
|
|
|
|
->get();
|
|
|
|
|
foreach ($combinationGoods as $item) {
|
|
|
|
|
$sku = GoodsSku::query()->find($item['item_id']);
|
|
|
|
|
$sku->stock -= $item['item_num'];
|
|
|
|
|
$sku->save();
|
|
|
|
|
$updateIds[] = $sku->id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 计算主商品库存
|
|
|
|
|
if ($combinationGoodsItemIds) {
|
2023-04-26 10:48:53 +08:00
|
|
|
$combinationGoodsItems = CombinationGood::query()
|
|
|
|
|
->whereIn('item_id', array_keys($combinationGoodsItemIds))
|
|
|
|
|
->get();
|
|
|
|
|
if ($combinationGoodsItems->isEmpty()){
|
2023-04-21 19:43:17 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-26 10:48:53 +08:00
|
|
|
$goodsSkuIds = array_column($combinationGoodsItems->toArray(), 'goods_sku_id');
|
2023-04-21 19:43:17 +08:00
|
|
|
$goodsSkus = GoodsSku::query()
|
|
|
|
|
->whereIn('id', $goodsSkuIds)
|
|
|
|
|
->pluck('stock', 'id')
|
|
|
|
|
->toArray();
|
2023-04-26 10:48:53 +08:00
|
|
|
foreach ($combinationGoodsItems as $item){
|
|
|
|
|
$stock = $combinationGoodsItemIds[$item['item_id']] / $item['item_num'];
|
|
|
|
|
if ($stock < $goodsSkus[$item['goods_sku_id']]) {
|
|
|
|
|
GoodsSku::query()->where('id', $item['goods_sku_id'])->update(['stock' => $stock]);
|
|
|
|
|
$updateIds[] = $item['goods_sku_id'];
|
2023-04-21 19:43:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($updateIds) {
|
2023-04-26 10:51:15 +08:00
|
|
|
$updateIds = array_unique($updateIds);
|
2023-04-21 19:43:17 +08:00
|
|
|
event(new StockUpdateEvent($updateIds, 1, true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|