ProjectService.php 3.8 KB

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