|
@@ -62,9 +62,7 @@ class AccountService
|
|
|
*/
|
|
|
public function submitBaseInfo($user, array $data)
|
|
|
{
|
|
|
- // 开启数据库事务,确保数据一致性
|
|
|
- DB::beginTransaction();
|
|
|
- try {
|
|
|
+ return DB::transaction(function () use ($user, $data) {
|
|
|
// 验证技师信息是否存在,不存在则抛出404异常
|
|
|
abort_if(!$user->coach, 404, '技师信息不存在');
|
|
|
|
|
@@ -90,18 +88,8 @@ class AccountService
|
|
|
// 避免用户获取到旧的缓存数据
|
|
|
$this->clearCoachCache($user->coach->id);
|
|
|
|
|
|
- // 提交事务,确保所有操作成功
|
|
|
- DB::commit();
|
|
|
-
|
|
|
- // 返回成功结果
|
|
|
return ['message' => '基本信息提交成功'];
|
|
|
- } catch (\Exception $e) {
|
|
|
- // 发生异常时回滚事务,确保数据一致性
|
|
|
- DB::rollBack();
|
|
|
-
|
|
|
- // 向上抛出异常,由调用方处理
|
|
|
- throw $e;
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -431,7 +419,7 @@ class AccountService
|
|
|
*/
|
|
|
public function setLocation($coachId, $latitude, $longitude, $type = TechnicianLocationType::COMMON->value, array $locationInfo = [])
|
|
|
{
|
|
|
- // 使用事务确保数据一致性
|
|
|
+ // 使用事务确保据一致性
|
|
|
return DB::transaction(function () use ($coachId, $latitude, $longitude, $type, $locationInfo) {
|
|
|
// 验证经纬度的有效性(-90≤纬度≤90,-180≤经度≤180)
|
|
|
$this->validateCoordinates($latitude, $longitude);
|
|
@@ -463,42 +451,77 @@ class AccountService
|
|
|
/**
|
|
|
* 获取技师位置信息
|
|
|
*
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @return array 位置信息
|
|
|
+ * 业务流程:
|
|
|
+ * 1. 获取指定类型的位置记录
|
|
|
+ * 2. 返回格式化的位置数据
|
|
|
+ *
|
|
|
+ * @param MemberUser $user 认证用户
|
|
|
+ * @param int $type 位置类型 (1:当前位置 2:常用位置)
|
|
|
+ * @return array 位置信息,包含:
|
|
|
+ * - province: string 省份
|
|
|
+ * - city: string 城市
|
|
|
+ * - district: string 区县
|
|
|
+ * - address: string 详细地址
|
|
|
+ * - adcode: string 行政区划代码
|
|
|
+ * - longitude: float 经度
|
|
|
+ * - latitude: float 纬度
|
|
|
+ * - updated_at: string 更新时间
|
|
|
*/
|
|
|
- public function getLocation($userId)
|
|
|
+ public function getLocation(MemberUser $user, int $type): array
|
|
|
{
|
|
|
- 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();
|
|
|
+ // 获取指定类型的位置记录
|
|
|
+ $location = $this->getLocationByType($user->coach, $type);
|
|
|
|
|
|
- $result = [
|
|
|
- 'address' => $location ? $location->location : null,
|
|
|
- ];
|
|
|
+ // 返回格式化的位置数据
|
|
|
+ return $this->formatLocationResponse($location);
|
|
|
+ }
|
|
|
|
|
|
- // 记录日志
|
|
|
- Log::info('获取技师常用位置信息成功', [
|
|
|
- 'coach_id' => $user->coach->id,
|
|
|
- 'location' => $result,
|
|
|
- ]);
|
|
|
+ /**
|
|
|
+ * 根据类型获取技师位置记录
|
|
|
+ *
|
|
|
+ * @param CoachUser $coach 技师对象
|
|
|
+ * @param int $type 位置类型 (1:当前位置 2:常用位置)
|
|
|
+ * @return CoachLocation 位置信息
|
|
|
+ * @throws \Illuminate\Http\Exceptions\HttpResponseException 当位置信息不存在时抛出404异常
|
|
|
+ */
|
|
|
+ private function getLocationByType(CoachUser $coach, int $type): array
|
|
|
+ {
|
|
|
+ // 获取指定类型的位置记录
|
|
|
+ $location = $coach->locations()
|
|
|
+ ->where('type', $type)
|
|
|
+ ->first();
|
|
|
+ abort_if(!$location, 404, '位置信息不存在');
|
|
|
|
|
|
- return $result;
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('获取技师常用位置信息异常', [
|
|
|
- 'coach_id' => $user->coach->id ?? null,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'file' => $e->getFile(),
|
|
|
- 'line' => $e->getLine(),
|
|
|
- ]);
|
|
|
- throw $e;
|
|
|
- }
|
|
|
+ // 返回位置数据
|
|
|
+ return $location;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化位置响应数据
|
|
|
+ *
|
|
|
+ * @param CoachLocation $location 位置记录
|
|
|
+ * @return array 格式化后的位置信息,包含:
|
|
|
+ * - province: string 省份
|
|
|
+ * - city: string 城市
|
|
|
+ * - district: string 区县
|
|
|
+ * - address: string 详细地址
|
|
|
+ * - adcode: string 行政区划代码
|
|
|
+ * - longitude: float 经度
|
|
|
+ * - latitude: float 纬度
|
|
|
+ * - updated_at: string 更新时间
|
|
|
+ */
|
|
|
+ private function formatLocationResponse(CoachLocation $location): array
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ 'province' => $location->province, // 省份
|
|
|
+ 'city' => $location->city, // 城市
|
|
|
+ 'district' => $location->district, // 区县
|
|
|
+ 'address' => $location->address, // 详细地址
|
|
|
+ 'adcode' => $location->adcode, // 行政区划代码
|
|
|
+ 'longitude' => $location->longitude, // 经度
|
|
|
+ 'latitude' => $location->latitude, // 纬度
|
|
|
+ 'updated_at' => $location->updated_at->toDateTimeString(), // 更新时间
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
/**
|