123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Services\Client;
- use App\Models\CoachUser;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Redis;
- class CoachService
- {
-
- public function getCoachList($latitude, $longitude)
- {
- $page = request()->get('page', 1);
- $perPage = request()->get('per_page', 15);
-
- $user = Auth::user();
-
- 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 = $coachData[array_search($coach->id, array_column($coachData, 'id'))]['distance'] ?? null;
- }
-
- $coaches = $coaches->sortBy('distance')->values();
- return $coaches;
- }
-
- public function getCoachDetail($coachId, $latitude, $longitude)
- {
-
- $user = Auth::user();
-
- 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);
-
- $distance = 0;
- if ($coach->locations) {
-
- }
- $coach->distance = $distance;
- return $coach;
- }
- }
|