Browse Source

feat: 增强订单指派功能以支持距离参数

在OrderController和OrderService中,更新了assignCoach方法,新增了距离参数以计算订单金额。这一改进旨在提升订单处理的准确性,确保在指派教练时能够考虑距离因素,从而更好地反映订单的实际费用。
刘学玺 2 months ago
parent
commit
298dcd80b5

+ 2 - 1
app/Http/Controllers/Client/OrderController.php

@@ -346,8 +346,9 @@ class OrderController extends Controller
         $coachId = $request->input('coach_id');
         $orderId = $request->input('order_id');
         $payment_type = $request->input('payment_type');
+        $distance = $request->input('distance');
 
-        return $this->success($this->service->assignCoach($userId, $orderId, $coachId, $payment_type));
+        return $this->success($this->service->assignCoach($userId, $orderId, $coachId, $payment_type, $distance));
     }
 
     /**

+ 22 - 2
app/Services/Client/OrderService.php

@@ -2441,9 +2441,9 @@ readonly class OrderService
      * }
      * @throws \Exception
      */
-    public function assignCoach(int $userId, int $orderId, int $coachId, int $payment_type): array|bool
+    public function assignCoach(int $userId, int $orderId, int $coachId, int $payment_type, ?float $distance = null): array|bool
     {
-        return DB::transaction(function () use ($userId, $orderId, $coachId, $payment_type) {
+        return DB::transaction(function () use ($userId, $orderId, $coachId, $payment_type, $distance) {
             try {
                 // 1. 验证参数
                 $order = $this->validateAssignOrder($userId, $orderId);
@@ -2462,6 +2462,26 @@ readonly class OrderService
 
                 if (! is_null($payment_type)) {
                     $order->payment_type = $payment_type;
+
+                    // 2. 计算订单金额
+                    $amounts = $this->calculateOrderAmount(
+                        userId: $userId,
+                        addressId: $order->address_id ?? null,
+                        coachId: $coachId ?? null,
+                        projectId: $order->project_id,
+                        agentId: $order->agent_id ?? null,
+                        useBalance: $order->payment_type === PaymentMethod::BALANCE->value ? true : false,
+                        distance: $distance ?? 0
+                    );
+
+                    $order->fill([
+                        'total_amount' => $amounts['total_amount'],
+                        'balance_amount' => $amounts['balance_amount'],
+                        'pay_amount' => $amounts['pay_amount'],
+                        'discount_amount' => $amounts['coupon_amount'],
+                        'project_amount' => $amounts['project_amount'],
+                        'traffic_amount' => $amounts['delivery_fee'],
+                    ]);
                     $order->save();
                 }