CoachService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Enums\OrderStatus;
  4. use App\Enums\TechnicianAuthStatus;
  5. use App\Enums\TechnicianLocationType;
  6. use App\Enums\TechnicianStatus;
  7. use App\Enums\UserStatus;
  8. use App\Models\CoachSchedule;
  9. use App\Models\CoachUser;
  10. use App\Models\MemberUser;
  11. use App\Models\Order;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class CoachService
  18. {
  19. /**
  20. * 获取技师列表
  21. */
  22. public function getNearCoachList(int $userId, float $latitude, float $longitude)
  23. {
  24. $page = request()->get('page', 1);
  25. $perPage = request()->get('per_page', 15);
  26. // 获取当前用户
  27. $user = MemberUser::find($userId);
  28. Log::info('Current user and coordinates:', [
  29. 'user' => $user ? $user->id : null,
  30. 'latitude' => $latitude,
  31. 'longitude' => $longitude,
  32. ]);
  33. // 检查用户状态
  34. if (! $user) {
  35. throw new \Exception('用户未登录');
  36. }
  37. if ($user->state !== UserStatus::OPEN->value) {
  38. throw new \Exception('用户状态异常');
  39. }
  40. // 使用 Redis 的 georadius 命令获取附近的技师 ID
  41. $nearbyCoachIds = Redis::georadius('coach_locations', $longitude, $latitude, 40, 'km', ['WITHDIST']);
  42. $coachData = array_map(function ($item) {
  43. [$id, $type] = explode('_', $item[0]);
  44. return ['id' => $id, 'type' => $type, 'distance' => $item[1]];
  45. }, $nearbyCoachIds);
  46. // 提取所有的id
  47. $coachIds = array_unique(array_column($coachData, 'id'));
  48. // 分页截取 coachIds
  49. $paginatedCoachIds = array_slice($coachIds, ($page - 1) * $perPage, $perPage);
  50. // 查询数据库获取技师信息
  51. $coaches = CoachUser::query()
  52. ->whereIn('id', $paginatedCoachIds)
  53. ->whereHas('info', function ($query) {
  54. $query->where('state', TechnicianAuthStatus::PASSED->value);
  55. })
  56. ->whereHas('real', function ($query) {
  57. $query->where('state', TechnicianAuthStatus::PASSED->value);
  58. })
  59. ->whereHas('qual', function ($query) {
  60. $query->where('state', TechnicianAuthStatus::PASSED->value);
  61. })
  62. ->with(['info:id,nickname,avatar,gender'])
  63. ->paginate($perPage);
  64. // 遍历技师并设置距离
  65. foreach ($coaches as $coach) {
  66. $coach->distance = round($coachData[array_search($coach->id, array_column($coachData, 'id'))]['distance'] ?? null, 2);
  67. }
  68. // 按 distance 升序排序
  69. $coaches = $coaches->sortBy('distance')->values();
  70. return $coaches;
  71. }
  72. /**
  73. * 获取技师详情
  74. */
  75. public function getCoachDetail($coachId, $latitude, $longitude)
  76. {
  77. // 检查Redis连接
  78. try {
  79. $pingResult = Redis::connection()->ping();
  80. Log::info('Redis connection test:', ['ping_result' => $pingResult]);
  81. } catch (\Exception $e) {
  82. Log::error('Redis connection error:', ['error' => $e->getMessage()]);
  83. throw new \Exception('Redis连接失败:'.$e->getMessage());
  84. }
  85. // 检查Redis中的所有位置数据
  86. $allLocations = Redis::zrange('coach_locations', 0, -1, 'WITHSCORES');
  87. Log::info('All locations in Redis:', ['locations' => $allLocations]);
  88. // 获取当前用户
  89. $user = Auth::user();
  90. Log::info('Current user and coordinates:', [
  91. 'user' => $user ? $user->id : null,
  92. 'latitude' => $latitude,
  93. 'longitude' => $longitude,
  94. 'coach_id' => $coachId,
  95. ]);
  96. // 检查用户状态
  97. if (! $user) {
  98. throw new \Exception('用户未登录');
  99. }
  100. if ($user->state !== UserStatus::OPEN->value) {
  101. throw new \Exception('用户状态异常');
  102. }
  103. // 获取技师信息
  104. $coach = CoachUser::where('state', TechnicianStatus::ACTIVE->value)
  105. ->whereHas('info', function ($query) {
  106. $query->where('state', TechnicianAuthStatus::PASSED->value);
  107. })
  108. ->whereHas('real', function ($query) {
  109. $query->where('state', TechnicianAuthStatus::PASSED->value);
  110. })
  111. ->whereHas('qual', function ($query) {
  112. $query->where('state', TechnicianAuthStatus::PASSED->value);
  113. })
  114. ->with(['info:id,nickname,avatar,gender'])
  115. ->find($coachId);
  116. if (! $coach) {
  117. throw new \Exception('技师不存在');
  118. }
  119. // 从 Redis 获取技师的 id_home 和 id_work 的经纬度
  120. $homeLocation = Redis::geopos('coach_locations', $coachId.'_'.TechnicianLocationType::COMMON->value);
  121. $workLocation = Redis::geopos('coach_locations', $coachId.'_'.TechnicianLocationType::CURRENT->value);
  122. // 检查输入的经纬度是否有效
  123. if (! is_numeric($latitude) || ! is_numeric($longitude)) {
  124. Log::error('Invalid coordinates:', ['latitude' => $latitude, 'longitude' => $longitude]);
  125. throw new \Exception('无效的经纬度坐标');
  126. }
  127. // 临时存储用户当前位置用于计算距离
  128. $tempKey = 'user_temp_'.$user->id;
  129. Redis::geoadd('coach_locations', $longitude, $latitude, $tempKey);
  130. // 计算距离(单位:km)
  131. $distanceHome = null;
  132. $distanceWork = null;
  133. if ($homeLocation && ! empty($homeLocation[0])) {
  134. $distanceHome = Redis::geodist('coach_locations', $tempKey, $coachId.'_'.TechnicianLocationType::COMMON->value, 'km');
  135. Log::info('Home distance calculation:', [
  136. 'from' => $tempKey,
  137. 'to' => $coachId.'_'.TechnicianLocationType::COMMON->value,
  138. 'distance' => $distanceHome,
  139. 'home_location' => $homeLocation[0],
  140. ]);
  141. }
  142. if ($workLocation && ! empty($workLocation[0])) {
  143. $distanceWork = Redis::geodist('coach_locations', $tempKey, $coachId.'_'.TechnicianLocationType::CURRENT->value, 'km');
  144. }
  145. // 删除临时位置点
  146. Redis::zrem('coach_locations', $tempKey);
  147. // 选择最近的距离
  148. $distances = array_filter([$distanceHome, $distanceWork]);
  149. $coach->distance = ! empty($distances) ? round(min($distances), 2) : null;
  150. return $coach;
  151. }
  152. /**
  153. * 设置技师位置信息
  154. *
  155. * @param int $coachId 技师ID
  156. * @param float $latitude 纬度
  157. * @param float $longitude 经度
  158. * @param int $type 位置类型 (current|common)
  159. * @return bool
  160. *
  161. * @throws \Exception
  162. */
  163. public function setCoachLocation($coachId, $latitude, $longitude, $type = TechnicianLocationType::COMMON->value)
  164. {
  165. if (! is_numeric($latitude) || ! is_numeric($longitude)) {
  166. Log::error('Invalid coordinates in setCoachLocation:', [
  167. 'coach_id' => $coachId,
  168. 'latitude' => $latitude,
  169. 'longitude' => $longitude,
  170. ]);
  171. throw new \Exception('无效的经纬度坐标');
  172. }
  173. if (! in_array($type, [TechnicianLocationType::CURRENT->value, TechnicianLocationType::COMMON->value])) {
  174. throw new \Exception('无效的位置类型,必须是 current 或 common');
  175. }
  176. $key = $coachId.'_'.$type;
  177. $result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
  178. Log::info('Coach location set:', [
  179. 'coach_id' => $coachId,
  180. 'type' => $type,
  181. 'key' => $key,
  182. 'latitude' => $latitude,
  183. 'longitude' => $longitude,
  184. 'result' => $result,
  185. ]);
  186. // 验证数据是否成功写入
  187. $location = Redis::geopos('coach_locations', $key);
  188. Log::info('Verify location after set:', [
  189. 'key' => $key,
  190. 'location' => $location,
  191. ]);
  192. return $result;
  193. }
  194. /**
  195. * 获取技师可预约时间段列表
  196. *
  197. * @param int $coachId 技师ID
  198. * @param string|null $date 日期,默认当天
  199. * @return array 返回格式:[
  200. * 'date' => '2024-03-22',
  201. * 'day_of_week' => '星期五',
  202. * 'is_today' => false,
  203. * 'time_slots' => [
  204. * [
  205. * 'start_time' => '09:00',
  206. * 'end_time' => '09:30',
  207. * 'is_available' => true,
  208. * 'duration' => 30
  209. * ]
  210. * ],
  211. * 'total_slots' => 1,
  212. * 'updated_at' => '2024-03-22 10:00:00'
  213. * ]
  214. *
  215. * @throws \Exception
  216. */
  217. public function getSchedule(int $coachId, ?string $date = null)
  218. {
  219. try {
  220. // 默认使用当天日期
  221. $date = $date ? Carbon::parse($date)->format('Y-m-d') : now()->toDateString();
  222. $targetDate = Carbon::parse($date);
  223. // 验证技师信息
  224. $coach = CoachUser::with('info')->find($coachId);
  225. abort_if(! $coach, 404, '技师不存在');
  226. abort_if($coach->info->state != TechnicianStatus::ACTIVE->value,
  227. 400, '技师状态异常');
  228. // 验证日期
  229. abort_if($targetDate->startOfDay()->lt(now()->startOfDay()),
  230. 400, '不能查询过去的日期');
  231. abort_if($targetDate->diffInDays(now()) > 30,
  232. 400, '只能查询未来30天内的时间段');
  233. $cacheKey = "coach:timeslots:{$coachId}:{$date}";
  234. Cache::forget($cacheKey);
  235. // 使用缓存减少数据库查询
  236. return Cache::remember(
  237. "coach:timeslots:{$coachId}:{$date}",
  238. now()->addMinutes(15), // 缓存15分钟
  239. function () use ($coachId, $date, $targetDate) {
  240. // 获取技师排班信息
  241. $schedule = CoachSchedule::where('coach_id', $coachId)
  242. ->where('state', 1)
  243. ->first();
  244. if (! $schedule || empty($schedule->time_ranges)) {
  245. return $this->formatResponse($date, []);
  246. }
  247. $timeRanges = json_decode($schedule->time_ranges, true);
  248. // 获取当天所有订单
  249. $dayOrders = $this->getDayOrders($coachId, $date);
  250. // 生成时间段列表
  251. $timeSlots = $this->generateAvailableTimeSlots(
  252. $date,
  253. $timeRanges,
  254. $dayOrders,
  255. $targetDate->isToday()
  256. );
  257. return $this->formatResponse($date, $timeSlots);
  258. }
  259. );
  260. } catch (\Exception $e) {
  261. Log::error('获取可预约时间段失败', [
  262. 'coach_id' => $coachId,
  263. 'date' => $date,
  264. 'error' => $e->getMessage(),
  265. 'trace' => $e->getTraceAsString(),
  266. ]);
  267. throw $e;
  268. }
  269. }
  270. /**
  271. * 获取当天所有订单
  272. *
  273. * @param int $coachId 技师ID
  274. * @param string $date 日期
  275. * @return array 订单列表
  276. */
  277. private function getDayOrders(int $coachId, string $date): array
  278. {
  279. $date = Carbon::parse($data['date'] ?? $date);
  280. $startOfDay = $date->startOfDay()->format('Y-m-d H:i:s');
  281. $endOfDay = $date->endOfDay()->format('Y-m-d H:i:s');
  282. return Order::where('coach_id', $coachId)
  283. ->whereBetween('service_time', [$startOfDay, $endOfDay])
  284. ->whereIn('state', [
  285. OrderStatus::ACCEPTED->value,
  286. OrderStatus::DEPARTED->value,
  287. OrderStatus::ARRIVED->value,
  288. OrderStatus::SERVING->value,
  289. ])
  290. ->select(['id', 'service_start_time', 'service_end_time', 'state'])
  291. ->get()
  292. ->toArray();
  293. }
  294. /**
  295. * 生成可用时间段列表
  296. *
  297. * @param string $date 日期
  298. * @param array $timeRanges 排班时间段
  299. * @param array $dayOrders 当天订单
  300. * @param bool $isToday 是否是当天
  301. * @return array 可用时间段列表
  302. */
  303. private function generateAvailableTimeSlots(
  304. string $date,
  305. array $timeRanges,
  306. array $dayOrders,
  307. bool $isToday
  308. ): array {
  309. $timeSlots = [];
  310. $currentTime = now();
  311. foreach ($timeRanges as $range) {
  312. $start = Carbon::parse($date.' '.$range['start_time']);
  313. $end = Carbon::parse($date.' '.$range['end_time']);
  314. // 如果是当天且开始时间已过,从下一个30分钟时间点开始
  315. if ($isToday && $start->lt($currentTime)) {
  316. $start = $currentTime->copy()->addMinutes(30)->floorMinutes(30);
  317. // 如果调整后的开始时间已超过结束时间,跳过此时间段
  318. if ($start->gt($end)) {
  319. continue;
  320. }
  321. }
  322. // 生成30分钟间隔的时间段
  323. while ($start->lt($end)) {
  324. $slotStart = $start->format('H:i');
  325. $slotEnd = $start->copy()->addMinutes(30)->format('H:i');
  326. // 检查时间段是否被订单占用
  327. if (! $this->hasConflictingOrder($date, $slotStart, $slotEnd, $dayOrders)) {
  328. $timeSlots[] = [
  329. 'start_time' => $slotStart,
  330. 'end_time' => $slotEnd,
  331. 'is_available' => true,
  332. 'duration' => 30,
  333. ];
  334. }
  335. $start->addMinutes(30);
  336. }
  337. }
  338. return $timeSlots;
  339. }
  340. /**
  341. * 格式化返回数据
  342. *
  343. * @param string $date 日期
  344. * @param array $timeSlots 时间段列表
  345. * @return array 格式化后的数据
  346. */
  347. private function formatResponse(string $date, array $timeSlots): array
  348. {
  349. $targetDate = Carbon::parse($date);
  350. return [
  351. 'date' => $date,
  352. 'day_of_week' => $targetDate->isoFormat('dddd'), // 星期几
  353. 'is_today' => $targetDate->isToday(),
  354. 'time_slots' => $timeSlots,
  355. 'total_slots' => count($timeSlots),
  356. 'updated_at' => now()->toDateTimeString(),
  357. ];
  358. }
  359. /**
  360. * 检查是否与已有订单冲突
  361. *
  362. * @param string $date 日期
  363. * @param string $startTime 开始时间
  364. * @param string $endTime 结束时间
  365. * @param array $dayOrders 当天订单
  366. * @return bool 是否冲突
  367. */
  368. private function hasConflictingOrder(
  369. string $date,
  370. string $startTime,
  371. string $endTime,
  372. array $dayOrders
  373. ): bool {
  374. $slotStart = Carbon::parse("$date $startTime");
  375. $slotEnd = Carbon::parse("$date $endTime");
  376. foreach ($dayOrders as $order) {
  377. $orderStart = Carbon::parse($order['service_start_time']);
  378. $orderEnd = Carbon::parse($order['service_end_time']);
  379. // 检查时间段是否重叠
  380. if (($slotStart >= $orderStart && $slotStart < $orderEnd) ||
  381. ($slotEnd > $orderStart && $slotEnd <= $orderEnd) ||
  382. ($slotStart <= $orderStart && $slotEnd >= $orderEnd)) {
  383. return true;
  384. }
  385. }
  386. return false;
  387. }
  388. /**
  389. * 验证技师服务时间是否在可用时间段内
  390. */
  391. public function validateServiceTimeWithinCoachAvailability(int $coachId, string $serviceTime): bool
  392. {
  393. $serviceDateTime = Carbon::parse($serviceTime);
  394. $coachSchedule = $this->getSchedule($coachId, $serviceDateTime->format('Y-m-d'));
  395. foreach ($coachSchedule['time_slots'] as $slot) {
  396. $slotStart = Carbon::parse($serviceDateTime->format('Y-m-d').' '.$slot['start_time']);
  397. $slotEnd = Carbon::parse($serviceDateTime->format('Y-m-d').' '.$slot['end_time']);
  398. if ($serviceDateTime->between($slotStart, $slotEnd)) {
  399. return true;
  400. }
  401. }
  402. return false;
  403. }
  404. }