commit
0f9d4e0d65
@ -36,7 +36,7 @@ class CombinationGoodsStockUpdateListener
|
|||||||
if ($event->goodsSku->is_combination) {
|
if ($event->goodsSku->is_combination) {
|
||||||
$combinationGoodsIds[] = $event->goodsSku->id;
|
$combinationGoodsIds[] = $event->goodsSku->id;
|
||||||
} else {
|
} else {
|
||||||
$combinationGoodsItemIds[$event->goodsSku->id] = $event->goodsSku->stock;
|
$combinationGoodsItemIds[] = $event->goodsSku->id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($event->goodsSkus) {
|
if ($event->goodsSkus) {
|
||||||
@ -44,7 +44,7 @@ class CombinationGoodsStockUpdateListener
|
|||||||
if ($sku->is_combination) {
|
if ($sku->is_combination) {
|
||||||
$combinationGoodsIds[] = $sku->id;
|
$combinationGoodsIds[] = $sku->id;
|
||||||
} else {
|
} else {
|
||||||
$combinationGoodsItemIds[$sku->id] = $sku->stock;
|
$combinationGoodsItemIds[] = $sku->id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,23 +63,31 @@ class CombinationGoodsStockUpdateListener
|
|||||||
}
|
}
|
||||||
// 计算主商品库存
|
// 计算主商品库存
|
||||||
if ($combinationGoodsItemIds) {
|
if ($combinationGoodsItemIds) {
|
||||||
$combinationGoodsItems = CombinationGood::query()
|
$goodsSkuIds = CombinationGood::query()
|
||||||
->whereIn('item_id', array_keys($combinationGoodsItemIds))
|
->whereIn('item_id', $combinationGoodsItemIds)
|
||||||
->get();
|
->pluck('goods_sku_id')
|
||||||
if ($combinationGoodsItems->isEmpty()){
|
->toArray();
|
||||||
|
if (empty($goodsSkuIds)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$goodsSkuIds = array_column($combinationGoodsItems->toArray(), 'goods_sku_id');
|
foreach ($goodsSkuIds as $goodsSkuId) {
|
||||||
$goodsSkus = GoodsSku::query()
|
$combinationGoods = CombinationGood::query()
|
||||||
->whereIn('id', $goodsSkuIds)
|
->with('goodsSkuItem:id,stock')
|
||||||
->pluck('stock', 'id')
|
->where('goods_sku_id', $goodsSkuId)
|
||||||
->toArray();
|
->get();
|
||||||
foreach ($combinationGoodsItems as $item){
|
$stock = 0;
|
||||||
$stock = $combinationGoodsItemIds[$item['item_id']] / $item['item_num'];
|
foreach ($combinationGoods as $goods) {
|
||||||
if ($stock < $goodsSkus[$item['goods_sku_id']]) {
|
$num = $goods['goodsSkuItem']['stock'] / $goods['item_num'];
|
||||||
GoodsSku::query()->where('id', $item['goods_sku_id'])->update(['stock' => $stock]);
|
if (0 === $stock) {
|
||||||
$updateIds[] = $item['goods_sku_id'];
|
$stock = $num;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
if ($num < $stock) {
|
||||||
|
$stock = $num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GoodsSku::query()->where('id', $goodsSkuId)->update(['stock' => $stock]);
|
||||||
|
$updateIds[] = $goodsSkuId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($updateIds) {
|
if ($updateIds) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user