123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace App\Services\Client;
- use App\Models\CoachUser;
- use App\Models\ShopCoachService;
- use App\Models\Project;
- use App\Models\AgentInfo;
- use Illuminate\Support\Facades\DB;
- class ProjectService
- {
- /**
- * 获取项目列表
- */
- public function getProjectList($areaCode)
- {
- // 根据区域代码获取代理商
- $agent = $this->findAvailableAgent($areaCode);
- // 获取项目列表
- if ($agent) {
- $projects = Project::where('state', 'enable')
- ->where('agent_id', $agent->id)
- ->paginate(10);
- } else {
- $projects = Project::where('state', 'enable')
- ->paginate(10);
- }
- return $projects;
- }
- /**
- * 递归查找可用的代理商
- *
- * @param string $areaCode 区域代码
- * @return Agent|null
- */
- private function findAvailableAgent($areaCode)
- {
- // 先查找当前区域的代理商
- $agent = AgentInfo::where('area_code', $areaCode)
- ->where('state', 'enable')
- ->first();
- if ($agent) {
- return $agent;
- }
- // 如果找不到,且区域代码长度大于2,则向上查找
- if (strlen($areaCode) > 2) {
- // 去掉最后两位,获取上级区域代码
- $parentAreaCode = substr($areaCode, 0, -2);
- return $this->findAvailableAgent($parentAreaCode);
- }
- return null;
- }
- /**
- * 获取项目详情
- */
- public function getProjectDetail($projectId, $areaCode)
- {
- // 根据区域代码获取代理商
- $agent = AgentInfo::where('business_area', 'like', "%{$areaCode}%")
- ->where('state', 'enable')
- ->first();
- if (!$agent) {
- throw new \Exception('该区域暂无代理商');
- }
- // 获取项目详情
- $project = Project::where('id', $projectId)
- ->where('state', 'enable')
- ->where(function($query) use ($areaCode) {
- // 根据区域地址过滤项目
- $query->where('area_code', $areaCode);
- })
- ->with([
- 'category:id,name',
- 'agent' => function($query) use ($agent) {
- $query->where('id', $agent->id);
- }
- ])
- ->firstOrFail();
- return $project;
- }
- /**
- * 获取技师开通的项目列表
- */
- public function getCoachProjectList($coachId, $areaCode)
- {
- // 查询技师信息
- $coach = CoachUser::where('id', $coachId)
- ->where('state', 'enable')
- ->where('auth_state', 'passed')
- ->firstOrFail();
- // 获取技师开通的项目ID列表
- $projectIds = ShopCoachService::where('coach_id', $coachId)
- ->where('state', 'enable')
- ->pluck('shop_service_id');
- // 查询项目列表
- $projects = Project::whereIn('id', $projectIds)
- ->where('state', 'enable')
- ->where(function($query) use ($areaCode) {
- // TODO: 根据区域地址过滤项目
- $query->where('area_code', $areaCode);
- })
- ->with([
- 'category:id,name',
- // 其他关联
- ])
- ->orderBy('sort', 'desc')
- ->paginate(10);
- return $projects;
- }
- }
|