Browse Source

feat:技师端-获取排班信息

刘学玺 4 months ago
parent
commit
43784f444d

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

@@ -342,4 +342,37 @@ class AccountController extends Controller
             $this->service->getWorkStatus(Auth::user()->coach->id)
         );
     }
+
+    /**
+     * [账户]获取技师排班信息
+     *
+     * @return \Illuminate\Http\JsonResponse
+     *
+     * @description 技师获取自己的排班时间段信息
+     *
+     * @response {
+     *   "status": true,
+     *   "message": "获取成功",
+     *   "data": {
+     *     "time_ranges": [
+     *       {
+     *         "start_time": "09:00",
+     *         "end_time": "12:00"
+     *       },
+     *       {
+     *         "start_time": "14:00",
+     *         "end_time": "18:00"
+     *       }
+     *     ],
+     *     "updated_at": "2024-03-20 10:00:00"
+     *   }
+     * }
+     */
+    public function getSchedule()
+    {
+        $schedule = $this->service->getSchedule(Auth::id());
+
+        return $this->success($schedule);
+
+    }
 }

+ 52 - 1
app/Services/Coach/AccountService.php

@@ -333,7 +333,7 @@ class AccountService
             // 生成Redis键
             $key = $coachId.'_'.$type;
 
-            // 将���置信息写入Redis
+            // 将置信息写入Redis
             $result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
 
             // 同时写入数据库保存历史记录
@@ -825,4 +825,55 @@ class AccountService
             throw $e;
         }
     }
+
+    /**
+     * 获取技师排班信息
+     *
+     * @param  int  $userId  用户ID
+     */
+    public function getSchedule(int $userId): array
+    {
+        try {
+            // 获取技师信息
+            $user = MemberUser::with(['coach'])->findOrFail($userId);
+            $coach = $user->coach;
+            abort_if(! $coach, 404, '技师信息不存在');
+
+            // 先尝试从缓存获取
+            $cacheKey = "coach:schedule:{$coach->id}";
+            $cached = Redis::get($cacheKey);
+            if ($cached) {
+                return json_decode($cached, true);
+            }
+
+            // 缓存不存在,从数据库获取
+            $schedule = CoachSchedule::where('coach_id', $coach->id)
+                ->where('state', 1)
+                ->first();
+
+            $result = [
+                'time_ranges' => $schedule ? json_decode($schedule->time_ranges, true) : [],
+                'updated_at' => $schedule ? $schedule->updated_at->toDateTimeString() : now()->toDateTimeString(),
+            ];
+
+            // 写入缓存
+            Redis::setex($cacheKey, 86400, json_encode($result));
+
+            // 记录日志
+            Log::info('获取技师排班信息成功', [
+                'coach_id' => $coach->id,
+                'schedule' => $result,
+            ]);
+
+            return $result;
+
+        } catch (\Exception $e) {
+            Log::error('获取技师排班信息失败', [
+                'user_id' => $userId,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString(),
+            ]);
+            throw $e;
+        }
+    }
 }

+ 4 - 0
routes/api.php

@@ -129,6 +129,8 @@ Route::prefix('client')->group(function () {
             Route::get('list', [MarketDistTeamController::class, 'index'])->name('team.list');
         });
 
+        // 技师排班相关接口
+        Route::get('/coach/schedule', [App\Http\Controllers\Client\Coach\ScheduleController::class, 'show']);
     });
 
 });
@@ -155,6 +157,8 @@ Route::middleware(['auth:sanctum'])->prefix('coach')->group(function () {
         Route::post('work-status', [App\Http\Controllers\Coach\AccountController::class, 'updateWorkStatus']);
         // 获取技师工作状态
         Route::get('work-status', [App\Http\Controllers\Coach\AccountController::class, 'getWorkStatus']);
+        // 获取技师排班信息
+        Route::get('schedule', [App\Http\Controllers\Coach\AccountController::class, 'getSchedule']);
     });
 
     // 订单相关路由