Browse Source

Merge branch 'master' of ssh://gogs.yinbin.ink:30004/didong/owl-admin

felixyin 4 months ago
parent
commit
1ad5c12023

+ 29 - 17
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\SetProjectRequest;
 use App\Http\Requests\Coach\OpenProjectRequest;
 
 /**
@@ -108,34 +109,45 @@ class ProjectController extends Controller
     /**
      * [项目]项目设置
      *
-     * @description 设置技师已开通项目的相关参数
+     * @description 设置技师已开通项目的相关参数,包括代金券、顾客性别和交通费设置
      *
-     * @authenticated
+     * 业务流程:
+     * 1. 验证项目设置参数
+     * 2. 调用服务层处理设置更新
+     * 3. 返回更新结果
+     *
+     * @authenticated 需要技师身份认证
      *
      * @bodyParam project_id integer required 项目ID Example: 1
      * @bodyParam voucher numeric 代金卷金额(0-10元) Example: 5.00
      * @bodyParam gender integer 顾客性别(0:不限 1:男 2:女) Example: 0
-     * @bodyParam traffic_fee integer 路费类型(0:免费 1:单程 2:双程) Example: 2
+     * @bodyParam traffic_fee_type integer 交通费类型(0:免费 1:单程 2:双程) Example: 2
      *
      * @response {
+     *   "status": true,
      *   "message": "项目设置更新成功",
-     *   "project_id": 1,
-     *   "settings": {
-     *     "voucher": "5.00",
-     *     "gender": 0,
-     *     "traffic_fee": 2,
+     *   "data": {
+     *     "coach_id": 1,
+     *     "settings": {
+     *       "voucher": "5.00",
+     *       "gender": 0,
+     *       "traffic_fee_type": 2
+     *     }
      *   }
      * }
+     *
+     * @response 404 {
+     *   "message": "未开通该项目"
+     * }
+     * @response 422 {
+     *   "message": "代金卷金额不能超过10元"
+     * }
      */
-    public function setProject(Request $request)
+    public function setProject(SetProjectRequest $request)
     {
-        $data = $request->validate([
-            'project_id' => 'required|integer|exists:project,id',
-            'voucher' => 'nullable|numeric|min:0|max:10',
-            'gender' => 'nullable|integer|in:0,1,2',
-            'traffic_fee' => 'nullable|integer|in:0,1,2',
-        ]);
-
-        return $this->success($this->service->setProject(Auth::user()->id, $data));
+        // 调用服务层处理设置更新并返回结果
+        return $this->success(
+            $this->service->setProject(Auth::user()->coach, $request->validated())
+        );
     }
 }

+ 55 - 0
app/Http/Requests/Coach/SetProjectRequest.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace App\Http\Requests\Coach;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+/**
+ * 技师项目设置请求验证
+ */
+class SetProjectRequest extends FormRequest
+{
+    /**
+     * 判断用户是否有权限进行此请求
+     */
+    public function authorize(): bool
+    {
+        return true;
+    }
+
+    /**
+     * 获取验证规则
+     *
+     * @return array<string, mixed> 验证规则数组
+     */
+    public function rules(): array
+    {
+        return [
+            'project_id' => 'required|integer|exists:project,id',
+            'voucher' => 'nullable|numeric|min:0|max:10',
+            'gender' => 'nullable|integer|in:0,1,2',
+            'traffic_fee_type' => 'nullable|integer|in:0,1,2',
+        ];
+    }
+
+    /**
+     * 获取验证错误消息
+     *
+     * @return array<string, string> 错误消息数组
+     */
+    public function messages(): array
+    {
+        return [
+            'project_id.required' => '项目ID不能为空',
+            'project_id.integer' => '项目ID必须是整数',
+            'project_id.exists' => '项目不存在',
+            'voucher.numeric' => '代金券金额必须是数字',
+            'voucher.min' => '代金券金额不能小于0元',
+            'voucher.max' => '代金券金额不能超过10元',
+            'gender.integer' => '顾客性别设置必须是整数',
+            'gender.in' => '顾客性别设置值无效',
+            'traffic_fee_type.integer' => '交通费类型必须是整数',
+            'traffic_fee_type.in' => '交通费类型值无效',
+        ];
+    }
+}

