Browse Source

feat:技师端-获取技师开通项目

刘学玺 3 months ago
parent
commit
9552ce93de

+ 41 - 0
app/Http/Controllers/Coach/AccountController.php

@@ -633,4 +633,45 @@ class AccountController extends Controller
             $this->service->sendVerifyCode($request->validated())
         );
     }
+
+    /**
+     * [账户]获取已开通项目
+     *
+     * @description 获取技师已开通的服务项目列表
+     *
+     * @authenticated 需要技师身份认证
+     *
+     * @response {
+     *   "status": true,
+     *   "message": "获取成功",
+     *   "data": {
+     *     "total": 2,
+     *     "list": [
+     *       {
+     *         "id": 1,
+     *         "project_id": 100,
+     *         "project_name": "全身按摩",
+     *         "category_id": 1,
+     *         "category_name": "按摩",
+     *         "duration": 60,
+     *         "price": 200.00,
+     *         "discount_amount": 20.00,
+     *         "final_price": 180.00,
+     *         "service_gender": "all",
+     *         "service_distance": 5000,
+     *         "traffic_fee_type": "round_trip",
+     *         "traffic_fee": 10.00,
+     *         "created_at": "2024-03-16 10:00:00",
+     *         "updated_at": "2024-03-16 10:00:00"
+     *       }
+     *     ]
+     *   }
+     * }
+     */
+    public function enabledProjects()
+    {
+        return $this->success(
+            $this->service->getEnabledProjects(Auth::user()->coach->id)
+        );
+    }
 }

+ 9 - 0
app/Models/CoachProject.php

@@ -24,4 +24,13 @@ class CoachProject extends Model
     {
         return $this->belongsTo(Project::class, 'project_id', 'id');
     }
+
+    /**
+     * @Author FelixYin
+     * @description 技师开通项目所属服务项目
+     */
+    public function project()
+    {
+        return $this->belongsTo(Project::class, 'project_id', 'id');
+    }
 }

+ 18 - 0
app/Models/Project.php

@@ -77,4 +77,22 @@ class Project extends Model
             'state_text' => $stateText ?? ($state === ProjectStatus::OPEN->value ? '已开通' : '已关闭'),
         ];
     }
+
+    /**
+     * @Author FelixYin
+     * @description 项目所属分类
+     */
+    public function category()
+    {
+        return $this->belongsTo(ProjectCate::class, 'category_id', 'id');
+    }
+
+    /**
+     * @Author FelixYin
+     * @description 项目关联技师
+     */
+    public function coachProjects()
+    {
+        return $this->hasMany(CoachProject::class, 'project_id', 'id');
+    }
 }

+ 51 - 1
app/Services/Coach/AccountService.php

@@ -6,6 +6,8 @@ use App\Models\ShopInfo;
 use App\Models\CoachUser;
 use App\Enums\OrderStatus;
 use App\Models\MemberUser;
+use App\Enums\ProjectStatus;
+use App\Models\CoachProject;
 use App\Models\CoachLocation;
 use App\Models\CoachSchedule;
 use App\Models\CoachStatistic;
@@ -897,7 +899,7 @@ class AccountService
                 Redis::del($keys);
             }
         } catch (\Exception $e) {
-            Log::error('清除���间段缓存失败', [
+            Log::error('清除可预约时间段缓存失败', [
                 'coach_id' => $coachId,
                 'error' => $e->getMessage(),
             ]);
@@ -1518,4 +1520,52 @@ class AccountService
             default => '未知等级',
         };
     }
+
+    /**
+     * 获取技师已开通的项目列表
+     *
+     * 业务逻辑:
+     * 1. 获取技师开通的所有项目
+     * 2. 预加载项目基础信息
+     * 3. 格式化返回数据
+     *
+     * @param int $coachId 技师ID
+     * @return array 项目列表数据,包含:
+     *        - total: int 总数
+     *        - list: array 项目列表
+     */
+    public function getEnabledProjects(int $coachId): array
+    {
+        // 获取技师开通且启用的项目
+        $projects = CoachProject::with(['project', 'project.category'])
+            ->where('coach_id', $coachId)
+            ->where('state', ProjectStatus::OPEN->value)
+            ->get();
+
+        // 格式化项目数据
+        $list = $projects->map(function ($item) {
+            return [
+                'id' => $item->id,
+                'project_id' => $item->project_id,
+                'project_name' => $item->project?->name,
+                'category_id' => $item->project?->category_id,
+                'category_name' => $item->project?->category?->name,
+                'duration' => $item->project?->duration,          // 服务时长(分钟)
+                'price' => $item->project?->price,               // 项目价格
+                'discount_amount' => $item->discount_amount,      // 优惠金额
+                'final_price' => $item->project?->price - $item->discount_amount,  // 最终价格
+                'service_gender' => $item->service_gender,        // 服务性别
+                'service_distance' => $item->service_distance,    // 服务距离(米)
+                'traffic_fee_type' => $item->traffic_fee_type,   // 路费类型
+                'traffic_fee' => $item->traffic_fee,             // 路费金额
+                'created_at' => $item->created_at?->format('Y-m-d H:i:s'),
+                'updated_at' => $item->updated_at?->format('Y-m-d H:i:s'),
+            ];
+        })->values()->all();
+
+        return [
+            'total' => count($list),
+            'list' => $list
+        ];
+    }
 }

+ 4 - 0
routes/api.php

@@ -205,6 +205,10 @@ Route::middleware(['auth:sanctum', 'coach'])->prefix('coach')->group(function ()
         Route::post('send-code', [App\Http\Controllers\Coach\AccountController::class, 'sendVerifyCode'])
             ->middleware(['throttle:3,1'])  // 限制每分钟最多发送3次
             ->name('coach.account.send-code');
+
+        // 获取已开通项目
+        Route::get('enabled-projects', [App\Http\Controllers\Coach\AccountController::class, 'enabledProjects'])
+            ->name('coach.account.enabled-projects');
     });
 
     // 订单相关路由