ProjectService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. public function getProjectDetail($projectId, $areaCode)
  39. {
  40. // 根据区域代码获取代理商
  41. $agent = $this->findAvailableAgent($areaCode);
  42. // 查询系统项目
  43. $project = Project::where('state', 'enable')->find($projectId);
  44. if (! $project) {
  45. throw new \Exception('项目不存在');
  46. }
  47. if ($agent) {
  48. // 查询代理商项目
  49. $project = $agent->projects()->where('project_id', $projectId)->first();
  50. $project->agent_id = $agent->id;
  51. }
  52. return $project;
  53. }
  54. /**
  55. * 获取技师开通的项目列表
  56. */
  57. public function getCoachProjectList($coachId, $areaCode, $projectCateId)
  58. {
  59. // 查询技师信息
  60. $coach = CoachUser::where('id', $coachId)
  61. ->where('state', 'enable')
  62. ->find($coachId);
  63. if (! $coach) {
  64. throw new \Exception('技师不存在');
  65. }
  66. $coachInfo = $coach->info;
  67. if (! $coachInfo) {
  68. throw new \Exception('技师信息不存在');
  69. }
  70. if ($coachInfo->state !== 'approved') {
  71. throw new \Exception('技师未通过审核');
  72. }
  73. $coachQual = $coach->qual;
  74. if (! $coachQual) {
  75. throw new \Exception('技师资格证书不存在');
  76. }
  77. if ($coachQual->state !== 'approved') {
  78. throw new \Exception('技师资格证书未通过审核');
  79. }
  80. $coachReal = $coach->real;
  81. if (! $coachReal) {
  82. throw new \Exception('技师实名认证记录不存在');
  83. }
  84. if ($coachReal->state !== 'approved') {
  85. throw new \Exception('技师实名认证未通过审核');
  86. }
  87. // 获取技师开通的项目ID列表
  88. $projectIds = $coach->projects()->where('state', 'enable')->pluck('project_id');
  89. // 根据区域代码获取代理商
  90. $agent = $this->findAvailableAgent($areaCode);
  91. if ($agent) {
  92. $agentCate = $agent->cates()->where('project_cate_id', $projectCateId)->first();
  93. $projectIds = $agentCate->projects()->whereIn('project_id', $projectIds)->pluck('project_id');
  94. }
  95. return $coach->projects()->with('basicInfo')->whereIn('project_id', $projectIds)->get();
  96. }
  97. /**
  98. * 递归查找可用的代理商
  99. *
  100. * @param string $areaCode 区域代码
  101. * @return Agent|null
  102. */
  103. private function findAvailableAgent($areaCode)
  104. {
  105. // 先查找当前区域的代理商
  106. $agent = AgentInfo::where('area_code', $areaCode)
  107. ->where('state', 'enable')
  108. ->first();
  109. if ($agent) {
  110. return $agent;
  111. }
  112. // 如果找不到,且区域代码长度大于2,则向上查找
  113. if (strlen($areaCode) > 2) {
  114. // 去掉最后两位,获取上级区域代码
  115. $parentAreaCode = substr($areaCode, 0, -2);
  116. return $this->findAvailableAgent($parentAreaCode);
  117. }
  118. return null;
  119. }
  120. }