Explorar el Código

fixed:技师端-优化获取技师排班信息

刘学玺 hace 3 meses
padre
commit
9e20e325ab

+ 14 - 15
app/Http/Controllers/Coach/AccountController.php

@@ -427,35 +427,34 @@ class AccountController extends Controller
     }
 
     /**
-     * [账户]获取技师排班信息
+     * [排班]获取技师排班信息
      *
-     * @return \Illuminate\Http\JsonResponse
+     * @description 获取技师当前的排班时间段设置
      *
-     * @description 技师获取自己的排班时间段信息
+     * @authenticated 需要技师身份认证
      *
-     * @response {
+     * @response 200 {
      *   "status": true,
      *   "message": "获取成功",
      *   "data": {
-     *     "time_ranges": [
-     *       {
-     *         "start_time": "09:00",
-     *         "end_time": "12:00"
-     *       },
+     *     "time_ranges": [                // 工作时间段列表
      *       {
-     *         "start_time": "14:00",
-     *         "end_time": "18:00"
+     *         "start_time": "09:00",      // 开始时间
+     *         "end_time": "12:00"         // 结束时间
      *       }
      *     ],
-     *     "updated_at": "2024-03-20 10:00:00"
+     *     "updated_at": "2024-03-21 10:00:00"  // 最后更新时间
      *   }
      * }
+     * @response 404 {
+     *   "message": "技师信息不存在"
+     * }
      */
     public function getSchedule()
     {
-        $schedule = $this->service->getSchedule(Auth::id());
-
-        return $this->success($schedule);
+        return $this->success(
+            $this->service->getSchedule(Auth::user()->coach)
+        );
     }
 
     /**

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

@@ -414,7 +414,7 @@ class AccountService
     }
 
     /**
-     * 格式化店铺���
+     * 格式化店铺
      * 处理技师所属店铺的基本信息
      *
      * 业务逻辑:
@@ -847,7 +847,7 @@ class AccountService
                     $range['start_minutes'] <= $prevRange['end_minutes'],
                     400,
                     "时间段 {$prevRange['start_time']}-{$prevRange['end_time']} 和 " .
-                        "{$range['start_time']}-{$range['end_time']} 之间有重��"
+                        "{$range['start_time']}-{$range['end_time']} 之间有重"
                 );
             }
         });
@@ -1053,51 +1053,48 @@ class AccountService
     /**
      * 获取技师排班信息
      *
-     * @param  int  $userId  用户ID
+     * 业务流程:
+     * 1. 从缓存获取数据,不存在则从数据库读取
+     * 2. 解析排班时间段
+     * 3. 缓存结果数据
+     *
+     * @param CoachUser $coach 技师对象
+     * @return array 返回排班信息,包含:
+     *        - time_ranges: array 工作时间段列表
+     *            - start_time: string 开始时间
+     *            - end_time: string 结束时间
+     * @throws \Exception 当获取失败时抛出异常
      */
-    public function getSchedule(int $userId): array
+    public function getSchedule(CoachUser $coach): 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);
-            }
+        $cacheKey = "coach:schedule:{$coach->id}";
 
-            // 缓存不存在,从数据库获取
+        return (array) Cache::remember($cacheKey, 86400, function () use ($coach) {
+            // 从数据库获取有效的排班记录
             $schedule = CoachSchedule::where('coach_id', $coach->id)
-                ->where('state', 1)
+                ->where('state', 1)  // 有效状态
                 ->first();
 
-            $result = [
-                'time_ranges' => $schedule ? json_decode($schedule->time_ranges, true) : [],
-                'updated_at' => $schedule ? $schedule->updated_at->toDateTimeString() : now()->toDateTimeString(),
+            // 构建并返回数据
+            return [
+                'time_ranges' => $this->parseTimeRanges($schedule),  // 解析时间段
             ];
+        });
+    }
 
-            // 写入缓存
-            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;
+    /**
+     * 解析排班时间段
+     *
+     * @param CoachSchedule|null $schedule 排班记录
+     * @return array 时间段列表
+     */
+    private function parseTimeRanges(?CoachSchedule $schedule): array
+    {
+        if (!$schedule) {
+            return [];
         }
+
+        return json_decode($schedule->time_ranges, true) ?: [];
     }
 
     /**