Ver Fonte

fixed:技师端-优化开通项目

刘学玺 há 4 meses atrás
pai
commit
0ac4b74dd4

+ 31 - 11
app/Http/Controllers/Coach/ProjectController.php

@@ -7,6 +7,7 @@ use App\Traits\ResponseTrait;
 use App\Http\Controllers\Controller;
 use Illuminate\Support\Facades\Auth;
 use App\Services\Coach\ProjectService;
+use App\Http\Requests\Coach\OpenProjectRequest;
 
 /**
  * @group 技师端
@@ -66,25 +67,44 @@ class ProjectController extends Controller
     /**
      * [项目]开通项目
      *
-     * @description 技师开通新的服务项目
+     * @description 技师开通新的服务项目,包含项目状态和开通资格检查
      *
-     * @authenticated
+     * 业务流程:
+     * 1. 验证项目ID
+     * 2. 调用服务层处理开通逻辑
+     * 3. 返回开通结果
+     *
+     * @authenticated 需要技师身份认证
      *
      * @bodyParam project_id integer required 项目ID Example: 1
      *
      * @response {
-     *   "message": "项目开通成功",
-     *   "project_id": 1,
-     *   "project_name": "精油推拿"
+     *   "status": true,
+     *   "message": "获取成功",
+     *   "data": {
+     *     "project_id": 1,         // 项目ID
+     *     "project_name": "精油推拿", // 项目名称
+     *     "state": 1,              // 项目状态
+     *     "state_text": "已开通"    // 状态描述
+     *   }
+     * }
+     *
+     * @response 404 {
+     *   "message": "项目不存在或已下架"
+     * }
+     * @response 422 {
+     *   "message": "已开通该项目"
+     * }
+     * @response 422 {
+     *   "message": "技师状态异常,无法开通项目"
      * }
      */
-    public function openProject(Request $request)
+    public function openProject(OpenProjectRequest $request)
     {
-        $data = $request->validate([
-            'project_id' => 'required|integer|exists:project,id',
-        ]);
-
-        return $this->success($this->service->openProject(Auth::user()->id, $data));
+        // 调用服务层处理开通逻辑并返回结果
+        return $this->success(
+            $this->service->openProject(Auth::user()->coach, $request->validated())
+        );
     }
 
     /**

+ 45 - 0
app/Http/Requests/Coach/OpenProjectRequest.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Http\Requests\Coach;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+/**
+ * 技师开通项目请求验证
+ */
+class OpenProjectRequest extends FormRequest
+{
+    /**
+     * 判断用户是否有权限进行此请求
+     */
+    public function authorize(): bool
+    {
+        return true;
+    }
+
+    /**
+     * 获取验证规则
+     *
+     * @return array<string, mixed> 验证规则数组
+     */
+    public function rules(): array
+    {
+        return [
+            'project_id' => 'required|integer|exists:project,id',
+        ];
+    }
+
+    /**
+     * 获取验证错误消息
+     *
+     * @return array<string, string> 错误消息数组
+     */
+    public function messages(): array
+    {
+        return [
+            'project_id.required' => '项目ID不能为空',
+            'project_id.integer' => '项目ID必须是整数',
+            'project_id.exists' => '项目不存在',
+        ];
+    }
+}

+ 63 - 1
app/Models/CoachUser.php

@@ -2,8 +2,21 @@
 
 namespace App\Models;
 
-use App\Enums\TechnicianStatus;
+use App\Models\Order;
+use App\Models\Wallet;
+use App\Models\ShopInfo;
+use App\Models\CoachScore;
+use App\Enums\ProjectStatus;
+use App\Models\CoachProject;
+use App\Models\OrderComment;
 use App\Traits\HasStateText;
+use App\Models\CoachLocation;
+use App\Models\CoachStatistic;
+use App\Enums\TechnicianStatus;
+use App\Models\CoachQualRecord;
+use App\Models\CoachRealRecord;
+use App\Models\OrderGrabRecord;
+use App\Models\ShopCoachService;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Slowlyo\OwlAdmin\Models\BaseModel as Model;
 
@@ -198,4 +211,53 @@ class CoachUser extends Model
     {
         return $this->hasMany(CoachRealRecord::class, 'coach_id', 'id');
     }
