CoachService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\CoachUser;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Redis;
  7. class CoachService
  8. {
  9. /**
  10. * 获取技师列表
  11. */
  12. public function getNearCoachList($latitude, $longitude)
  13. {
  14. $page = request()->get('page', 1);
  15. $perPage = request()->get('per_page', 15);
  16. // 获取当前用户
  17. $user = Auth::user();
  18. Log::info('Current user and coordinates:', [
  19. 'user' => $user ? $user->id : null,
  20. 'latitude' => $latitude,
  21. 'longitude' => $longitude,
  22. ]);
  23. // 检查用户状态
  24. if (! $user) {
  25. throw new \Exception('用户未登录');
  26. }
  27. if ($user->state !== 'enable') {
  28. throw new \Exception('用户状态异常');
  29. }
  30. // 使用 Redis 的 georadius 命令获取附近的技师 ID
  31. $nearbyCoachIds = Redis::georadius('coach_locations', $longitude, $latitude, 40, 'km', ['WITHDIST']);
  32. $coachData = array_map(function ($item) {
  33. [$id, $type] = explode('_', $item[0]);
  34. return ['id' => $id, 'type' => $type, 'distance' => $item[1]];
  35. }, $nearbyCoachIds);
  36. // 提取所有的id
  37. $coachIds = array_unique(array_column($coachData, 'id'));
  38. // 分页截取 coachIds
  39. $paginatedCoachIds = array_slice($coachIds, ($page - 1) * $perPage, $perPage);
  40. // 查询数据库获取技师信息
  41. $coaches = CoachUser::query()
  42. ->whereIn('id', $paginatedCoachIds)
  43. ->whereHas('info', function ($query) {
  44. $query->where('state', 'approved');
  45. })
  46. ->whereHas('real', function ($query) {
  47. $query->where('state', 'approved');
  48. })
  49. ->whereHas('qual', function ($query) {
  50. $query->where('state', 'approved');
  51. })
  52. ->with(['info:id,nickname,avatar,gender'])
  53. ->paginate($perPage);
  54. // 遍历技师并设置距离
  55. foreach ($coaches as $coach) {
  56. $coach->distance = round($coachData[array_search($coach->id, array_column($coachData, 'id'))]['distance'] ?? null, 2);
  57. }
  58. // 按 distance 升序排序
  59. $coaches = $coaches->sortBy('distance')->values();
  60. return $coaches;
  61. }
  62. /**
  63. * 获取技师详情
  64. */
  65. public function getCoachDetail($coachId, $latitude, $longitude)
  66. {
  67. // 检查Redis连接
  68. try {
  69. $pingResult = Redis::connection()->ping();
  70. Log::info('Redis connection test:', ['ping_result' => $pingResult]);
  71. } catch (\Exception $e) {
  72. Log::error('Redis connection error:', ['error' => $e->getMessage()]);
  73. throw new \Exception('Redis连接失败:'.$e->getMessage());
  74. }
  75. // 检查Redis中的所有位置数据
  76. $allLocations = Redis::zrange('coach_locations', 0, -1, 'WITHSCORES');
  77. Log::info('All locations in Redis:', ['locations' => $allLocations]);
  78. // 获取当前用户
  79. $user = Auth::user();
  80. Log::info('Current user and coordinates:', [
  81. 'user' => $user ? $user->id : null,
  82. 'latitude' => $latitude,
  83. 'longitude' => $longitude,
  84. 'coach_id' => $coachId,
  85. ]);
  86. // 检查用户状态
  87. if (! $user) {
  88. throw new \Exception('用户未登录');
  89. }
  90. if ($user->state !== 'enable') {
  91. throw new \Exception('用户状态异常');
  92. }
  93. // 获取技师信息
  94. $coach = CoachUser::where('state', 'enable')
  95. ->whereHas('info', function ($query) {
  96. $query->where('state', 'approved');
  97. })
  98. ->whereHas('real', function ($query) {
  99. $query->where('state', 'approved');
  100. })
  101. ->whereHas('qual', function ($query) {
  102. $query->where('state', 'approved');
  103. })
  104. ->with(['info:id,nickname,avatar,gender'])
  105. ->find($coachId);
  106. if (! $coach) {
  107. throw new \Exception('技师不存在');
  108. }
  109. // 从 Redis 获取技师的 id_home 和 id_work 的经纬度
  110. $homeLocation = Redis::geopos('coach_locations', $coachId.'_home');
  111. $workLocation = Redis::geopos('coach_locations', $coachId.'_work');
  112. // 检查输入的经纬度是否有效
  113. if (! is_numeric($latitude) || ! is_numeric($longitude)) {
  114. Log::error('Invalid coordinates:', ['latitude' => $latitude, 'longitude' => $longitude]);
  115. throw new \Exception('无效的经纬度坐标');
  116. }
  117. // 临时存储用户当前位置用于计算距离
  118. $tempKey = 'user_temp_'.$user->id;
  119. Redis::geoadd('coach_locations', $longitude, $latitude, $tempKey);
  120. // 计算距离(单位:km)
  121. $distanceHome = null;
  122. $distanceWork = null;
  123. if ($homeLocation && ! empty($homeLocation[0])) {
  124. $distanceHome = Redis::geodist('coach_locations', $tempKey, $coachId.'_home', 'km');
  125. Log::info('Home distance calculation:', [
  126. 'from' => $tempKey,
  127. 'to' => $coachId.'_home',
  128. 'distance' => $distanceHome,
  129. 'home_location' => $homeLocation[0],
  130. ]);
  131. }
  132. if ($workLocation && ! empty($workLocation[0])) {
  133. $distanceWork = Redis::geodist('coach_locations', $tempKey, $coachId.'_work', 'km');
  134. }
  135. // 删除临时位置点
  136. Redis::zrem('coach_locations', $tempKey);
  137. // 选择最近的距离
  138. $distances = array_filter([$distanceHome, $distanceWork]);
  139. $coach->distance = ! empty($distances) ? round(min($distances), 2) : null;
  140. return $coach;
  141. }
  142. /**
  143. * 设置技师位置信息
  144. *
  145. * @param int $coachId 技师ID
  146. * @param float $latitude 纬度
  147. * @param float $longitude 经度
  148. * @param string $type 位置类型 (home|work)
  149. * @return bool
  150. *
  151. * @throws \Exception
  152. */
  153. public function setCoachLocation($coachId, $latitude, $longitude, $type = 'home')
  154. {
  155. if (! is_numeric($latitude) || ! is_numeric($longitude)) {
  156. Log::error('Invalid coordinates in setCoachLocation:', [
  157. 'coach_id' => $coachId,
  158. 'latitude' => $latitude,
  159. 'longitude' => $longitude,
  160. ]);
  161. throw new \Exception('无效的经纬度坐标');
  162. }
  163. if (! in_array($type, ['home', 'work'])) {
  164. throw new \Exception('无效的位置类型,必须是 home 或 work');
  165. }
  166. $key = $coachId.'_'.$type;
  167. $result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
  168. Log::info('Coach location set:', [
  169. 'coach_id' => $coachId,
  170. 'type' => $type,
  171. 'key' => $key,
  172. 'latitude' => $latitude,
  173. 'longitude' => $longitude,
  174. 'result' => $result,
  175. ]);
  176. // 验证数据是否成功写入
  177. $location = Redis::geopos('coach_locations', $key);
  178. Log::info('Verify location after set:', [
  179. 'key' => $key,
  180. 'location' => $location,
  181. ]);
  182. return $result;
  183. }
  184. }