|
@@ -3,9 +3,12 @@
|
|
|
namespace App\Services\Coach;
|
|
|
|
|
|
use App\Enums\TechnicianAuthStatus;
|
|
|
+use App\Enums\TechnicianLocationType;
|
|
|
+use App\Models\MemberUser;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
class AccountService
|
|
|
{
|
|
@@ -297,4 +300,113 @@ class AccountService
|
|
|
{
|
|
|
Cache::forget(self::CACHE_KEY_PREFIX.$coachId);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置定位信息
|
|
|
+ *
|
|
|
+ * @param int $coachId 技师ID
|
|
|
+ * @param float $latitude 纬度
|
|
|
+ * @param float $longitude 经度
|
|
|
+ * @param int $type 位置类型 (current:1|common:2)
|
|
|
+ * @return bool
|
|
|
+ *
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function setLocation($coachId, $latitude, $longitude, $type = TechnicianLocationType::COMMON->value)
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ // 验证经纬度参数
|
|
|
+ if (! is_numeric($latitude) || ! is_numeric($longitude)) {
|
|
|
+ throw new \Exception('无效的经纬度坐标');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证位置类型
|
|
|
+ if (! in_array($type, [TechnicianLocationType::CURRENT->value, TechnicianLocationType::COMMON->value])) {
|
|
|
+ throw new \Exception('无效的位置类型');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成Redis键
|
|
|
+ $key = $coachId.'_'.$type;
|
|
|
+
|
|
|
+ // 将位置信息写入Redis
|
|
|
+ $result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
|
|
|
+
|
|
|
+ // 同时写入数据库保存历史记录
|
|
|
+ DB::table('coach_locations')->updateOrInsert(
|
|
|
+ ['coach_id' => $coachId, 'type' => $type],
|
|
|
+ [
|
|
|
+ 'latitude' => $latitude,
|
|
|
+ 'longitude' => $longitude,
|
|
|
+ 'updated_at' => now(),
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+
|
|
|
+ Log::info('技师位置信息设置成功', [
|
|
|
+ 'coach_id' => $coachId,
|
|
|
+ 'type' => $type,
|
|
|
+ 'latitude' => $latitude,
|
|
|
+ 'longitude' => $longitude,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return $result;
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('技师位置信息设置异常', [
|
|
|
+ 'coach_id' => $coachId,
|
|
|
+ 'latitude' => $latitude,
|
|
|
+ 'longitude' => $longitude,
|
|
|
+ 'type' => $type,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'file' => $e->getFile(),
|
|
|
+ 'line' => $e->getLine(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取技师位置信息
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @return array 位置信息
|
|
|
+ */
|
|
|
+ public function getLocation($userId)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 改进:直接使用 coach 模型
|
|
|
+ $user = MemberUser::find($userId);
|
|
|
+ abort_if(! $user, 404, '用户不存在');
|
|
|
+ abort_if(! $user->coach, 404, '技师信息不存在');
|
|
|
+
|
|
|
+ // 获取常用位置信息
|
|
|
+ $location = $user->coach->locations()
|
|
|
+ ->where('type', TechnicianLocationType::COMMON->value)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $result = [
|
|
|
+ 'address' => $location ? $location->location : null,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 记录日志
|
|
|
+ Log::info('获取技师常用位置信息成功', [
|
|
|
+ 'coach_id' => $user->coach->id,
|
|
|
+ 'location' => $result,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return $result;
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('获取技师常用位置信息异常', [
|
|
|
+ 'coach_id' => $user->coach->id ?? null,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'file' => $e->getFile(),
|
|
|
+ 'line' => $e->getLine(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|