|
@@ -2,49 +2,90 @@
|
|
|
|
|
|
namespace App\Services\Client;
|
|
|
|
|
|
+use App\Enums\ProjectStatus;
|
|
|
+use App\Enums\ProjectType;
|
|
|
use App\Models\AgentInfo;
|
|
|
use App\Models\CoachUser;
|
|
|
use App\Models\Project;
|
|
|
use App\Models\ProjectCate;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class ProjectService
|
|
|
{
|
|
|
/**
|
|
|
* 获取项目列表
|
|
|
*/
|
|
|
- public function getProjectList($areaCode, $projectCateId = null, $type = 'normal')
|
|
|
+ public function getProjectList($areaCode, $projectCateId = null, $type = ProjectType::VISIT)
|
|
|
{
|
|
|
- // 根据区域代码获取代理商
|
|
|
- $agent = $this->findAvailableAgent($areaCode);
|
|
|
-
|
|
|
- // 获取项目分类
|
|
|
- $projectCate = ProjectCate::find($projectCateId);
|
|
|
- // 获取项目列表
|
|
|
- if ($agent) {
|
|
|
- $agentCate = $agent->cates()->find($projectCate->id);
|
|
|
- if ($type == 'normal') {
|
|
|
- $projects = $agentCate->projects()->where('state', 'enable')->whereHas('basicInfo', fn ($q) => $q->where('type', 'normal'))->paginate(10);
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 获取项目分类
|
|
|
+ $projectCate = ProjectCate::find($projectCateId);
|
|
|
+
|
|
|
+ abort_if(! $projectCate, 404, '项目分类不存在');
|
|
|
+ abort_if($projectCate->state != ProjectStatus::OPEN->value(), 404, '项目分类状态异常');
|
|
|
+
|
|
|
+ // 根据区域代码获取代理商
|
|
|
+ $agent = $this->findAvailableAgent($areaCode);
|
|
|
+
|
|
|
+ // 获取项目列表
|
|
|
+ if ($agent) {
|
|
|
+ $agentCate = $agent->cates()->where('project_cate_id', $projectCate->id)->first();
|
|
|
+ abort_if(! $agentCate || ($agentCate->state != ProjectStatus::OPEN->value()), 404, '当前区域未开通服务');
|
|
|
+
|
|
|
+ if (in_array($type, [ProjectType::VISIT->value()])) {
|
|
|
+ $projects = $agentCate->projects()
|
|
|
+ ->where('state', ProjectStatus::OPEN->value())
|
|
|
+ ->with('basicInfo')
|
|
|
+ ->whereHas('basicInfo', fn ($q) => $q->where('type', [ProjectType::VISIT->value()]))
|
|
|
+ ->paginate(10);
|
|
|
+ // 遍历项目,替换成代理商设置
|
|
|
+
|
|
|
+ }
|
|
|
+ if (in_array($type, [ProjectType::OVERTIME->value()])) {
|
|
|
+
|
|
|
+ $projects = $agentCate->projects()
|
|
|
+ ->where('state', ProjectStatus::OPEN->value())
|
|
|
+ ->with('basicInfo')
|
|
|
+ ->whereHas('basicInfo', fn ($q) => $q->whereIn('type', [ProjectType::VISIT->value(), ProjectType::OVERTIME->value()]))
|
|
|
+ ->paginate(10);
|
|
|
+ // 遍历项目,替换成代理商设置
|
|
|
+ }
|
|
|
} else {
|
|
|
- $projects = $agentCate->projects()->where('state', 'enable')->paginate(10);
|
|
|
+ if (in_array($type, [ProjectType::VISIT->value()])) {
|
|
|
+ $projects = $projectCate?->projects()
|
|
|
+ ->whereIn('type', [ProjectType::VISIT->value()])
|
|
|
+ ->where('state', ProjectStatus::OPEN->value())
|
|
|
+ ->paginate(10);
|
|
|
+ }
|
|
|
+ if (in_array($type, [ProjectType::OVERTIME->value()])) {
|
|
|
+ $projects = $projectCate?->projects()
|
|
|
+ ->whereIn('type', [ProjectType::VISIT->value(), ProjectType::OVERTIME->value()])
|
|
|
+ ->where('state', ProjectStatus::OPEN->value())
|
|
|
+ ->paginate(10);
|
|
|
+ }
|
|
|
}
|
|
|
- } else {
|
|
|
|
|
|
- if ($type == 'normal') {
|
|
|
- $projects = $projectCate?->services()->where('type', 'normal')->get();
|
|
|
- } else {
|
|
|
- $projects = $projectCate?->services;
|
|
|
- }
|
|
|
- }
|
|
|
+ return $projects;
|
|
|
|
|
|
- return $projects;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('获取项目列表失败', [
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'areaCode' => $areaCode,
|
|
|
+ 'projectCateId' => $projectCateId,
|
|
|
+ 'type' => $type,
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 如果代理商存在,则返回代理商项目,否则返回系统项目
|
|
|
*
|
|
|
- * @param int $projectId 项目ID
|
|
|
- * @param string $areaCode 区域代码
|
|
|
+ * @param int $projectId 项目ID
|
|
|
+ * @param string $areaCode 区域代码
|
|
|
* @return Project 项目模型
|
|
|
+ *
|
|
|
* @throws \Illuminate\Http\Exceptions\HttpResponseException 项目不存在时抛出404异常
|
|
|
*/
|
|
|
public function getProjectDetail($projectId, $areaCode)
|
|
@@ -53,11 +94,18 @@ class ProjectService
|
|
|
$project = Project::where('state', 'enable')->find($projectId);
|
|
|
abort_if(! $project, 404, '项目不存在');
|
|
|
|
|
|
- // 根据区域代码获取代理商
|
|
|
+ // 根据区域代码获取代理商
|
|
|
$agent = $this->findAvailableAgent($areaCode);
|
|
|
if ($agent) {
|
|
|
// 查询代理商项目
|
|
|
- $project = $agent->projects()->where('project_id', $projectId)->first();
|
|
|
+ $agentProject = $agent->projects()->where('project_id', $projectId)->first();
|
|
|
+ // 遍历代理商项目,替换系统项目
|
|
|
+ if ($agentProject) {
|
|
|
+ // 合并代理商项目的金额、时长、距离到系统项目
|
|
|
+ $project->price = $agentProject->price ?? $project->price; // 金额
|
|
|
+ $project->duration = $agentProject->duration ?? $project->duration; // 时长
|
|
|
+ $project->distance = $agentProject->distance ?? $project->distance; // 距离
|
|
|
+ }
|
|
|
$project->agent_id = $agent->id;
|
|
|
}
|
|
|
|
|
@@ -69,55 +117,53 @@ class ProjectService
|
|
|
*/
|
|
|
public function getCoachProjectList($coachId, $areaCode, $projectCateId)
|
|
|
{
|
|
|
- // 查询技师信息
|
|
|
- $coach = CoachUser::where('id', $coachId)
|
|
|
- ->where('state', 'enable')
|
|
|
- ->find($coachId);
|
|
|
-
|
|
|
- if (! $coach) {
|
|
|
- throw new \Exception('技师不存在');
|
|
|
- }
|
|
|
-
|
|
|
- $coachInfo = $coach->info;
|
|
|
- if (! $coachInfo) {
|
|
|
- throw new \Exception('技师信息不存在');
|
|
|
- }
|
|
|
-
|
|
|
- if ($coachInfo->state !== 'approved') {
|
|
|
- throw new \Exception('技师未通过审核');
|
|
|
- }
|
|
|
-
|
|
|
- $coachQual = $coach->qual;
|
|
|
- if (! $coachQual) {
|
|
|
- throw new \Exception('技师资格证书不存在');
|
|
|
- }
|
|
|
-
|
|
|
- if ($coachQual->state !== 'approved') {
|
|
|
- throw new \Exception('技师资格证书未通过审核');
|
|
|
- }
|
|
|
-
|
|
|
- $coachReal = $coach->real;
|
|
|
-
|
|
|
- if (! $coachReal) {
|
|
|
- throw new \Exception('技师实名认证记录不存在');
|
|
|
- }
|
|
|
-
|
|
|
- if ($coachReal->state !== 'approved') {
|
|
|
- throw new \Exception('技师实名认证未通过审核');
|
|
|
- }
|
|
|
-
|
|
|
- // 获取技师开通的项目ID列表
|
|
|
- $projectIds = $coach->projects()->where('state', 'enable')->pluck('project_id');
|
|
|
-
|
|
|
- // 根据区域代码获取代理商
|
|
|
- $agent = $this->findAvailableAgent($areaCode);
|
|
|
+ try {
|
|
|
+ // 查询技师信息
|
|
|
+ $coach = CoachUser::where('id', $coachId)
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->with(['info', 'qual', 'real'])
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ abort_if(! $coach, 404, '技师不存在');
|
|
|
+ abort_if(! $coach->info, 404, '技师信息不存在');
|
|
|
+ abort_if($coach->info->state !== 'approved', 404, '技师未通过审核');
|
|
|
+ abort_if(! $coach->qual, 404, '技师资格证书不存在');
|
|
|
+ abort_if($coach->qual->state !== 'approved', 404, '技师资格证书未通过审核');
|
|
|
+ abort_if(! $coach->real, 404, '技师实名认证记录不存在');
|
|
|
+ abort_if($coach->real->state !== 'approved', 404, '技师实名认证未通过审核');
|
|
|
+
|
|
|
+ // 获取技师开通的项目ID列表
|
|
|
+ $projectIds = $coach->projects()
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->pluck('project_id');
|
|
|
+
|
|
|
+ // 根据区域代码获取代理商
|
|
|
+ $agent = $this->findAvailableAgent($areaCode);
|
|
|
+
|
|
|
+ if ($agent) {
|
|
|
+ $agentCate = $agent->cates()
|
|
|
+ ->where('project_cate_id', $projectCateId)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $projectIds = $agentCate->projects()
|
|
|
+ ->whereIn('project_id', $projectIds)
|
|
|
+ ->pluck('project_id');
|
|
|
+ }
|
|
|
|
|
|
- if ($agent) {
|
|
|
- $agentCate = $agent->cates()->where('project_cate_id', $projectCateId)->first();
|
|
|
- $projectIds = $agentCate->projects()->whereIn('project_id', $projectIds)->pluck('project_id');
|
|
|
+ return $coach->projects()
|
|
|
+ ->with('basicInfo')
|
|
|
+ ->whereIn('project_id', $projectIds)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('获取技师项目列表失败', [
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'coachId' => $coachId,
|
|
|
+ 'areaCode' => $areaCode,
|
|
|
+ 'projectCateId' => $projectCateId,
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
}
|
|
|
-
|
|
|
- return $coach->projects()->with('basicInfo')->whereIn('project_id', $projectIds)->get();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -129,7 +175,9 @@ class ProjectService
|
|
|
private function findAvailableAgent($areaCode)
|
|
|
{
|
|
|
// 先查找当前区域的代理商
|
|
|
- $agent = AgentInfo::where('area_code', $areaCode)
|
|
|
+ // 区域代码不足6位,右边补0
|
|
|
+ $fullAreaCode = str_pad($areaCode, 6, '0', STR_PAD_RIGHT);
|
|
|
+ $agent = AgentInfo::where('area_code', $fullAreaCode)
|
|
|
->where('state', 'enable')
|
|
|
->first();
|
|
|
|