+
+    /**
+     * 验证技师状态是否正常
+     *
+     * @param string|null $message 自定义错误消息
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException 当技师状态异常时抛出异常
+     */
+    public function validateActiveStatus(?string $message = null): void
+    {
+        abort_if(
+            $this->state !== TechnicianStatus::ACTIVE->value,
+            422,
+            $message ?? '技师状态异常'
+        );
+    }
+
+    /**
+     * 验证技师是否已开通指定项目
+     *
+     * @param int $projectId 项目ID
+     * @param string|null $message 自定义错误消息
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException 当已开通项目时抛出异常
+     */
+    public function validateProjectNotOpened(int $projectId, ?string $message = null): void
+    {
+        $existingProject = $this->projects()
+            ->where('project_id', $projectId)
+            ->first();
+
+        abort_if($existingProject, 422, $message ?? '已开通该项目');
+    }
+
+    /**
+     * 创建技师项目关联
+     *
+     * @param int $projectId 项目ID
+     * @return CoachProject 返回创建的技师项目关联实例
+     */
+    public function createProjectRelation(int $projectId): CoachProject
+    {
+        return $this->projects()->create([
+            'project_id' => $projectId,
+            'state' => ProjectStatus::OPEN->value,
+            'discount_amount' => 0.00,    // 默认折扣金额
+            'service_gender' => 0,        // 默认服务性别(不限)
+            'service_distance' => 0,      // 默认服务距离
+            'traffic_fee_type' => 2,      // 默认交通费类型(双程)
+        ]);
+    }
 }

+ 43 - 0
app/Models/Project.php

@@ -2,6 +2,10 @@
 
 namespace App\Models;
 
+use App\Models\ProjectCate;
+use App\Models\ShopService;
+use App\Enums\ProjectStatus;
+use App\Models\CoachProject;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Slowlyo\OwlAdmin\Models\BaseModel as Model;
 
@@ -33,4 +37,43 @@ class Project extends Model
     {
         return $this->hasMany(ShopService::class, 'service_id', 'id');
     }
+
+    /**
+     * 验证项目是否存在且状态正常
+     *
+     * @param int $projectId 项目ID
+     * @param string|null $message 自定义错误消息
+     * @return self 返回项目模型实例
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException 当项目不存在或状态异常时抛出异常
+     */
+    public static function validateOpenProject(int $projectId, ?string $message = null): self
+    {
+        $project = self::where('id', $projectId)
+            ->where('state', ProjectStatus::OPEN->value)
+            ->first();
+
+        abort_if(!$project, 404, $message ?? '项目不存在或已下架');
+
+        return $project;
+    }
+
+    /**
+     * 格式化项目开通结果
+     *
+     * @param CoachProject $coachProject 技师项目关联实例
+     * @return array 格式化后的开通结果,包含:
+     *        - project_id: int 项目ID
+     *        - project_name: string 项目名称
+     *        - state: int 项目状态
+     *        - state_text: string 状态描述(已开通)
+     */
+    public function formatOpenResult(CoachProject $coachProject): array
+    {
+        return [
+            'project_id' => $this->id,
+            'project_name' => $this->title,
+            'state' => $coachProject->state,
+            'state_text' => '已开通',
+        ];
+    }
 }

+ 39 - 62
app/Services/Coach/ProjectService.php

@@ -74,70 +74,48 @@ class ProjectService
     /**
      * 技师开通项目
      *
-     * @param  int  $userId  技师用户ID
-     * @param  array  $data  开通项目数据
+     * 业务流程:
+     * 1. 验证技师状态
+     * 2. 验证项目状态
+     * 3. 检查是否已开通
+     * 4. 创建项目关联
+     * 5. 返回开通结果
+     *
+     * 注意事项:
+     * - 技师状态必须正常
+     * - 只能开通状态为开放的项目
+     * - 不能重复开通同一个项目
+     * - 使用事务确保数据一致性
+     * - 返回标准化的开通结果
+     *
+     * @param CoachUser $coach 技师对象
+     * @param array $data 开通项目数据,包含:
+     *        - project_id: int 项目ID
+     * @return array 开通结果,包含:
+     *        - project_id: int 项目ID
+     *        - project_name: string 项目名称
+     *        - state: int 项目状态
+     *        - state_text: string 状态描述(已开通)
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException 当验证失败时抛出异常
      */
