CoachService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\CoachUser;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Support\Facades\Redis;
  6. use Illuminate\Support\Facades\Log;
  7. class CoachService
  8. {
  9. /**
  10. * 获取技师列表
  11. */
  12. public function getCoachList($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. Log::info('Coach locations from Redis:', [
  113. 'coach_id' => $coachId,
  114. 'home_location' => $homeLocation,
  115. 'work_location' => $workLocation,
  116. 'home_key' => $coachId.'_home',
  117. 'work_key' => $coachId.'_work'
  118. ]);
  119. // 检查输入的经纬度是否有效
  120. if (!is_numeric($latitude) || !is_numeric($longitude)) {
  121. Log::error('Invalid coordinates:', ['latitude' => $latitude, 'longitude' => $longitude]);
  122. throw new \Exception('无效的经纬度坐标');
  123. }
  124. // 临时存储用户当前位置用于计算距离
  125. $tempKey = 'user_temp_'.$user->id;
  126. $addResult = Redis::geoadd('coach_locations', $longitude, $latitude, $tempKey);
  127. Log::info('User location added to Redis:', [
  128. 'temp_key' => $tempKey,
  129. 'latitude' => $latitude,
  130. 'longitude' => $longitude,
  131. 'add_result' => $addResult
  132. ]);
  133. // 计算距离(单位:km)
  134. $distanceHome = null;
  135. $distanceWork = null;
  136. if ($homeLocation && !empty($homeLocation[0])) {
  137. $distanceHome = Redis::geodist('coach_locations', $tempKey, $coachId.'_home', 'km');
  138. Log::info('Home distance calculation:', [
  139. 'from' => $tempKey,
  140. 'to' => $coachId.'_home',
  141. 'distance' => $distanceHome,
  142. 'home_location' => $homeLocation[0]
  143. ]);
  144. }
  145. if ($workLocation && !empty($workLocation[0])) {
  146. $distanceWork = Redis::geodist('coach_locations', $tempKey, $coachId.'_work', 'km');
  147. Log::info('Work distance calculation:', [
  148. 'from' => $tempKey,
  149. 'to' => $coachId.'_work',
  150. 'distance' => $distanceWork,
  151. 'work_location' => $workLocation[0]
  152. ]);
  153. }
  154. Log::info('Distance calculation results:', [
  155. 'distance_home' => $distanceHome,
  156. 'distance_work' => $distanceWork,
  157. 'temp_key' => $tempKey,
  158. 'coach_id' => $coachId
  159. ]);
  160. // 删除临时位置点
  161. Redis::zrem('coach_locations', $tempKey);
  162. // 选择最近的距离
  163. $distances = array_filter([$distanceHome, $distanceWork]);
  164. $coach->distance = !empty($distances) ? round(min($distances), 2) : null;
  165. return $coach;
  166. }
  167. /**
  168. * 设置技师位置信息
  169. *
  170. * @param int $coachId 技师ID
  171. * @param float $latitude 纬度
  172. * @param float $longitude 经度
  173. * @param string $type 位置类型 (home|work)
  174. * @return bool
  175. * @throws \Exception
  176. */
  177. public function setCoachLocation($coachId, $latitude, $longitude, $type = 'home')
  178. {
  179. if (!is_numeric($latitude) || !is_numeric($longitude)) {
  180. Log::error('Invalid coordinates in setCoachLocation:', [
  181. 'coach_id' => $coachId,
  182. 'latitude' => $latitude,
  183. 'longitude' => $longitude
  184. ]);
  185. throw new \Exception('无效的经纬度坐标');
  186. }
  187. if (!in_array($type, ['home', 'work'])) {
  188. throw new \Exception('无效的位置类型,必须是 home 或 work');
  189. }
  190. $key = $coachId . '_' . $type;
  191. $result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
  192. Log::info('Coach location set:', [
  193. 'coach_id' => $coachId,
  194. 'type' => $type,
  195. 'key' => $key,
  196. 'latitude' => $latitude,
  197. 'longitude' => $longitude,
  198. 'result' => $result
  199. ]);
  200. // 验证数据是否成功写入
  201. $location = Redis::geopos('coach_locations', $key);
  202. Log::info('Verify location after set:', [
  203. 'key' => $key,
  204. 'location' => $location
  205. ]);
  206. return $result;
  207. }
  208. }