diff --git a/app/Services/EventCalendarService.php b/app/Services/EventCalendarService.php index 76faa56..bedde52 100644 --- a/app/Services/EventCalendarService.php +++ b/app/Services/EventCalendarService.php @@ -61,21 +61,8 @@ class EventCalendarService extends BaseService $end_time = get_datetime('datetime', strtotime($data['end_time'])); // 活动开始时间不能在已有活动时间内 - $exists = EventCalendar::query()->whereIn('status', [0, 1])->where( - 'pattern_id', - $data['pattern_id'] - ) - ->where(function ($query) use ($start_time) { - $query->whereRaw( - "'{$start_time}' BETWEEN start_time AND end_time" - ); - }) - ->exists(); - if ($exists) { - throw new Exception( - __('service.event_calendar.pattern_exists') - ); - } + $this->validateTime($data['pattern_id'], $start_time); + $this->validateTime($data['pattern_id'], $end_time); $model = EventCalendar::query()->create([ 'pattern_id' => $data['pattern_id'], @@ -96,6 +83,29 @@ class EventCalendarService extends BaseService } } + // 验证时间是否在已有模式下存在 + protected function validateTime($pattern_id, $start_time, $id = 0) + { + $where = [['pattern_id', '=', $pattern_id]]; + if ($id) { + $where[] = ['id', '<>', $id]; + } + $exists = EventCalendar::query()->whereIn('status', [0, 1])->where( + $where + ) + ->where(function ($query) use ($start_time) { + $query->whereRaw( + "'{$start_time}' BETWEEN start_time AND end_time" + ); + }) + ->exists(); + if ($exists) { + throw new Exception( + __('service.event_calendar.pattern_exists') + ); + } + } + /** * @param array $data * @param string $id @@ -109,24 +119,7 @@ class EventCalendarService extends BaseService && isset($data['start_time']) ) { $start_time = $data['start_time']; - $existsWhere = [ - ['pattern_id', '=', $data['pattern_id']], - ['id', '<>', $id] - ]; - if (EventCalendar::query()->where($existsWhere)->whereIn( - 'status', - [0, 1] - ) - ->where(function ($query) use ($start_time) { - $query->whereRaw( - "'{$start_time}' BETWEEN start_time AND end_time" - ); - })->exists() - ) { - throw new Exception( - __('service.event_calendar.pattern_exists') - ); - } + $this->validateTime($data['pattern_id'], $start_time, $id); } $model = EventCalendar::query()->findOrFail($id); $oldValues = $model->toArray();