+ 81 - 99
app/Services/Coach/ProjectService.php

@@ -140,109 +140,91 @@ class ProjectService
     }
 
     /**
-     * 设置项目
+     * 设置项目参数
      *
-     * @param  int  $userId  技师用户ID
-     * @param  array  $data  项目设置数据
-     * @return array
+     * 业务流程:
+     * 1. 验证项目开通状态
+     * 2. 更新项目设置
+     * 3. 返回更新结果
      *
-     * @throws \Exception
+     * @param CoachUser $coach 技师对象
+     * @param array $data 项目设置数据,包含:
+     *        - project_id: int 项目ID
+     *        - voucher: ?float 代金券金额(0-10元)
+     *        - gender: ?int 顾客性别设置(0:不限 1:男 2:女)
+     *        - traffic_fee_type: ?int 交通费设置(0:免费 1:单程 2:双程)
+     * @return array 更新结果,包含:
+     *        - message: string 更新结果消息
+     *        - coach_id: int 技师ID
+     *        - settings: array 更新后的设置值
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException 当项目未开通时抛出异常
      */
-    public function setProject(int $userId, array $data)
+    public function setProject(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, '技师信息不存在');
-
-                // 检查项目是否存在
-                $coachProject = $user->coach->projects()
-                    ->where('project_id', $data['project_id'])
-                    ->first();
-                abort_if(! $coachProject, 404, '未开通该项目');
-
-                // 获取全局项目设置分组
-                $projectSettingGroup = SettingGroup::where('code', 'project')->first();
-                abort_if(! $projectSettingGroup, 404, '项目设置分组不存在');
-
-                // 获取项目设置中代金卷、交通费、顾客性别设置
-                $projectSettingItems = $projectSettingGroup->items()->where('code', 'voucher')->orWhere('code', 'traffic_fee')->orWhere('code', 'gender')->get();
-
-                // 检查代金卷金额是否存在,并且是否大于0
-                if (isset($data['voucher']) && $data['voucher'] > 0) {
-                    $voucherSettingItem = $projectSettingItems->where('code', 'voucher')->first();
-                    abort_if(! $voucherSettingItem, 404, '代金卷设置不存在');
-
-                    // 从项目设置中验证代金卷金额的合理范围
-                    abort_if($data['voucher'] > $voucherSettingItem->max_value, 422, '代金卷金额不能超过' . $voucherSettingItem->max_value . '元');
-
-                    // 更新技师折扣金额配置
-                    $user->coach->settingValues()->updateOrCreate(
-                        ['item_id' => $projectSettingItems->where('code', 'voucher')->first()->id],
-                        ['value' => $data['voucher']],
-                        ['object_type' => $coachProject::class],
-                        ['object_id' => $coachProject->id],
-                    );
-                }
-
-                // 检查路费设置是否存在,并且是否大于0
-                if (isset($data['traffic_fee']) && $data['traffic_fee'] >= 0) {
-                    $trafficFeeSettingItem = $projectSettingItems->where('code', 'traffic_fee')->first();
-                    abort_if(! $trafficFeeSettingItem, 404, '路费设置不存在');
-
-                    // 从项目设置中验证交通费类型的合理范围
-                    abort_if($data['traffic_fee'] > $trafficFeeSettingItem->max_value, 422, '交通费类型不能超过' . $trafficFeeSettingItem->max_value . '元');
-
-                    // 更新技师路费配置
-                    $user->coach->settingValues()->updateOrCreate(
-                        ['item_id' => $projectSettingItems->where('code', 'traffic_fee')->first()->id],
-                        ['value' => $data['traffic_fee']],
-                        ['object_type' => $coachProject::class],
-                        ['object_id' => $coachProject->id],
-                    );
-                }
-
-                // 检查顾客性别设置是否存在,并且是否大于0
-                if (isset($data['gender']) && $data['gender'] >= 0) {
-                    $customerGenderSettingItem = $projectSettingItems->where('code', 'gender')->first();
-                    abort_if(! $customerGenderSettingItem, 404, '顾客性别设置不存在');
-
-                    // 从项目设置中验证顾客性别设置的合理范围
-                    abort_if($data['gender'] > $customerGenderSettingItem->max_value, 422, '顾客性别设置不能超过' . $customerGenderSettingItem->max_value . '元');
-
-                    // 更新技师顾客性别设置
-                    $user->coach->settingValues()->updateOrCreate(
-                        ['item_id' => $projectSettingItems->where('code', 'gender')->first()->id],
-                        ['value' => $data['gender']],
-                        ['object_type' => $coachProject::class],
-                        ['object_id' => $coachProject->id],
-                    );
-                }
-
-                // 记录日志
-                Log::info('技师项目设置更新成功', [
-                    'user_id' => $userId,
-                    'coach_id' => $user->coach->id,
-                    'project_id' => $data['project_id'],
-                    'settings' => $data,
-                ]);
-
-                return [
-                    'message' => '项目设置更新成功',
-                    'coach_id' => $user->coach->id,
-                    'settings' => $data,
-                ];
-            } catch (\Exception $e) {
-                Log::error('技师项目设置更新失败', [
-                    'user_id' => $userId,
-                    'data' => $data,
-                    'error' => $e->getMessage(),
-                    'file' => $e->getFile(),
-                    'line' => $e->getLine(),
-                ]);
-                throw $e;
-            }
+        return DB::transaction(function () use ($coach, $data) {
+            // 获取并验证已开通的项目
+            $coachProject = $this->getValidatedCoachProject($coach, $data['project_id']);
+
+            // 更新项目设置
+            $coachProject->update($this->prepareProjectSettings($data, $coachProject));
+
+            // 返回更新结果
+            return $this->formatProjectSettings($coach->id, $coachProject);
         });
     }
