392 lines
12 KiB
PHP
392 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Endpoint\Api\Http\Resources\ProductSku\ProductSkuSimpleResource;
|
|
use App\Endpoint\Api\Http\Resources\ShippingAddressResource;
|
|
use App\Endpoint\Api\Http\Resources\UserCouponResource;
|
|
use App\Exceptions\BizException;
|
|
use App\Exceptions\ShippingNotSupportedException;
|
|
use App\Helpers\Numeric;
|
|
use App\Models\ProductSku;
|
|
use App\Models\ShippingAddress;
|
|
use App\Models\User;
|
|
use App\Models\UserCoupon;
|
|
|
|
class OrderService
|
|
{
|
|
/**
|
|
* 确认快速下单
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param int $skuId
|
|
* @param int $quantity
|
|
* @param int|null $shippingAddressId
|
|
* @param int|null $couponId
|
|
* @return array
|
|
*/
|
|
public function verifyQuickOrder(User $user, int $skuId, int $quantity, ?int $shippingAddressId = null, ?int $couponId = null)
|
|
{
|
|
$sku = ProductSku::online()->findOrFail($skuId);
|
|
|
|
$product = [
|
|
'sku' => $sku,
|
|
'quantity' => $quantity,
|
|
];
|
|
|
|
return $this->verifyOrder($user, [$product], $shippingAddressId, $couponId);
|
|
}
|
|
|
|
/**
|
|
* 确认购物车订单
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param array $shoppingCartItemIds
|
|
* @param int|null $shippingAddressId
|
|
* @param int|null $couponId
|
|
* @return array
|
|
*/
|
|
public function verifyShoppingCartOrder(User $user, array $shoppingCartItemIds, ?int $shippingAddressId = null, ?int $couponId = null)
|
|
{
|
|
// 获取购买商品
|
|
$products = $this->getProductsByShoppingCart($user, $shoppingCartItemIds);
|
|
|
|
return $this->verifyOrder($user, $products, $shippingAddressId, $couponId);
|
|
}
|
|
|
|
/**
|
|
* 确认订单
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param array $products
|
|
* @param int|null $shippingAddressId
|
|
* @param int|null $couponId
|
|
* @return array
|
|
*/
|
|
protected function verifyOrder(User $user, array $products, ?int $shippingAddressId = null, ?int $couponId = null): array
|
|
{
|
|
// 获取收货地址
|
|
$shippingAddress = $this->getShippingAddress($user, $shippingAddressId);
|
|
|
|
// 优惠券
|
|
$coupon = null;
|
|
|
|
if ($couponId) {
|
|
$coupon = $user->coupons()->onlyAvailable()->findOrFail($couponId);
|
|
}
|
|
|
|
$mapProducts = $this->mapProducts($user, $products, $coupon);
|
|
|
|
// 是否支持配送
|
|
$shippingSupported = true;
|
|
// 运费
|
|
$shippingFee = 0;
|
|
|
|
if ($shippingAddress) {
|
|
try {
|
|
$shippingFee = $this->calculateShippingFee($mapProducts, $shippingAddress);
|
|
} catch (ShippingNotSupportedException $e) {
|
|
$shippingFee = 0;
|
|
$shippingSupported = false;
|
|
}
|
|
}
|
|
|
|
list(
|
|
$productsTotalAmount,
|
|
$vipDiscountAmount,
|
|
$couponDiscountAmount,
|
|
$totalAmount,
|
|
) = $this->calculateFees($mapProducts);
|
|
|
|
return [
|
|
'products' => collect($mapProducts)->map(function ($item) {
|
|
return [
|
|
'sku' => ProductSkuSimpleResource::make($item['sku']),
|
|
'quantity' => $item['quantity'],
|
|
];
|
|
}),
|
|
'coupons' => UserCouponResource::collection((new CouponService())->getAvailableCoupons($user, $products)),
|
|
'shipping_address' => $shippingAddress ? ShippingAddressResource::make($shippingAddress) : null, // 收货地址
|
|
'shipping_supported' => $shippingSupported,
|
|
'shipping_fee' => Numeric::trimTrailingZero(bcdiv($shippingFee, 100, 2)), // 运费
|
|
'products_total_amount' => Numeric::trimTrailingZero(bcdiv($productsTotalAmount, 100, 2)), // 商品总额
|
|
'vip_discount_amount' => Numeric::trimTrailingZero(bcdiv($vipDiscountAmount, 100, 2)), // 会员折扣金额
|
|
'coupon_discount_amount' => Numeric::trimTrailingZero(bcdiv($couponDiscountAmount, 100, 2)), // 优惠券折扣金额
|
|
'total_amount' => Numeric::trimTrailingZero(bcdiv($totalAmount + $shippingFee, 100, 2)), // 实付金额
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 计算费用
|
|
*
|
|
* @param array $products
|
|
* @return array
|
|
*/
|
|
protected function calculateFees(array $products)
|
|
{
|
|
$productsTotalAmount = 0;
|
|
$vipDiscountAmount = 0;
|
|
$couponDiscountAmount = 0;
|
|
|
|
|
|
foreach ($products as $product) {
|
|
$productsTotalAmount += $product['total_amount'];
|
|
$vipDiscountAmount += $product['vip_discount_amount'];
|
|
$couponDiscountAmount += $product['coupon_discount_amount'];
|
|
}
|
|
|
|
// 订单总额
|
|
$totalAmount = $productsTotalAmount - $vipDiscountAmount - $couponDiscountAmount;
|
|
|
|
return [
|
|
$productsTotalAmount,
|
|
$vipDiscountAmount,
|
|
$couponDiscountAmount,
|
|
$totalAmount,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 计算运费
|
|
*
|
|
* @param array $products
|
|
* @param \App\Models\ShippingAddress $shippingAddress
|
|
* @return int
|
|
*/
|
|
protected function calculateShippingFee(array $products, ShippingAddress $shippingAddress): int
|
|
{
|
|
// 运费
|
|
$shippingFee = 0;
|
|
|
|
$shippings = [];
|
|
|
|
foreach ($products as $product) {
|
|
$sku = $product['sku'];
|
|
|
|
if (is_null($sku->shipping_template_id)) {
|
|
continue;
|
|
}
|
|
|
|
$shipping = $shippings[$sku->shipping_template_id] ?? [
|
|
'total_weight' => 0,
|
|
'total_amount' => 0,
|
|
];
|
|
$shipping['total_weight'] += $sku->weight * $product['quantity'];
|
|
$shipping['total_amount'] += $product['total_amount'] - $product['vip_discount_amount'] - $product['coupon_discount_amount'];
|
|
|
|
$shippings[$sku->shipping_template_id] = $shipping;
|
|
}
|
|
|
|
$shippingService = new ShippingService();
|
|
|
|
foreach ($shippings as $templateId => $shipping) {
|
|
$shippingFee += $shippingService->countShippingAmount(
|
|
$shipping['total_weight'],
|
|
$shipping['total_amount'],
|
|
$templateId,
|
|
$shippingAddress->zone_id
|
|
);
|
|
}
|
|
|
|
return $shippingFee;
|
|
}
|
|
|
|
/**
|
|
* 准备商品信息
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param array $products
|
|
* @param \App\Models\UserCoupon|null $coupon
|
|
* @return array
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
*/
|
|
protected function mapProducts(User $user, array $products, ?UserCoupon $coupon): array
|
|
{
|
|
$_products = collect($products)->map(function ($item) use ($user) {
|
|
$sku = $item['sku'];
|
|
|
|
return array_merge($item, [
|
|
// 优惠券折扣金额
|
|
'coupon_discount_amount' => 0,
|
|
// 会员折扣金额
|
|
'vip_discount_amount' => $this->calculateVipDiscountAmount(
|
|
$user, $sku, $item['quantity']
|
|
),
|
|
// 总金额
|
|
'total_amount' => $sku->sell_price * $item['quantity'],
|
|
]);
|
|
});
|
|
|
|
if ($coupon === null) {
|
|
return $_products->all();
|
|
}
|
|
|
|
$couponDiscountAmounts = $this->getCouponDiscountAmounts($coupon, $_products->all());
|
|
|
|
return $_products->map(function ($item) use ($couponDiscountAmounts) {
|
|
$item['coupon_discount_amount'] = $couponDiscountAmounts[$item['sku']->id] ?? 0;
|
|
|
|
return $item;
|
|
})->all();
|
|
}
|
|
|
|
/**
|
|
* 计算会员折扣金额
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param \App\Models\ProductSku $sku
|
|
* @param int $quantity
|
|
* @return int
|
|
*/
|
|
protected function calculateVipDiscountAmount(User $user, ProductSku $sku, int $quantity)
|
|
{
|
|
$price = $sku->getRealPrice($user);
|
|
|
|
if ($price > $sku->sell_price) {
|
|
return 0;
|
|
}
|
|
|
|
return ($sku->sell_price - $price) * $quantity;
|
|
}
|
|
|
|
/**
|
|
* 获取商品的优惠券折扣金额
|
|
*
|
|
* @param \App\Models\UserCoupon $coupon
|
|
* @param array $products
|
|
* @return array
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
*/
|
|
protected function getCouponDiscountAmounts(UserCoupon $coupon, array $products): array
|
|
{
|
|
// 启用的券的使用规则
|
|
$coupon->loadMissing(['ranges' => function ($query) {
|
|
$query->isEnable();
|
|
}]);
|
|
|
|
// 可使用优惠券的商品总额
|
|
$amounts = [];
|
|
|
|
foreach ($products as $item) {
|
|
$sku = $item['sku'];
|
|
|
|
if (! $coupon->isSupport($sku)) {
|
|
throw new BizException('优惠券不满足使用条件');
|
|
}
|
|
|
|
$amount = $item['total_amount'] - $item['vip_discount_amount'];
|
|
|
|
// 仅保留商品真实总额大于0的商品
|
|
if ($amount > 0) {
|
|
$amounts[$sku->id] = $amount;
|
|
}
|
|
}
|
|
|
|
// 全部商品总额
|
|
$totalAmount = array_sum($amounts);
|
|
|
|
if ($coupon->coupon_threshold > $totalAmount) {
|
|
throw new BizException('优惠券不满足使用条件');
|
|
}
|
|
|
|
// 商品的券折扣金额
|
|
$discountAmounts = [];
|
|
// 优惠券折扣总额
|
|
$discountTotalAmount = 0;
|
|
// 未计算优惠金额的商品总数
|
|
$lastCount = count($amounts);
|
|
|
|
foreach ($amounts as $skuId => $amount) {
|
|
if ($coupon->isDiscountCoupon()) {
|
|
/*
|
|
|----------------------------------------
|
|
| 计算折扣券的折扣金额
|
|
|----------------------------------------
|
|
*/
|
|
|
|
$discountAmounts[$skuId] = (int) ($amount * $coupon->coupon_amount / 100);
|
|
} else {
|
|
/*
|
|
|----------------------------------------
|
|
| 计算抵扣券的抵扣金额
|
|
|----------------------------------------
|
|
*/
|
|
|
|
$couponAmount = $coupon->coupon_amount;
|
|
|
|
// 如果券的抵用金额大于商品的实际金额,则以商品实际金额作为抵扣金额
|
|
if ($couponAmount > $totalAmount) {
|
|
$couponAmount = $totalAmount;
|
|
}
|
|
|
|
$discountAmounts[$skuId] = (int) ($couponAmount * $amount / $totalAmount);
|
|
$discountTotalAmount += $discountAmounts[$skuId];
|
|
|
|
$lastCount--;
|
|
|
|
// 将券的剩余抵扣金额给最后一个商品
|
|
if ($lastCount === 0) {
|
|
$discountAmounts[$skuId] += $couponAmount - $discountTotalAmount;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $discountAmounts;
|
|
}
|
|
|
|
/**
|
|
* 根据购物车获取商品
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param array $shoppingCartItemIds
|
|
* @return array
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
*/
|
|
protected function getProductsByShoppingCart(User $user, array $shoppingCartItemIds): array
|
|
{
|
|
$shoppingCartItems = $user->shoppingCartItems()->findMany($shoppingCartItemIds);
|
|
|
|
if ($shoppingCartItems->count() !== count($shoppingCartItemIds)) {
|
|
throw new BizException('购物车商品已丢失');
|
|
}
|
|
|
|
$shoppingCartItems->load('sku');
|
|
|
|
$lostShoppingCartItems = $shoppingCartItems->filter(function ($item) {
|
|
return $item->sku === null;
|
|
});
|
|
|
|
if ($lostShoppingCartItems->count() > 0) {
|
|
throw new BizException('购物车商品已失效');
|
|
}
|
|
|
|
return $shoppingCartItems->map(function ($item) {
|
|
return [
|
|
'sku' => $item->sku,
|
|
'quantity' => $item->quantity,
|
|
];
|
|
})->all();
|
|
}
|
|
|
|
/**
|
|
* 获取收货地址
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param int|null $shippingAddressId
|
|
* @return \App\Models\ShippingAddress|null
|
|
*
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
*/
|
|
protected function getShippingAddress(User $user, ?int $shippingAddressId = null): ?ShippingAddress
|
|
{
|
|
if ($shippingAddressId) {
|
|
return $user->shippingAddresses()->findOrFail($shippingAddressId);
|
|
}
|
|
|
|
return $user->shippingAddresses()->where('is_default', true)->first();
|
|
}
|
|
}
|