123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace App\Services\Client;
- use App\Models\CoachUser;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Log;
- class CoachService
- {
-
- public function getCoachList($latitude, $longitude)
- {
- $page = request()->get('page', 1);
- $perPage = request()->get('per_page', 15);
-
- $user = Auth::user();
- Log::info('Current user and coordinates:', [
- 'user' => $user ? $user->id : null,
- 'latitude' => $latitude,
- 'longitude' => $longitude,
- ]);
-
- if (!$user) {
- throw new \Exception('用户未登录');
- }
- if ($user->state !== 'enable') {
- 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', 'approved');
- })
- ->whereHas('real', function ($query) {
- $query->where('state', 'approved');
- })
- ->whereHas('qual', function ($query) {
- $query->where('state', 'approved');
- })
- ->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 !== 'enable') {
- throw new \Exception('用户状态异常');
- }
-
- $coach = CoachUser::where('state', 'enable')
- ->whereHas('info', function ($query) {
- $query->where('state', 'approved');
- })
- ->whereHas('real', function ($query) {
- $query->where('state', 'approved');
- })
- ->whereHas('qual', function ($query) {
- $query->where('state', 'approved');
- })
- ->with(['info:id,nickname,avatar,gender'])
- ->find($coachId);
- if (! $coach) {
- throw new \Exception('技师不存在');
- }
-
- $homeLocation = Redis::geopos('coach_locations', $coachId.'_home');
- $workLocation = Redis::geopos('coach_locations', $coachId.'_work');
- Log::info('Coach locations from Redis:', [
- 'coach_id' => $coachId,
- 'home_location' => $homeLocation,
- 'work_location' => $workLocation,
- 'home_key' => $coachId.'_home',
- 'work_key' => $coachId.'_work'
- ]);
-
- if (!is_numeric($latitude) || !is_numeric($longitude)) {
- Log::error('Invalid coordinates:', ['latitude' => $latitude, 'longitude' => $longitude]);
- throw new \Exception('无效的经纬度坐标');
- }
-
- $tempKey = 'user_temp_'.$user->id;
- $addResult = Redis::geoadd('coach_locations', $longitude, $latitude, $tempKey);
- Log::info('User location added to Redis:', [
- 'temp_key' => $tempKey,
- 'latitude' => $latitude,
- 'longitude' => $longitude,
- 'add_result' => $addResult
- ]);
-
- $distanceHome = null;
- $distanceWork = null;
- if ($homeLocation && !empty($homeLocation[0])) {
- $distanceHome = Redis::geodist('coach_locations', $tempKey, $coachId.'_home', 'km');
- Log::info('Home distance calculation:', [
- 'from' => $tempKey,
- 'to' => $coachId.'_home',
- 'distance' => $distanceHome,
- 'home_location' => $homeLocation[0]
- ]);
- }
- if ($workLocation && !empty($workLocation[0])) {
- $distanceWork = Redis::geodist('coach_locations', $tempKey, $coachId.'_work', 'km');
- Log::info('Work distance calculation:', [
- 'from' => $tempKey,
- 'to' => $coachId.'_work',
- 'distance' => $distanceWork,
- 'work_location' => $workLocation[0]
- ]);
- }
- Log::info('Distance calculation results:', [
- 'distance_home' => $distanceHome,
- 'distance_work' => $distanceWork,
- 'temp_key' => $tempKey,
- 'coach_id' => $coachId
- ]);
-
- 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 = 'home')
- {
- 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, ['home', 'work'])) {
- throw new \Exception('无效的位置类型,必须是 home 或 work');
- }
- $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;
- }
- }
|