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
- {
-
- 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;
- }
- }
-
- 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;
- }
- });
- }
-
- 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();
-
- 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],
- );
- }
-
- 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],
- );
- }
-
- 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;
- }
- });
- }
- }
|