state != ProjectStatus::OPEN->value(), 404, '项目分类状态异常'); // 根据区域代码获取代理商 $agent = $this->findAvailableAgent($areaCode); // 获取项目列表 if ($agent) { $agentCate = $agent->cates()->where('project_cate_id', $projectCate->id)->first(); abort_if(! $agentCate || ($agentCate->state != ProjectStatus::OPEN->value()), 404, '当前区域未开通服务'); if (in_array($type, [ProjectType::VISIT->value()])) { $projects = $agentCate->projects() ->where('state', ProjectStatus::OPEN->value()) ->with('basicInfo') ->whereHas('basicInfo', fn ($q) => $q->where('type', [ProjectType::VISIT->value()])) ->paginate(10); // 遍历项目,替换成代理商设置 } if (in_array($type, [ProjectType::OVERTIME->value()])) { $projects = $agentCate->projects() ->where('state', ProjectStatus::OPEN->value()) ->with('basicInfo') ->whereHas('basicInfo', fn ($q) => $q->whereIn('type', [ProjectType::VISIT->value(), ProjectType::OVERTIME->value()])) ->paginate(10); // 遍历项目,替换成代理商设置 } } else { if (in_array($type, [ProjectType::VISIT->value()])) { $projects = $projectCate?->projects() ->whereIn('type', [ProjectType::VISIT->value()]) ->where('state', ProjectStatus::OPEN->value()) ->paginate(10); } if (in_array($type, [ProjectType::OVERTIME->value()])) { $projects = $projectCate?->projects() ->whereIn('type', [ProjectType::VISIT->value(), ProjectType::OVERTIME->value()]) ->where('state', ProjectStatus::OPEN->value()) ->paginate(10); } } return $projects; } catch (\Exception $e) { Log::error('获取项目列表失败', [ 'error' => $e->getMessage(), 'areaCode' => $areaCode, 'projectCateId' => $projectCateId, 'type' => $type, ]); throw $e; } } /** * 如果代理商存在,则返回代理商项目,否则返回系统项目 * * @param int $projectId 项目ID * @param string $areaCode 区域代码 * @return Project 项目模型 * * @throws \Illuminate\Http\Exceptions\HttpResponseException 项目不存在时抛出404异常 */ public function getProjectDetail($projectId, $areaCode) { // 查询系统项目 $project = Project::where('state', ProjectStatus::OPEN->value())->find($projectId); abort_if(! $project, 404, '项目不存在'); // 根据区域代码获取代理商 $agent = $this->findAvailableAgent($areaCode); if ($agent) { // 查询代理商项目 $agentProject = $agent->projects()->where('project_id', $projectId)->first(); // 遍历代理商项目,替换系统项目 if ($agentProject) { // 合并代理商项目的金额、时长、距离到系统项目 $project->price = $agentProject->price ?? $project->price; // 金额 $project->duration = $agentProject->duration ?? $project->duration; // 时长 $project->distance = $agentProject->distance ?? $project->distance; // 距离 } $project->agent_id = $agent->id; } return $project; } /** * 获取技师开通的项目列表 */ public function getCoachProjectList($coachId, $areaCode, $projectCateId) { try { // 查询技师信息 $coach = CoachUser::where('id', $coachId) ->where('state', 'enable') ->with(['info', 'qual', 'real']) ->first(); abort_if(! $coach, 404, '技师不存在'); abort_if(! $coach->info, 404, '技师信息不存在'); abort_if($coach->info->state !== 'approved', 404, '技师未通过审核'); abort_if(! $coach->qual, 404, '技师资格证书不存在'); abort_if($coach->qual->state !== 'approved', 404, '技师资格证书未通过审核'); abort_if(! $coach->real, 404, '技师实名认证记录不存在'); abort_if($coach->real->state !== 'approved', 404, '技师实名认证未通过审核'); // 获取技师开通的项目ID列表 $projectIds = $coach->projects() ->where('state', 'enable') ->pluck('project_id'); // 根据区域代码获取代理商 $agent = $this->findAvailableAgent($areaCode); if ($agent) { $agentCate = $agent->cates() ->where('project_cate_id', $projectCateId) ->first(); $projectIds = $agentCate->projects() ->whereIn('project_id', $projectIds) ->pluck('project_id'); } return $coach->projects() ->with('basicInfo') ->whereIn('project_id', $projectIds) ->get(); } catch (\Exception $e) { Log::error('获取技师项目列表失败', [ 'error' => $e->getMessage(), 'coachId' => $coachId, 'areaCode' => $areaCode, 'projectCateId' => $projectCateId, ]); throw $e; } } /** * 递归查找可用的代理商 * * @param string $areaCode 区域代码 * @return Agent|null */ private function findAvailableAgent($areaCode) { // 先查找当前区域的代理商 // 区域代码不足6位,右边补0 $fullAreaCode = str_pad($areaCode, 6, '0', STR_PAD_RIGHT); $agent = AgentInfo::where('area_code', $fullAreaCode) ->where('state', 'enable') ->first(); if ($agent) { return $agent; } // 如果找不到,且区域代码长度大于2,则向上查找 if (strlen($areaCode) > 2) { // 去掉最后两位,获取上级区域代码 $parentAreaCode = substr($areaCode, 0, -2); return $this->findAvailableAgent($parentAreaCode); } return null; } }