123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- namespace App\Services\Client;
- use App\Enums\OrderStatus;
- use App\Enums\TechnicianAuthStatus;
- use App\Enums\TechnicianLocationType;
- use App\Enums\TechnicianStatus;
- use App\Enums\UserStatus;
- use App\Models\CoachSchedule;
- use App\Models\CoachUser;
- use App\Models\MemberUser;
- use App\Models\Order;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class CoachService
- {
-
- public function getNearCoachList(int $userId, float $latitude, float $longitude)
- {
- $page = request()->get('page', 1);
- $perPage = request()->get('per_page', 15);
-
- $user = MemberUser::find($userId);
- Log::info('Current user and coordinates:', [
- 'user' => $user ? $user->id : null,
- 'latitude' => $latitude,
- 'longitude' => $longitude,
- ]);
-
- if (! $user) {
- throw new \Exception('用户未登录');
- }
- if ($user->state !== UserStatus::OPEN->value) {
- throw new \Exception('用户状态异常');
- }
-
- $nearbyCoachIds = Redis::georadius('coach_locations', $longitude, $latitude, 40, 'km', ['WITHDIST']);
- $coachData = array_map(function ($item) {
- [$id, $type] = explode('_', $item[0]);
- return ['id' => $id, 'type' => $type, 'distance' => $item[1]];
- }, $nearbyCoachIds);
-
- $coachIds = array_unique(array_column($coachData, 'id'));
-
- $paginatedCoachIds = array_slice($coachIds, ($page - 1) * $perPage, $perPage);
-
- $coaches = CoachUser::query()
- ->whereIn('id', $paginatedCoachIds)
- ->whereHas('info', function ($query) {
- $query->where('state', TechnicianAuthStatus::PASSED->value);
- })
- ->whereHas('real', function ($query) {
- $query->where('state', TechnicianAuthStatus::PASSED->value);
- })
- ->whereHas('qual', function ($query) {
- $query->where('state', TechnicianAuthStatus::PASSED->value);
- })
- ->with(['info:id,nickname,avatar,gender'])
- ->paginate($perPage);
-
- foreach ($coaches as $coach) {
- $coach->distance = round($coachData[array_search($coach->id, array_column($coachData, 'id'))]['distance'] ?? null, 2);
- }
-
- $coaches = $coaches->sortBy('distance')->values();
- return $coaches;
- }
-
- public function getCoachDetail($coachId, $latitude, $longitude)
- {
-
- try {
- $pingResult = Redis::connection()->ping();
- Log::info('Redis connection test:', ['ping_result' => $pingResult]);
- } catch (\Exception $e) {
- Log::error('Redis connection error:', ['error' => $e->getMessage()]);
- throw new \Exception('Redis连接失败:'.$e->getMessage());
- }
-
- $allLocations = Redis::zrange('coach_locations', 0, -1, 'WITHSCORES');
- Log::info('All locations in Redis:', ['locations' => $allLocations]);
-
- $user = Auth::user();
- Log::info('Current user and coordinates:', [
- 'user' => $user ? $user->id : null,
- 'latitude' => $latitude,
- 'longitude' => $longitude,
- 'coach_id' => $coachId,
- ]);
-
- if (! $user) {
- throw new \Exception('用户未登录');
- }
- if ($user->state !== UserStatus::OPEN->value) {
- throw new \Exception('用户状态异常');
- }
-
- $coach = CoachUser::where('state', TechnicianStatus::ACTIVE->value)
- ->whereHas('info', function ($query) {
- $query->where('state', TechnicianAuthStatus::PASSED->value);
- })
- ->whereHas('real', function ($query) {
- $query->where('state', TechnicianAuthStatus::PASSED->value);
- })
- ->whereHas('qual', function ($query) {
- $query->where('state', TechnicianAuthStatus::PASSED->value);
- })
- ->with(['info:id,nickname,avatar,gender'])
- ->find($coachId);
- if (! $coach) {
- throw new \Exception('技师不存在');
- }
-
- $homeLocation = Redis::geopos('coach_locations', $coachId.'_'.TechnicianLocationType::COMMON->value);
- $workLocation = Redis::geopos('coach_locations', $coachId.'_'.TechnicianLocationType::CURRENT->value);
-
- if (! is_numeric($latitude) || ! is_numeric($longitude)) {
- Log::error('Invalid coordinates:', ['latitude' => $latitude, 'longitude' => $longitude]);
- throw new \Exception('无效的经纬度坐标');
- }
-
- $tempKey = 'user_temp_'.$user->id;
- Redis::geoadd('coach_locations', $longitude, $latitude, $tempKey);
-
- $distanceHome = null;
- $distanceWork = null;
- if ($homeLocation && ! empty($homeLocation[0])) {
- $distanceHome = Redis::geodist('coach_locations', $tempKey, $coachId.'_'.TechnicianLocationType::COMMON->value, 'km');
- Log::info('Home distance calculation:', [
- 'from' => $tempKey,
- 'to' => $coachId.'_'.TechnicianLocationType::COMMON->value,
- 'distance' => $distanceHome,
- 'home_location' => $homeLocation[0],
- ]);
- }
- if ($workLocation && ! empty($workLocation[0])) {
- $distanceWork = Redis::geodist('coach_locations', $tempKey, $coachId.'_'.TechnicianLocationType::CURRENT->value, 'km');
- }
-
- Redis::zrem('coach_locations', $tempKey);
-
- $distances = array_filter([$distanceHome, $distanceWork]);
- $coach->distance = ! empty($distances) ? round(min($distances), 2) : null;
- return $coach;
- }
-
- public function setCoachLocation($coachId, $latitude, $longitude, $type = TechnicianLocationType::COMMON->value)
- {
- if (! is_numeric($latitude) || ! is_numeric($longitude)) {
- Log::error('Invalid coordinates in setCoachLocation:', [
- 'coach_id' => $coachId,
- 'latitude' => $latitude,
- 'longitude' => $longitude,
- ]);
- throw new \Exception('无效的经纬度坐标');
- }
- if (! in_array($type, [TechnicianLocationType::CURRENT->value, TechnicianLocationType::COMMON->value])) {
- throw new \Exception('无效的位置类型,必须是 current 或 common');
- }
- $key = $coachId.'_'.$type;
- $result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
- Log::info('Coach location set:', [
- 'coach_id' => $coachId,
- 'type' => $type,
- 'key' => $key,
- 'latitude' => $latitude,
- 'longitude' => $longitude,
- 'result' => $result,
- ]);
-
- $location = Redis::geopos('coach_locations', $key);
- Log::info('Verify location after set:', [
- 'key' => $key,
- 'location' => $location,
- ]);
- return $result;
- }
-
- public function getSchedule(int $coachId, ?string $date = null)
- {
- try {
-
- $date = $date ? Carbon::parse($date)->format('Y-m-d') : now()->toDateString();
- $targetDate = Carbon::parse($date);
-
- $coach = CoachUser::with('info')->find($coachId);
- abort_if(! $coach, 404, '技师不存在');
- abort_if($coach->info->state != TechnicianStatus::ACTIVE->value,
- 400, '技师状态异常');
-
- abort_if($targetDate->startOfDay()->lt(now()->startOfDay()),
- 400, '不能查询过去的日期');
- abort_if($targetDate->diffInDays(now()) > 30,
- 400, '只能查询未来30天内的时间段');
- $cacheKey = "coach:timeslots:{$coachId}:{$date}";
- Cache::forget($cacheKey);
-
- return Cache::remember(
- "coach:timeslots:{$coachId}:{$date}",
- now()->addMinutes(15),
- function () use ($coachId, $date, $targetDate) {
-
- $schedule = CoachSchedule::where('coach_id', $coachId)
- ->where('state', 1)
- ->first();
- if (! $schedule || empty($schedule->time_ranges)) {
- return $this->formatResponse($date, []);
- }
- $timeRanges = json_decode($schedule->time_ranges, true);
-
- $dayOrders = $this->getDayOrders($coachId, $date);
-
- $timeSlots = $this->generateAvailableTimeSlots(
- $date,
- $timeRanges,
- $dayOrders,
- $targetDate->isToday()
- );
- return $this->formatResponse($date, $timeSlots);
- }
- );
- } catch (\Exception $e) {
- Log::error('获取可预约时间段失败', [
- 'coach_id' => $coachId,
- 'date' => $date,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- throw $e;
- }
- }
-
- private function getDayOrders(int $coachId, string $date): array
- {
- $date = Carbon::parse($data['date'] ?? $date);
- $startOfDay = $date->startOfDay()->format('Y-m-d H:i:s');
- $endOfDay = $date->endOfDay()->format('Y-m-d H:i:s');
- return Order::where('coach_id', $coachId)
- ->whereBetween('service_time', [$startOfDay, $endOfDay])
- ->whereIn('state', [
- OrderStatus::ACCEPTED->value,
- OrderStatus::DEPARTED->value,
- OrderStatus::ARRIVED->value,
- OrderStatus::SERVING->value,
- ])
- ->select(['id', 'service_start_time', 'service_end_time', 'state'])
- ->get()
- ->toArray();
- }
-
- private function generateAvailableTimeSlots(
- string $date,
- array $timeRanges,
- array $dayOrders,
- bool $isToday
- ): array {
- $timeSlots = [];
- $currentTime = now();
- foreach ($timeRanges as $range) {
- $start = Carbon::parse($date.' '.$range['start_time']);
- $end = Carbon::parse($date.' '.$range['end_time']);
-
- if ($isToday && $start->lt($currentTime)) {
- $start = $currentTime->copy()->addMinutes(30)->floorMinutes(30);
-
- if ($start->gt($end)) {
- continue;
- }
- }
-
- while ($start->lt($end)) {
- $slotStart = $start->format('H:i');
- $slotEnd = $start->copy()->addMinutes(30)->format('H:i');
-
- if (! $this->hasConflictingOrder($date, $slotStart, $slotEnd, $dayOrders)) {
- $timeSlots[] = [
- 'start_time' => $slotStart,
- 'end_time' => $slotEnd,
- 'is_available' => true,
- 'duration' => 30,
- ];
- }
- $start->addMinutes(30);
- }
- }
- return $timeSlots;
- }
-
- private function formatResponse(string $date, array $timeSlots): array
- {
- $targetDate = Carbon::parse($date);
- return [
- 'date' => $date,
- 'day_of_week' => $targetDate->isoFormat('dddd'),
- 'is_today' => $targetDate->isToday(),
- 'time_slots' => $timeSlots,
- 'total_slots' => count($timeSlots),
- 'updated_at' => now()->toDateTimeString(),
- ];
- }
-
- private function hasConflictingOrder(
- string $date,
- string $startTime,
- string $endTime,
- array $dayOrders
- ): bool {
- $slotStart = Carbon::parse("$date $startTime");
- $slotEnd = Carbon::parse("$date $endTime");
- foreach ($dayOrders as $order) {
- $orderStart = Carbon::parse($order['service_start_time']);
- $orderEnd = Carbon::parse($order['service_end_time']);
-
- if (($slotStart >= $orderStart && $slotStart < $orderEnd) ||
- ($slotEnd > $orderStart && $slotEnd <= $orderEnd) ||
- ($slotStart <= $orderStart && $slotEnd >= $orderEnd)) {
- return true;
- }
- }
- return false;
- }
-
- public function validateServiceTimeWithinCoachAvailability(int $coachId, string $serviceTime): bool
- {
- $serviceDateTime = Carbon::parse($serviceTime);
- $coachSchedule = $this->getSchedule($coachId, $serviceDateTime->format('Y-m-d'));
- foreach ($coachSchedule['time_slots'] as $slot) {
- $slotStart = Carbon::parse($serviceDateTime->format('Y-m-d').' '.$slot['start_time']);
- $slotEnd = Carbon::parse($serviceDateTime->format('Y-m-d').' '.$slot['end_time']);
- if ($serviceDateTime->between($slotStart, $slotEnd)) {
- return true;
- }
- }
- return false;
- }
- }
|