123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace App\Services\Coach;
- use App\Enums\ProjectStatus;
- use App\Enums\TechnicianStatus;
- use App\Models\MemberUser;
- use App\Models\Project;
- use App\Models\SettingGroup;
- 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, '已开通该项目');
- // 检查技师状态
- 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(),
- ]);
- // 记录日志
- 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
- * @param array $data 项目设置数据
- * @return array
- *
- * @throws \Exception
- */
- public function setProject(int $userId, array $data)
- {
- 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;
- }
- });
- }
- }
|