Browse Source

fixed:订单初始化

刘学玺 4 months ago
parent
commit
9b5136d9c7

+ 1 - 1
app/Http/Controllers/Client/CoachController.php

@@ -58,7 +58,7 @@ class CoachController extends Controller
      *
      * @authenticated
      *
-     * @urlParam id int required 技师ID. Example: 1
+     * @urlParam id int required 技师ID. Example: 6
      *
      * @queryParam latitude float 纬度. Example: 34.0522
      * @queryParam longitude float 经度. Example: -118.2437

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

@@ -31,6 +31,8 @@ class OrderController extends Controller
      * @bodyParam coach_id int required 技师ID. Example: 6
      * @bodyParam area_code string required 区划代码. Example: 370602
      * @bodyParam project_id int required 项目ID. Example: 1
+     * @bodyParam latitude int 纬度. Example: 37.4219983
+     * @bodyParam longitude int 经度. Example: 122.1347344
      *
      * @response {
      *   "status": "success",
@@ -39,7 +41,7 @@ class OrderController extends Controller
      */
     public function initialize(Request $request)
     {
-        $data = $request->only(['coach_id', 'area_code', 'project_id']);
+        $data = $request->only(['coach_id', 'area_code', 'project_id', 'latitude', 'longitude']);
 
         return $this->service->initialize(Auth::user()->id, $data);
     }
@@ -316,4 +318,31 @@ class OrderController extends Controller
 
         return $this->service->assignCoach($userId, $orderId, $coachId);
     }
+
+    /**
+     * [订单]获取抢单列表
+     *
+     * 获取抢单列表
+     *
+     * @queryParam order_id int required 订单ID. Example: 7
+     *
+     * @response {
+     *  "data": [
+     *    {
+     *      "id": 1,
+     *      "coach_id": 1,
+     *      "nickname": "技师昵称",
+     *      "avatar": "头像地址",
+     *      "created_at": "2024-03-21 10:00:00"
+     *    }
+     *  ]
+     * }
+     */
+    public function getOrderGrabList(Request $request)
+    {
+
+        $orderId = $request->input('order_id');
+
+        return $this->service->getOrderGrabList($orderId);
+    }
 }

+ 5 - 5
app/Http/Controllers/Client/ProjectController.php

@@ -77,8 +77,7 @@ class ProjectController extends Controller
      *
      * @authenticated
      *
-     * @urlParam id integer required 项目ID. Example: 1
-     *
+     * @queryParam id integer required 项目ID. Example: 1
      * @queryParam area_code string required 区域代码. Example: 330100
      *
      * @response {
@@ -111,11 +110,12 @@ class ProjectController extends Controller
      *   "message": "该区域暂无代理商"
      * }
      */
-    public function detail(Request $request, $id)
+    public function detail(Request $request)
     {
+        $projectId = $request->input('id');
         $areaCode = $request->input('area_code');
 
-        return $this->service->getProjectDetail($id, $areaCode);
+        return $this->service->getProjectDetail($projectId, $areaCode);
     }
 
     /**
@@ -125,7 +125,7 @@ class ProjectController extends Controller
      *
      * @authenticated
      *
-     * @queryParam coach_id integer required 技师ID. Example: 1
+     * @queryParam coach_id integer required 技师ID. Example: 6
      * @queryParam area_code string required 区域代码. Example: 330100
      * @queryParam project_cate_id integer 项目分类ID. Example: 1
      *

+ 56 - 9
app/Services/Client/OrderService.php

@@ -2,6 +2,7 @@
 
 namespace App\Services\Client;
 
+use App\Enums\ProjectStatus;
 use App\Models\AgentConfig;
 use App\Models\AgentInfo;
 use App\Models\CoachConfig;
@@ -66,7 +67,6 @@ class OrderService
 
                 // 查询技师数据
                 $coach = $this->validateCoach($data['coach_id']);
-
                 // 获取项目详情
                 $project = $this->projectService->getProjectDetail($data['project_id'], $areaCode);
                 abort_if(! $project, 400, '项目不存在');
@@ -77,7 +77,10 @@ class OrderService
                     $address?->id ?? 0,
                     $data['coach_id'],
                     $data['project_id'],
-                    $project->agent_id
+                    $project->agent_id,
+                    false,
+                    $data['latitude'],
+                    $data['longitude']
                 );
 
                 return [
@@ -820,6 +823,8 @@ class OrderService
      * @param  int  $agentId  代理商ID
      * @param  bool  $useBalance  是否使用余额
      * @param  float  $distance  距离
+     * @param  int  $lat  纬度
+     * @param  int  $lng  经度
      *
      * @throws Exception
      */
