6
0
Fork 0

创建订单后增加商品销量

release
李静 2021-12-29 09:50:47 +08:00
parent 8b3de12160
commit 682a105b91
2 changed files with 12 additions and 9 deletions

View File

@ -173,6 +173,14 @@ class Order extends Model
return $this->morphMany(PayLog::class, 'payable'); return $this->morphMany(PayLog::class, 'payable');
} }
/**
* 属于此订单的售后单
*/
public function afterSales()
{
return $this->hasMany(AfterSale::class, 'order_id');
}
/** /**
* 此订单是否待付款 * 此订单是否待付款
* *
@ -253,14 +261,6 @@ class Order extends Model
*/ */
public function markAsCompleted() public function markAsCompleted()
{ {
// 加载订单商品
$this->loadMissing('products');
foreach ($this->products->loadMissing(['spu', 'sku']) as $product) {
$product->spu?->increment('sales', $product->quantity);
$product->sku?->increment('sales', $product->quantity);
}
$this->update([ $this->update([
'status' => static::STATUS_COMPLETED, 'status' => static::STATUS_COMPLETED,
'completed_at' => now(), 'completed_at' => now(),

View File

@ -302,9 +302,12 @@ class OrderService
{ {
// 扣商品库存 // 扣商品库存
$sku->update([ $sku->update([
'sales' => DB::Raw("sales + {$qty}"),
'stock' => DB::raw("stock - {$qty}"), // 库存 'stock' => DB::raw("stock - {$qty}"), // 库存
]); ]);
$sku->spu?->increment('sales', $qty);
// 如果是因为赠品库存不足引起的异常,则需重试 // 如果是因为赠品库存不足引起的异常,则需重试
do { do {
try { try {
@ -705,7 +708,7 @@ class OrderService
throw new BizException('购物车商品已丢失'); throw new BizException('购物车商品已丢失');
} }
$shoppingCartItems->load('sku'); $shoppingCartItems->load('sku.spu');
$lostShoppingCartItems = $shoppingCartItems->filter(function ($item) { $lostShoppingCartItems = $shoppingCartItems->filter(function ($item) {
return $item->sku === null; return $item->sku === null;