-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e308978
commit d738d09
Showing
4 changed files
with
132 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Auth; | ||
use App\Models\CartItem; | ||
|
||
class CartService | ||
{ | ||
public function get() | ||
{ | ||
return Auth::user()->cartItems()->with(['productSku.product'])->get(); | ||
} | ||
|
||
public function add($skuId, $amount) | ||
{ | ||
$user = Auth::user(); | ||
// 从数据库中查询该商品是否已经在购物车中 | ||
if ($item = $user->cartItems()->where('product_sku_id', $skuId)->first()) { | ||
// 如果存在则直接叠加商品数量 | ||
$item->update([ | ||
'amount' => $item->amount + $amount, | ||
]); | ||
} else { | ||
// 否则创建一个新的购物车记录 | ||
$item = new CartItem(['amount' => $amount]); | ||
$item->user()->associate($user); | ||
$item->productSku()->associate($skuId); | ||
$item->save(); | ||
} | ||
|
||
return $item; | ||
} | ||
|
||
public function remove($skuIds) | ||
{ | ||
// 可以传单个 ID,也可以传 ID 数组 | ||
if (!is_array($skuIds)) { | ||
$skuIds = [$skuIds]; | ||
} | ||
Auth::user()->cartItems()->whereIn('product_sku_id', $skuIds)->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\User; | ||
use App\Models\UserAddress; | ||
use App\Models\Order; | ||
use App\Models\ProductSku; | ||
use App\Exceptions\InvalidRequestException; | ||
use App\Jobs\CloseOrder; | ||
use Carbon\Carbon; | ||
|
||
class OrderService | ||
{ | ||
public function store(User $user, UserAddress $address, $remark, $items) | ||
{ | ||
// 开启一个数据库事务 | ||
$order = \DB::transaction(function () use ($user, $address, $remark, $items) { | ||
// 更新此地址的最后使用时间 | ||
$address->update(['last_used_at' => Carbon::now()]); | ||
// 创建一个订单 | ||
$order = new Order([ | ||
'address' => [ // 将地址信息放入订单中 | ||
'address' => $address->full_address, | ||
'zip' => $address->zip, | ||
'contact_name' => $address->contact_name, | ||
'contact_phone' => $address->contact_phone, | ||
], | ||
'remark' => $remark, | ||
'total_amount' => 0, | ||
]); | ||
// 订单关联到当前用户 | ||
$order->user()->associate($user); | ||
// 写入数据库 | ||
$order->save(); | ||
|
||
$totalAmount = 0; | ||
// 遍历用户提交的 SKU | ||
foreach ($items as $data) { | ||
$sku = ProductSku::find($data['sku_id']); | ||
// 创建一个 OrderItem 并直接与当前订单关联 | ||
$item = $order->items()->make([ | ||
'amount' => $data['amount'], | ||
'price' => $sku->price, | ||
]); | ||
$item->product()->associate($sku->product_id); | ||
$item->productSku()->associate($sku); | ||
$item->save(); | ||
$totalAmount += $sku->price * $data['amount']; | ||
if ($sku->decreaseStock($data['amount']) <= 0) { | ||
throw new InvalidRequestException('该商品库存不足'); | ||
} | ||
} | ||
// 更新订单总金额 | ||
$order->update(['total_amount' => $totalAmount]); | ||
|
||
// 将下单的商品从购物车中移除 | ||
$skuIds = collect($items)->pluck('sku_id')->all(); | ||
app(CartService::class)->remove($skuIds); | ||
|
||
return $order; | ||
}); | ||
|
||
// 这里我们直接使用 dispatch 函数 | ||
dispatch(new CloseOrder($order, config('app.order_ttl'))); | ||
|
||
return $order; | ||
} | ||
} |