+
+    /**
+     * 获取并验证已开通的项目
+     *
+     * @param CoachUser $coach 技师对象
+     * @param int $projectId 项目ID
+     * @return \App\Models\CoachProject
+     * @throws \Illuminate\Http\Exceptions\HttpResponseException
+     */
+    private function getValidatedCoachProject(CoachUser $coach, int $projectId)
+    {
+        $coachProject = $coach->projects()
+            ->where('project_id', $projectId)
+            ->first();
+
+        abort_if(!$coachProject, 404, '未开通该项目');
+
+        return $coachProject;
+    }
+
+    /**
+     * 准备项目设置数据
+     *
+     * @param array $data 请求数据
+     * @param \App\Models\CoachProject $coachProject 当前项目设置
+     * @return array 更新数据
+     */
+    private function prepareProjectSettings(array $data, $coachProject): array
+    {
+        return [
+            'discount_amount' => $data['voucher'] ?? $coachProject->discount_amount,
+            'service_gender' => $data['gender'] ?? $coachProject->service_gender,
+            'traffic_fee_type' => $data['traffic_fee_type'] ?? $coachProject->traffic_fee_type,
+        ];
+    }
+
+    /**
+     * 格式化项目设置结果
+     *
+     * @param int $coachId 技师ID
+     * @param \App\Models\CoachProject $coachProject 项目设置
+     * @return array 格式化后的结果
+     */
+    private function formatProjectSettings(int $coachId, $coachProject): array
+    {
+        return [
+            'message' => '项目设置更新成功',
+            'coach_id' => $coachId,
+            'settings' => [
+                'voucher' => $coachProject->discount_amount,
+                'gender' => $coachProject->service_gender,
+                'traffic_fee_type' => $coachProject->traffic_fee_type,
+            ],
+        ];
+    }
 }