From 90b61f4fe4d0a1391ee66dd649ca3c969cfe3d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Mon, 20 Dec 2021 20:18:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=94=AF=E4=BB=98=E9=87=91?= =?UTF-8?q?=E9=A2=9D=E4=B8=BA0=E6=97=B6,=E8=87=AA=E5=8A=A8=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=94=AF=E4=BB=98=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Order.php | 1 + app/Services/OrderService.php | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/Models/Order.php b/app/Models/Order.php index 3d6100ee..26facbe6 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -33,6 +33,7 @@ class Order extends Model */ public const PAY_WAY_WXPAY = 'wxpay'; // 微信支付 public const PAY_WAY_OFFLINE = 'offline'; // 现金支付 + public const PAY_WAY_NONE = 'none'; // 无 /** * @var array diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index ea619b13..875da72e 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -139,7 +139,7 @@ class OrderService // 订单总费用 $totalAmount += $shippingFee; - $order = $user->orders()->create([ + $orderAttrs = [ 'sn' => OrderHelper::serialNumber(), 'user_coupon_id' => $coupon?->id, 'coupon_discount_amount' => $couponDiscountAmount, @@ -147,16 +147,26 @@ class OrderService 'shipping_fee' => $shippingFee, 'products_total_amount' => $productsTotalAmount, 'total_amount' => $totalAmount, - 'status' => $totalAmount > 0 ? Order::STATUS_PENDING : Order::STATUS_PAID, 'note' => $note, // 收货地址 'consignee_name' => $shippingAddress->consignee, 'consignee_telephone' => $shippingAddress->telephone, 'consignee_zone' => $shippingAddress->zone, 'consignee_address' => $shippingAddress->address, - ]); + ]; - $data = []; + if ($totalAmount === 0) { + $orderAttrs = array_merge([ + 'status' => Order::STATUS_PAID, + 'pay_sn' => OrderHelper::serialNumber(), + 'pay_way' => Order::PAY_WAY_NONE, + 'pay_at' => now(), + ]); + } + + $order = $user->orders()->create($orderAttrs); + + $orderProducts = []; foreach ($mapProducts as $product) { $sku = $product['sku']; @@ -164,7 +174,7 @@ class OrderService // 支付金额 = 商品总额 - 优惠券折扣金额- 会员折扣金额 $totalAmount = $product['total_amount'] - $product['coupon_discount_amount'] - $product['vip_discount_amount']; - $data[] = [ + $orderProducts[] = [ 'user_id' => $order->user_id, 'order_id' => $order->id, 'spu_id' => $sku->spu_id, @@ -185,13 +195,13 @@ class OrderService 'updated_at' => $order->updated_at, ]; - // 扣商品库存 $sku->update([ 'stock' => DB::raw("stock - {$product['quantity']}"), // 库存 ]); } - OrderProduct::insert($data); + // 处理赠品 + OrderProduct::insert($orderProducts); // 将优惠券标记为已使用 $coupon?->markAsUse();