|
@@ -0,0 +1,189 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\Coach;
|
|
|
+
|
|
|
+use App\Enums\ProjectStatus;
|
|
|
+use App\Models\MemberUser;
|
|
|
+use App\Models\Project;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+
|
|
|
+class ProjectService
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 获取可开通的项目列表
|
|
|
+ *
|
|
|
+ * @param int $userId 技师用户ID
|
|
|
+ */
|
|
|
+ public function getAvailableProjects(int $userId): array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 加载用户和技师信息
|
|
|
+ $user = MemberUser::with(['coach', 'coach.projects'])->findOrFail($userId);
|
|
|
+ abort_if(! $user->coach, 404, '技师信息不存在');
|
|
|
+
|
|
|
+ // 获取所有可开通的项目
|
|
|
+ $projects = Project::where('state', ProjectStatus::OPEN->value)
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'title',
|
|
|
+ 'subtitle',
|
|
|
+ 'cover',
|
|
|
+ 'price',
|
|
|
+ 'original_price',
|
|
|
+ 'sales',
|
|
|
+ 'duration',
|
|
|
+ 'project_desc',
|
|
|
+ 'service_desc',
|
|
|
+ 'type',
|
|
|
+ ])
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ // 记录日志
|
|
|
+ Log::info('获取可开通项目列表成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'coach_id' => $user->coach->id,
|
|
|
+ 'project_count' => $projects->count(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'items' => $projects,
|
|
|
+ 'total' => $projects->count(),
|
|
|
+ ];
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('获取可开通项目列表失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'file' => $e->getFile(),
|
|
|
+ 'line' => $e->getLine(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 技师开通项目
|
|
|
+ *
|
|
|
+ * @param int $userId 技师用户ID
|
|
|
+ * @param array $data 开通项目数据
|
|
|
+ */
|
|
|
+ public function openProject(int $userId, array $data): array
|
|
|
+ {
|
|
|
+ return DB::transaction(function () use ($userId, $data) {
|
|
|
+ try {
|
|
|
+ // 加载用户和技师信息
|
|
|
+ $user = MemberUser::with(['coach', 'coach.projects'])->findOrFail($userId);
|
|
|
+ abort_if(! $user->coach, 404, '技师信息不存在');
|
|
|
+
|
|
|
+ // 验证项目是否存在且状态正常
|
|
|
+ $project = Project::where('id', $data['project_id'])
|
|
|
+ ->where('state', ProjectStatus::OPEN->value)
|
|
|
+ ->first();
|
|
|
+ abort_if(! $project, 404, '项目不存在或已下架');
|
|
|
+
|
|
|
+ // 检查是否已开通该项目
|
|
|
+ $existingProject = $user->coach->projects()
|
|
|
+ ->where('project_id', $data['project_id'])
|
|
|
+ ->first();
|
|
|
+ abort_if($existingProject, 422, '已开通该项目');
|
|
|
+
|
|
|
+ // 创建技师项目关联
|
|
|
+ $coachProject = $user->coach->projects()->create([
|
|
|
+ // 项目ID
|
|
|
+ 'project_id' => $data['project_id'],
|
|
|
+ // 项目状态
|
|
|
+ 'state' => ProjectStatus::OPEN->value,
|
|
|
+ // 优惠金额
|
|
|
+ 'discount_amount' => 0.00,
|
|
|
+ // 服务性别(0:不限 1:男 2:女)
|
|
|
+ 'service_gender' => 0,
|
|
|
+ // 服务距离(公里 0:不限)
|
|
|
+ 'service_distance' => 0,
|
|
|
+ // 收取路费(0:免费 1:单程 2:双程)
|
|
|
+ 'traffic_fee_type' => 2,
|
|
|
+ // 路费金额
|
|
|
+ 'traffic_fee' => 0,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 记录日志
|
|
|
+ Log::info('技师开通项目成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'coach_id' => $user->coach->id,
|
|
|
+ 'project_id' => $data['project_id'],
|
|
|
+ 'project_name' => $project->name,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'message' => '项目开通成功',
|
|
|
+ 'project_id' => $project->id,
|
|
|
+ 'project_name' => $project->name,
|
|
|
+ ];
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('技师开通项目失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'data' => $data,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'file' => $e->getFile(),
|
|
|
+ 'line' => $e->getLine(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取技师已开通的项目列表
|
|
|
+ *
|
|
|
+ * @param int $userId 技师用户ID
|
|
|
+ */
|
|
|
+ public function getOpenedProjects(int $userId): array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 加载用户和技师信息
|
|
|
+ $user = MemberUser::with(['coach'])->findOrFail($userId);
|
|
|
+ abort_if(! $user->coach, 404, '技师信息不存在');
|
|
|
+
|
|
|
+ // 获取已开通的项目
|
|
|
+ $projects = $user->coach->projects()
|
|
|
+ ->with('project')
|
|
|
+ ->where('state', ProjectStatus::ACTIVE->value)
|
|
|
+ ->get()
|
|
|
+ ->map(function ($coachProject) {
|
|
|
+ return [
|
|
|
+ 'id' => $coachProject->project->id,
|
|
|
+ 'name' => $coachProject->project->name,
|
|
|
+ 'cover' => $coachProject->project->cover,
|
|
|
+ 'price' => $coachProject->project->price,
|
|
|
+ 'duration' => $coachProject->project->duration,
|
|
|
+ 'discount_amount' => $coachProject->discount_amount,
|
|
|
+ 'traffic_fee' => $coachProject->traffic_fee,
|
|
|
+ 'is_round_trip' => $coachProject->is_round_trip,
|
|
|
+ 'opened_at' => $coachProject->created_at,
|
|
|
+ ];
|
|
|
+ });
|
|
|
+
|
|
|
+ // 记录日志
|
|
|
+ Log::info('获取已开通项目列表成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'coach_id' => $user->coach->id,
|
|
|
+ 'project_count' => $projects->count(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'items' => $projects,
|
|
|
+ 'total' => $projects->count(),
|
|
|
+ ];
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('获取已开通项目列表失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'file' => $e->getFile(),
|
|
|
+ 'line' => $e->getLine(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|