Browse Source

活动添加编辑时间验证优化

master
wanghongjun 3 weeks ago
parent
commit
97b33f222b
  1. 59
      app/Services/EventCalendarService.php

59
app/Services/EventCalendarService.php

@ -61,21 +61,8 @@ class EventCalendarService extends BaseService
$end_time = get_datetime('datetime', strtotime($data['end_time'])); $end_time = get_datetime('datetime', strtotime($data['end_time']));
// 活动开始时间不能在已有活动时间内 // 活动开始时间不能在已有活动时间内
$exists = EventCalendar::query()->whereIn('status', [0, 1])->where( $this->validateTime($data['pattern_id'], $start_time);
'pattern_id', $this->validateTime($data['pattern_id'], $end_time);
$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')
);
}
$model = EventCalendar::query()->create([ $model = EventCalendar::query()->create([
'pattern_id' => $data['pattern_id'], '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 array $data
* @param string $id * @param string $id
@ -109,24 +119,7 @@ class EventCalendarService extends BaseService
&& isset($data['start_time']) && isset($data['start_time'])
) { ) {
$start_time = $data['start_time']; $start_time = $data['start_time'];
$existsWhere = [ $this->validateTime($data['pattern_id'], $start_time, $id);
['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')
);
}
} }
$model = EventCalendar::query()->findOrFail($id); $model = EventCalendar::query()->findOrFail($id);
$oldValues = $model->toArray(); $oldValues = $model->toArray();

Loading…
Cancel
Save