ProjectService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\CoachUser;
  4. use App\Models\ShopCoachService;
  5. use App\Models\Project;
  6. use App\Models\AgentInfo;
  7. use Illuminate\Support\Facades\DB;
  8. class ProjectService
  9. {
  10. /**
  11. * 获取项目列表
  12. */
  13. public function getProjectList($areaCode)
  14. {
  15. // 根据区域代码获取代理商
  16. $agent = $this->findAvailableAgent($areaCode);
  17. // 获取项目列表
  18. if ($agent) {
  19. $projects = Project::where('state', 'enable')
  20. ->where('agent_id', $agent->id)
  21. ->paginate(10);
  22. } else {
  23. $projects = Project::where('state', 'enable')
  24. ->paginate(10);
  25. }
  26. return $projects;
  27. }
  28. /**
  29. * 递归查找可用的代理商
  30. *
  31. * @param string $areaCode 区域代码
  32. * @return Agent|null
  33. */
  34. private function findAvailableAgent($areaCode)
  35. {
  36. // 先查找当前区域的代理商
  37. $agent = AgentInfo::where('area_code', $areaCode)
  38. ->where('state', 'enable')
  39. ->first();
  40. if ($agent) {
  41. return $agent;
  42. }
  43. // 如果找不到,且区域代码长度大于2,则向上查找
  44. if (strlen($areaCode) > 2) {
  45. // 去掉最后两位,获取上级区域代码
  46. $parentAreaCode = substr($areaCode, 0, -2);
  47. return $this->findAvailableAgent($parentAreaCode);
  48. }
  49. return null;
  50. }
  51. /**
  52. * 获取项目详情
  53. */
  54. public function getProjectDetail($projectId, $areaCode)
  55. {
  56. // 根据区域代码获取代理商
  57. $agent = AgentInfo::where('business_area', 'like', "%{$areaCode}%")
  58. ->where('state', 'enable')
  59. ->first();
  60. if (!$agent) {
  61. throw new \Exception('该区域暂无代理商');
  62. }
  63. // 获取项目详情
  64. $project = Project::where('id', $projectId)
  65. ->where('state', 'enable')
  66. ->where(function($query) use ($areaCode) {
  67. // 根据区域地址过滤项目
  68. $query->where('area_code', $areaCode);
  69. })
  70. ->with([
  71. 'category:id,name',
  72. 'agent' => function($query) use ($agent) {
  73. $query->where('id', $agent->id);
  74. }
  75. ])
  76. ->firstOrFail();
  77. return $project;
  78. }
  79. /**
  80. * 获取技师开通的项目列表
  81. */
  82. public function getCoachProjectList($coachId, $areaCode)
  83. {
  84. // 查询技师信息
  85. $coach = CoachUser::where('id', $coachId)
  86. ->where('state', 'enable')
  87. ->where('auth_state', 'passed')
  88. ->firstOrFail();
  89. // 获取技师开通的项目ID列表
  90. $projectIds = ShopCoachService::where('coach_id', $coachId)
  91. ->where('state', 'enable')
  92. ->pluck('shop_service_id');
  93. // 查询项目列表
  94. $projects = Project::whereIn('id', $projectIds)
  95. ->where('state', 'enable')
  96. ->where(function($query) use ($areaCode) {
  97. // TODO: 根据区域地址过滤项目
  98. $query->where('area_code', $areaCode);
  99. })
  100. ->with([
  101. 'category:id,name',
  102. // 其他关联
  103. ])
  104. ->orderBy('sort', 'desc')
  105. ->paginate(10);
  106. return $projects;
  107. }
  108. }