CoachService.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\CoachUser;
  4. use App\Models\MemberUser;
  5. use Illuminate\Support\Facades\Auth;
  6. class CoachService
  7. {
  8. /**
  9. * 获取技师列表
  10. */
  11. public function getCoachList($latitude, $longitude)
  12. {
  13. // 获取当前用户
  14. $user = Auth::user();
  15. // 检查用户状态
  16. if ($user->state !== 'enable') {
  17. throw new \Exception('用户状态异常');
  18. }
  19. // 获取附近的技师
  20. $coaches = CoachUser::query()
  21. ->where('state', 'enable')
  22. ->whereHas('info', function ($query) {
  23. $query->where('state', 'approved');
  24. })
  25. ->whereHas('real', function ($query) {
  26. $query->where('state', 'approved');
  27. })
  28. ->whereHas('qual', function ($query) {
  29. $query->where('state', 'approved');
  30. })
  31. ->with(['info:id,nickname,avatar,gender'])
  32. ->with(['locations'])
  33. ->whereHas('locations', function ($query) use ($latitude, $longitude) {
  34. $query->where('state', 'enable')
  35. ->whereRaw(
  36. '(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) < ?',
  37. [$latitude, $longitude, $latitude, 40]
  38. );
  39. })
  40. ->selectRaw(
  41. '*, (6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) as distance',
  42. [$latitude, $longitude, $latitude]
  43. )
  44. ->paginate(10);
  45. return $coaches;
  46. }
  47. /**
  48. * 获取技师详情
  49. */
  50. public function getCoachDetail($coachId, $latitude, $longitude)
  51. {
  52. // 获取当前用户
  53. $userId = Auth::id();
  54. $user = MemberUser::findOrFail($userId);
  55. // 检查用户状态
  56. if ($user->state !== 'enable') {
  57. throw new \Exception('用户状态异常');
  58. }
  59. // 获取技师信息
  60. $coach = CoachUser::where('id', $coachId)
  61. ->where('state', 'enable')
  62. ->where('auth_state', 'passed')
  63. ->with(['user:id,nickname,avatar,gender'])
  64. ->with(['location'])
  65. ->firstOrFail();
  66. // TODO: 计算距离
  67. $distance = 0;
  68. if ($coach->location) {
  69. // 实现距离计算逻辑
  70. }
  71. $coach->distance = $distance;
  72. return $coach;
  73. }
  74. }