CoachService.php 7.7 KB

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