ProjectService.php 4.0 KB

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