|
@@ -414,7 +414,7 @@ class AccountService
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 格式化店铺���息
|
|
|
|
|
|
+ * 格式化店铺信息
|
|
* 处理技师所属店铺的基本信息
|
|
* 处理技师所属店铺的基本信息
|
|
*
|
|
*
|
|
* 业务逻辑:
|
|
* 业务逻辑:
|
|
@@ -847,7 +847,7 @@ class AccountService
|
|
$range['start_minutes'] <= $prevRange['end_minutes'],
|
|
$range['start_minutes'] <= $prevRange['end_minutes'],
|
|
400,
|
|
400,
|
|
"时间段 {$prevRange['start_time']}-{$prevRange['end_time']} 和 " .
|
|
"时间段 {$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)
|
|
$schedule = CoachSchedule::where('coach_id', $coach->id)
|
|
- ->where('state', 1)
|
|
|
|
|
|
+ ->where('state', 1) // 有效状态
|
|
->first();
|
|
->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) ?: [];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|