-    public function openProject(int $userId, array $data): array
+    public function openProject(CoachUser $coach, array $data): array
     {
-        return DB::transaction(function () use ($userId, $data) {
-            try {
-                // 加载用户和技师信息
-                $user = MemberUser::with(['coach', 'coach.projects'])->findOrFail($userId);
-                abort_if(! $user->coach, 404, '技师信息不存在');
+        return DB::transaction(function () use ($coach, $data) {
+            // 检查技师状态是否正常
+            $coach->validateActiveStatus('技师状态异常,无法开通项目');
 
-                // 验证项目是否存在且状态正常
-                $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, '已开通该项目');
-
-                // 检查技师状态
-                abort_if($user->coach->state !== TechnicianStatus::ACTIVE->value,
-                    422, '技师状态异常,无法开通项目');
-
-                // 创建技师项目关联
-                $coachProject = $user->coach->projects()->create([
-                    'project_id' => $data['project_id'],
-                    'state' => ProjectStatus::OPEN->value,
-                    'discount_amount' => 0.00,
-                    'service_gender' => 0,
-                    'service_distance' => 0,
-                    'traffic_fee_type' => 2,
-                    'traffic_fee' => 0,
-                    'created_at' => now(),
-                    'updated_at' => now(),
-                ]);
+            // 验证项目是否存在且状态为开放
+            // 验证项目是否存在且状态正常
+            $project = Project::validateOpenProject($data['project_id']);
 
-                // 记录日志
-                Log::info('技师开通项目成功', [
-                    'user_id' => $userId,
-                    'coach_id' => $user->coach->id,
-                    'project_id' => $data['project_id'],
-                    'project_name' => $project->name,
-                ]);
+            // 检查是否已开通该项目
+            $coach->validateProjectNotOpened($data['project_id']);
 
-                return [
-                    'message' => '项目开通成功',
-                    'project_id' => $project->id,
-                    'project_name' => $project->name,
-                ];
+            // 创建技师项目关联
+            $coachProject = $coach->createProjectRelation($data['project_id']);
 
-            } catch (\Exception $e) {
-                Log::error('技师开通项目失败', [
-                    'user_id' => $userId,
-                    'data' => $data,
-                    'error' => $e->getMessage(),
-                    'file' => $e->getFile(),
-                    'line' => $e->getLine(),
-                ]);
-                throw $e;
-            }
+            // 返回格式化的开通结果
+            return $project->formatOpenResult($coachProject);
         });
     }
 
@@ -177,7 +155,7 @@ class ProjectService
                     abort_if(! $voucherSettingItem, 404, '代金卷设置不存在');
 
                     // 从项目设置中验证代金卷金额的合理范围
-                    abort_if($data['voucher'] > $voucherSettingItem->max_value, 422, '代金卷金额不能超过'.$voucherSettingItem->max_value.'元');
+                    abort_if($data['voucher'] > $voucherSettingItem->max_value, 422, '代金卷金额不能超过' . $voucherSettingItem->max_value . '元');
 
                     // 更新技师折扣金额配置
                     $user->coach->settingValues()->updateOrCreate(
@@ -194,7 +172,7 @@ class ProjectService
                     abort_if(! $trafficFeeSettingItem, 404, '路费设置不存在');
 
                     // 从项目设置中验证交通费类型的合理范围
-                    abort_if($data['traffic_fee'] > $trafficFeeSettingItem->max_value, 422, '交通费类型不能超过'.$trafficFeeSettingItem->max_value.'元');
+                    abort_if($data['traffic_fee'] > $trafficFeeSettingItem->max_value, 422, '交通费类型不能超过' . $trafficFeeSettingItem->max_value . '元');
 
                     // 更新技师路费配置
                     $user->coach->settingValues()->updateOrCreate(
@@ -211,7 +189,7 @@ class ProjectService
                     abort_if(! $customerGenderSettingItem, 404, '顾客性别设置不存在');
 
                     // 从项目设置中验证顾客性别设置的合理范围
-                    abort_if($data['gender'] > $customerGenderSettingItem->max_value, 422, '顾客性别设置不能超过'.$customerGenderSettingItem->max_value.'元');
+                    abort_if($data['gender'] > $customerGenderSettingItem->max_value, 422, '顾客性别设置不能超过' . $customerGenderSettingItem->max_value . '元');
 
                     // 更新技师顾客性别设置
                     $user->coach->settingValues()->updateOrCreate(
@@ -235,7 +213,6 @@ class ProjectService
                     'coach_id' => $user->coach->id,
                     'settings' => $data,
                 ];
-
             } catch (\Exception $e) {
                 Log::error('技师项目设置更新失败', [
                     'user_id' => $userId,