Browse Source

feat:技师端-获取当前工作状态

刘学玺 4 months ago
parent
commit
05bc1d446d

+ 25 - 0
app/Http/Controllers/Coach/AccountController.php

@@ -317,4 +317,29 @@ class AccountController extends Controller
             $this->service->updateWorkStatus(Auth::user()->id, $validated['status'])
         );
     }
+
+    /**
+     * [账户]获取技师工作状态
+     *
+     * @description 获取技师当前工作状态
+     *
+     * @authenticated
+     *
+     * @response {
+     *   "data": {
+     *     "work_status": 2,
+     *     "work_status_text": "空闲中",
+     *     "updated_at": "2024-03-22 10:00:00"
+     *   }
+     * }
+     * @response 404 {
+     *   "message": "技师不存在"
+     * }
+     */
+    public function getWorkStatus()
+    {
+        return $this->success(
+            $this->service->getWorkStatus(Auth::user()->coach->id)
+        );
+    }
 }

+ 36 - 0
app/Http/Controllers/Coach/CoachController.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace App\Http\Controllers\Coach;
+
+use App\Http\Controllers\Controller;
+use App\Services\Coach\AccountService;
+use Illuminate\Support\Facades\Auth;
+
+class CoachController extends Controller
+{
+    protected AccountService $service;
+
+    public function __construct(AccountService $service)
+    {
+        $this->service = $service;
+    }
+
+    /**
+     * [技师]获取工作状态
+     *
+     * @description 获取技师当前工作状态
+     *
+     * @authenticated
+     *
+     * @response {
+     *   "work_status": 2,
+     *   "work_status_text": "空闲中",
+     *   "has_active_order": false,
+     *   "updated_at": "2024-03-22 10:00:00"
+     * }
+     */
+    public function getWorkStatus()
+    {
+        return $this->service->getWorkStatus(Auth::user()->coach->id);
+    }
+}

+ 35 - 2
app/Services/Coach/AccountService.php

@@ -7,6 +7,7 @@ use App\Enums\TechnicianAuthStatus;
 use App\Enums\TechnicianLocationType;
 use App\Enums\TechnicianWorkStatus;
 use App\Models\CoachSchedule;
+use App\Models\CoachUser;
 use App\Models\MemberUser;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\DB;
@@ -332,7 +333,7 @@ class AccountService
             // 生成Redis键
             $key = $coachId.'_'.$type;
 
-            // 将置信息写入Redis
+            // 将���置信息写入Redis
             $result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
 
             // 同时写入数据库保存历史记录
@@ -467,7 +468,7 @@ class AccountService
                 ];
 
             } catch (\Exception $e) {
-                Log::error('技师排班设置���败', [
+                Log::error('技师排班设置败', [
                     'user_id' => $userId,
                     'time_ranges' => $timeRanges,
                     'error' => $e->getMessage(),
@@ -792,4 +793,36 @@ class AccountService
             // 缓存更新失败不影响主流程
         }
     }
+
+    /**
+     * 获取技师工作状态
+     *
+     * @param  int  $coachId  技师ID
+     */
+    public function getWorkStatus(int $coachId): array
+    {
+        try {
+            // 验证技师信息
+            $coach = CoachUser::find($coachId);
+
+            abort_if(! $coach, 404, '技师不存在');
+
+            // 直接获取技师信息里的work_status
+            $workStatus = $coach->work_status;
+
+            return [
+                'work_status' => $workStatus,
+                'work_status_text' => TechnicianWorkStatus::fromValue($workStatus)->label(),
+                'updated_at' => now()->toDateTimeString(),
+            ];
+
+        } catch (\Exception $e) {
+            Log::error('获取技师工作状态失败', [
+                'coach_id' => $coachId,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString(),
+            ]);
+            throw $e;
+        }
+    }
 }

+ 5 - 0
routes/api.php

@@ -153,6 +153,8 @@ Route::middleware(['auth:sanctum'])->prefix('coach')->group(function () {
         Route::post('schedule', [\App\Http\Controllers\Coach\AccountController::class, 'setSchedule']);
         // 更新技师工作状态
         Route::post('work-status', [App\Http\Controllers\Coach\AccountController::class, 'updateWorkStatus']);
+        // 获取技师工作状态
+        Route::get('work-status', [App\Http\Controllers\Coach\AccountController::class, 'getWorkStatus']);
     });
 
     // 订单相关路由
@@ -194,6 +196,9 @@ Route::middleware(['auth:sanctum'])->prefix('coach')->group(function () {
         Route::post('/withdraw', [CoachWalletController::class, 'withdraw'])
             ->middleware(['throttle:10,1']); // 限制提现频率
     });
+
+    // 获取技师工作状态
+    Route::get('/work-status', [CoachController::class, 'getWorkStatus']);
 });
 
 // 大观支付回调