123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace App\Services\Client;
- use App\Models\Project;
- use App\Models\AgentInfo;
- use App\Models\CoachUser;
- use App\Enums\AgentStatus;
- use App\Enums\ProjectType;
- use App\Models\ProjectCate;
- use App\Enums\ProjectStatus;
- use App\Enums\AgentAuditStatus;
- use App\Enums\TechnicianStatus;
- use App\Enums\TechnicianAuthStatus;
- use Illuminate\Support\Facades\Cache;
- class ProjectService
- {
-
- public function getProjectList($areaCode, $projectCateId = null, $type = ProjectType::VISIT, $perPage = 10)
- {
-
- $cacheKey = "project:list:{$areaCode}:{$projectCateId}:{$type}:{$perPage}";
- return Cache::remember($cacheKey, 3600, function () use ($areaCode, $projectCateId, $type, $perPage) {
-
- $projectCate = ProjectCate::find($projectCateId);
- abort_if(!$projectCate, 404, '项目分类不存在');
- abort_if($projectCate->state != ProjectStatus::OPEN->value(), 404, '项目分类状态异常');
-
- $agent = $this->findAvailableAgent($areaCode);
-
- return $this->getProjects($projectCate, $type, $agent, $perPage);
- });
- }
-
- public function getProjectDetail($projectId, $areaCode)
- {
-
- $project = $this->getSystemProject($projectId);
-
- $agent = $this->findAvailableAgent($areaCode);
- if ($agent) {
- $this->mergeAgentSettings($project, $agent);
- $project->agent_id = $agent->id;
- }
- return $project;
- }
-
- private function getSystemProject($projectId)
- {
- $project = Project::where('state', ProjectStatus::OPEN->value())->find($projectId);
- abort_if(!$project, 404, '项目不存在');
- return $project;
- }
-
- public function getCoachProjectList($coachId, $areaCode, $projectCateId)
- {
-
- $coach = CoachUser::where('id', $coachId)
- ->where('state', TechnicianStatus::ACTIVE->value)
- ->with(['info', 'qual', 'real'])
- ->first();
- abort_if(! $coach, 404, '技师不存在');
- abort_if(! $coach->info, 404, '技师信息不存在');
- abort_if($coach->info->state !== TechnicianAuthStatus::PASSED->value, 404, '技师未通过审核');
- abort_if(! $coach->qual, 404, '技师资格证书不存在');
- abort_if($coach->qual->state !== TechnicianAuthStatus::PASSED->value, 404, '技师资格证书未通过审核');
- abort_if(! $coach->real, 404, '技师实名认证记录不存在');
- abort_if($coach->real->state !== TechnicianAuthStatus::PASSED->value, 404, '技师实名认证未通过审核');
-
- $projectIds = $coach->projects()
- ->where('state', ProjectStatus::OPEN->value)
- ->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();
- }
-
- private function getProjects(ProjectCate $projectCate, int $type, $agent = null, int $perPage = 10)
- {
-
- $query = $projectCate->projects()
- ->where('state', ProjectStatus::OPEN->value());
-
- if ($type == ProjectType::OVERTIME->value()) {
- $query->whereIn('type', [
- ProjectType::VISIT->value(),
- ProjectType::OVERTIME->value()
- ]);
- } else {
- $query->where('type', ProjectType::VISIT->value());
- }
-
- if ($agent) {
- $agentCate = $agent->cates()
- ->where('project_cate_id', $projectCate->id)
- ->first();
-
- abort_if(
- $agentCate && ($agentCate->state != ProjectStatus::OPEN->value()),
- 404,
- '当前区域未开通服务'
- );
-
- $projects = $query->paginate($perPage);
-
- foreach ($projects as $project) {
- $this->mergeAgentSettings($project, $agent);
- }
- return $projects;
- }
-
- return $query->paginate($perPage);
- }
-
- private function findAvailableAgent($areaCode)
- {
-
- $cacheKey = "agent:area:{$areaCode}";
- return Cache::remember($cacheKey, 3600, function () use ($areaCode) {
-
- $fullAreaCode = str_pad($areaCode, 6, '0', STR_PAD_RIGHT);
- $agent = AgentInfo::where('area_code', $fullAreaCode)
- ->where('state', AgentStatus::ENABLE->value)
- ->where('audit_state', AgentAuditStatus::PASSED->value)
- ->first();
- if ($agent) {
- return $agent;
- }
- if (strlen($areaCode) > 2) {
- $parentAreaCode = substr($areaCode, 0, -2);
- return $this->findAvailableAgent($parentAreaCode);
- }
- return null;
- });
- }
-
- private function mergeAgentSettings(&$project, $agent)
- {
-
- $agentProject = $agent->projects()
- ->where('project_id', $project->id)
- ->first();
-
- if ($agentProject) {
- $project->price = $agentProject->price ?? $project->price;
- $project->duration = $agentProject->duration ?? $project->duration;
- $project->distance = $agentProject->distance ?? $project->distance;
- }
- }
- }
|