ProjectService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\AgentInfo;
  4. use App\Models\CoachUser;
  5. use App\Models\Project;
  6. use App\Models\ProjectCate;
  7. class ProjectService
  8. {
  9. /**
  10. * 获取项目列表
  11. */
  12. public function getProjectList($areaCode, $projectCateId = null, $type = 'normal')
  13. {
  14. // 根据区域代码获取代理商
  15. $agent = $this->findAvailableAgent($areaCode);
  16. // 获取项目分类
  17. $projectCate = ProjectCate::find($projectCateId);
  18. // 获取项目列表
  19. if ($agent) {
  20. $agentCate = $agent->cates()->find($projectCate->id);
  21. if ($type == 'normal') {
  22. $projects = $agentCate->projects()->where('state', 'enable')->whereHas('basicInfo', fn ($q) => $q->where('type', 'normal'))->paginate(10);
  23. } else {
  24. $projects = $agentCate->projects()->where('state', 'enable')->paginate(10);
  25. }
  26. } else {
  27. if ($type == 'normal') {
  28. $projects = $projectCate?->services()->where('type', 'normal')->get();
  29. } else {
  30. $projects = $projectCate?->services;
  31. }
  32. }
  33. return $projects;
  34. }
  35. /**
  36. * 如果代理商存在,则返回代理商项目,否则返回系统项目
  37. *
  38. * @param int $projectId 项目ID
  39. * @param string $areaCode 区域代码
  40. * @return Project 项目模型
  41. * @throws \Illuminate\Http\Exceptions\HttpResponseException 项目不存在时抛出404异常
  42. */
  43. public function getProjectDetail($projectId, $areaCode)
  44. {
  45. // 查询系统项目
  46. $project = Project::where('state', 'enable')->find($projectId);
  47. abort_if(! $project, 404, '项目不存在');
  48. // 根据区域代码获取代理商
  49. $agent = $this->findAvailableAgent($areaCode);
  50. if ($agent) {
  51. // 查询代理商项目
  52. $project = $agent->projects()->where('project_id', $projectId)->first();
  53. $project->agent_id = $agent->id;
  54. }
  55. return $project;
  56. }
  57. /**
  58. * 获取技师开通的项目列表
  59. */
  60. public function getCoachProjectList($coachId, $areaCode, $projectCateId)
  61. {
  62. // 查询技师信息
  63. $coach = CoachUser::where('id', $coachId)
  64. ->where('state', 'enable')
  65. ->find($coachId);
  66. if (! $coach) {
  67. throw new \Exception('技师不存在');
  68. }
  69. $coachInfo = $coach->info;
  70. if (! $coachInfo) {
  71. throw new \Exception('技师信息不存在');
  72. }
  73. if ($coachInfo->state !== 'approved') {
  74. throw new \Exception('技师未通过审核');
  75. }
  76. $coachQual = $coach->qual;
  77. if (! $coachQual) {
  78. throw new \Exception('技师资格证书不存在');
  79. }
  80. if ($coachQual->state !== 'approved') {
  81. throw new \Exception('技师资格证书未通过审核');
  82. }
  83. $coachReal = $coach->real;
  84. if (! $coachReal) {
  85. throw new \Exception('技师实名认证记录不存在');
  86. }
  87. if ($coachReal->state !== 'approved') {
  88. throw new \Exception('技师实名认证未通过审核');
  89. }
  90. // 获取技师开通的项目ID列表
  91. $projectIds = $coach->projects()->where('state', 'enable')->pluck('project_id');
  92. // 根据区域代码获取代理商
  93. $agent = $this->findAvailableAgent($areaCode);
  94. if ($agent) {
  95. $agentCate = $agent->cates()->where('project_cate_id', $projectCateId)->first();
  96. $projectIds = $agentCate->projects()->whereIn('project_id', $projectIds)->pluck('project_id');
  97. }
  98. return $coach->projects()->with('basicInfo')->whereIn('project_id', $projectIds)->get();
  99. }
  100. /**
  101. * 递归查找可用的代理商
  102. *
  103. * @param string $areaCode 区域代码
  104. * @return Agent|null
  105. */
  106. private function findAvailableAgent($areaCode)
  107. {
  108. // 先查找当前区域的代理商
  109. $agent = AgentInfo::where('area_code', $areaCode)
  110. ->where('state', 'enable')
  111. ->first();
  112. if ($agent) {
  113. return $agent;
  114. }
  115. // 如果找不到,且区域代码长度大于2,则向上查找
  116. if (strlen($areaCode) > 2) {
  117. // 去掉最后两位,获取上级区域代码
  118. $parentAreaCode = substr($areaCode, 0, -2);
  119. return $this->findAvailableAgent($parentAreaCode);
  120. }
  121. return null;
  122. }
  123. }