@@ -830,7 +835,9 @@ class OrderService
         int $projectId,
         ?int $agentId = null,
         bool $useBalance = false,
-        float $distance = 0
+        float $distance = 0,
+        int $lat = 0,
+        int $lng = 0
     ): array {
         try {
             // 1. 参数校验
@@ -848,19 +855,18 @@ class OrderService
                 ->first();
 
             abort_if(! $coachProject, 404, '技师项目不存在');
-
             // 3. 查询基础项目
+
             $project = Project::where('id', $projectId)
-                ->where('state', 'enable')
+                ->where('state', ProjectStatus::OPEN->value())
                 ->first();
 
             abort_if(! $project, 404, '项目不存在或状态异常');
-
             // 4. 计算距离
-            if ($distance <= 0) {
-                $address = $user->addresses()->findOrFail($addressId);
+            if (floatval($distance) <= 0) {
+                $address = $addressId && $user->addresses()->find($addressId) ?? ['latitude' => $lat, 'longitude' => $lng];
                 $coachService = app(CoachService::class);
-                $coachDetail = $coachService->getCoachDetail($coachId, $address->latitude, $address->longitude);
+                $coachDetail = $coachService->getCoachDetail($coachId, $address['latitude'], $address['longitude']);
                 $distance = $coachDetail['distance'] ?? 0;
             }
 
@@ -977,6 +983,47 @@ class OrderService
         return [$balanceAmount, $payAmount];
     }
 
+    /**
+     * 获取订单抢单池列表
+     *
+     * @param  int  $orderId  订单ID
+     * @return array 抢单池列表
+     */
+    public function getOrderGrabList(int $orderId): array
+    {
+
+        try {
+            // 查询订单信息
+            $order = Order::where('id', $orderId)
+                ->whereIn('state', ['wait_pay', 'wait_service'])
+                ->firstOrFail();
+            // 查询抢单池列表
+            $grabList = $order->grabRecords()->with(['coach.info'])->get();
+
+            // 格式化返回数据
+            $result = [];
+            foreach ($grabList as $grab) {
+                $coach = $grab->coach;
+                $result[] = [
+                    'id' => $grab->id,
+                    'coach_id' => $coach->id,
+                    'nickname' => $coach->info->nickname,
+                    'avatar' => $coach->info->avatar,
+                    'created_at' => $grab->created_at->format('Y-m-d H:i:s'),
+                ];
+            }
+
+            return $result;
+
+        } catch (\Exception $e) {
+            Log::error('获取订单抢单池列表失败', [
+                'error' => $e->getMessage(),
+                'order_id' => $orderId,
+            ]);
+            throw $e;
+        }
+    }
+
     /**
      * 指定技师
      */

+ 2 - 1
app/Services/Client/ProjectService.php

@@ -90,8 +90,9 @@ class ProjectService
      */
     public function getProjectDetail($projectId, $areaCode)
     {
+
         // 查询系统项目
-        $project = Project::where('state', 'enable')->find($projectId);
+        $project = Project::where('state', ProjectStatus::OPEN->value())->find($projectId);
         abort_if(! $project, 404, '项目不存在');
 
         // 根据区域代码获取代理商

+ 2 - 1
routes/api.php

@@ -58,7 +58,7 @@ Route::middleware('auth:sanctum')->group(function () {
     // 项目相关
     Route::prefix('project')->group(function () {
         Route::get('/', [ProjectController::class, 'index']); // 获取项目列表
-        Route::get('/{id}/detail', [ProjectController::class, 'detail']); // 获取项目详情
+        Route::get('/detail', [ProjectController::class, 'detail']); // 获取项目详情
         Route::get('/coach-list', [ProjectController::class, 'coachProjectList']); // 获取技师开通的项目列表
     });
 
@@ -98,6 +98,7 @@ Route::middleware('auth:sanctum')->group(function () {
         // Route::post('get-agent-config', [OrderController::class, 'getAgentConfig']);
         // Route::post('get-coach-config', [OrderController::class, 'getCoachConfig']);
         // Route::post('calculate-delivery-fee', [OrderController::class, 'calculateDeliveryFee']);
+        Route::get('grab-list', [OrderController::class, 'getOrderGrabList']);
     });
 
     // 钱包相关