123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace App\Services\Client\Traits;
- use App\Enums\OrderStatus;
- use App\Enums\TechnicianAuthStatus;
- use App\Enums\TechnicianStatus;
- use App\Models\CoachSchedule;
- use App\Models\CoachUser;
- use App\Models\Order;
- use Carbon\Carbon;
- trait ValidatesServiceTime
- {
-
- private function validateServiceTimeParams(int $coachId, string $serviceTime): void
- {
-
- $coach = CoachUser::query()
- ->with(['info'])
- ->where('id', $coachId)
- ->where('state', TechnicianStatus::ACTIVE->value)
- ->first();
- abort_if(! $coach, 400, '技师不存在或未激活');
-
- abort_if(! $coach->info || $coach->info->state !== TechnicianAuthStatus::PASSED->value,
- 400, '技师未通过认证');
-
- $this->validateBasicServiceTime($serviceTime);
-
- $workSchedule = $this->getCoachWorkSchedule($coachId);
- $this->validateWorkingHours($serviceTime, $workSchedule);
-
- $this->checkTimeConflicts($coachId, $serviceTime);
- }
-
- private function validateBasicServiceTime(string $serviceTime): void
- {
- $serviceDateTime = Carbon::parse($serviceTime);
-
- abort_if(
- $serviceDateTime->isPast(),
- 400,
- '服务时间不能早于当前时间'
- );
-
- $maxAdvanceDays = config('business.max_advance_days', 7);
- abort_if(
- $serviceDateTime->diffInDays(now()) > $maxAdvanceDays,
- 400,
- "最多只能提前{$maxAdvanceDays}天预约"
- );
- }
-
- private function getCoachWorkSchedule(int $coachId): array
- {
- $schedule = CoachSchedule::where('coach_id', $coachId)
- ->where('state', 1)
- ->first();
- if (! $schedule || empty($schedule->time_ranges)) {
- return [
- 'work_days' => range(1, 7),
- 'work_hours' => [
- 'start' => '09:00',
- 'end' => '21:00',
- ],
- 'rest_dates' => [],
- ];
- }
- $timeRanges = json_decode($schedule->time_ranges, true);
- return [
- 'work_days' => range(1, 7),
- 'work_hours' => [
- 'start' => $timeRanges[0]['start_time'],
- 'end' => end($timeRanges)['end_time'],
- ],
- 'rest_dates' => [],
- ];
- }
-
- private function validateWorkingHours(string $serviceTime, array $workSchedule): void
- {
- $serviceDateTime = Carbon::parse($serviceTime);
-
- $dayOfWeek = $serviceDateTime->dayOfWeek;
- $workDays = $workSchedule['work_days'] ?? range(1, 7);
- abort_if(
- ! in_array($dayOfWeek, $workDays),
- 400,
- '该时间不在技师工作日内'
- );
-
- $dateStr = $serviceDateTime->format('Y-m-d');
- $restDates = $workSchedule['rest_dates'] ?? [];
- abort_if(
- in_array($dateStr, $restDates),
- 400,
- '技师该日期休息'
- );
-
- $timeStr = $serviceDateTime->format('H:i');
- $workHours = $workSchedule['work_hours'] ?? [
- 'start' => '09:00',
- 'end' => '21:00',
- ];
- $startTime = Carbon::parse($workHours['start']);
- $endTime = Carbon::parse($workHours['end']);
- abort_if(
- $timeStr < $startTime->format('H:i') || $timeStr > $endTime->format('H:i'),
- 400,
- sprintf(
- '服务时间需在%s-%s之间',
- $startTime->format('H:i'),
- $endTime->format('H:i')
- )
- );
- }
-
- private function checkTimeConflicts(int $coachId, string $serviceTime): void
- {
- $serviceDateTime = Carbon::parse($serviceTime);
-
- $serviceDuration = config('business.default_service_duration', 120);
-
- $serviceEndTime = $serviceDateTime->copy()->addMinutes($serviceDuration);
-
- $conflictingOrder = Order::query()
- ->where('coach_id', $coachId)
- ->where('service_time', '<=', $serviceEndTime)
- ->where('service_end_time', '>=', $serviceDateTime)
- ->whereIn('status', [
- OrderStatus::PAID->value,
- OrderStatus::ACCEPTED->value,
- OrderStatus::SERVING->value,
- ])
- ->first();
- abort_if($conflictingOrder, 400, '该时间段已被预约');
- }
- }
|