You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
3.0 KiB
83 lines
3.0 KiB
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\VisitTime;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class VisitTimeRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$id = $this->input('id');
|
|
$visitTimeId = $this->route('id') ?? $id;
|
|
|
|
return [
|
|
'start_time' => 'required|date_format:H:i',
|
|
'end_time' => 'required|date_format:H:i|after:start_time',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'start_time.required' => __('validation.required', ['attribute' => __('visit_time.start_time')]),
|
|
'start_time.date_format' => __('validation.date_format', ['attribute' => __('visit_time.start_time'), 'format' => 'H:i']),
|
|
'end_time.required' => __('validation.required', ['attribute' => __('visit_time.end_time')]),
|
|
'end_time.date_format' => __('validation.date_format', ['attribute' => __('visit_time.end_time'), 'format' => 'H:i']),
|
|
'end_time.after' => __('validation.after', ['attribute' => __('visit_time.end_time'), 'date' => __('visit_time.start_time')]),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Configure the validator instance.
|
|
*/
|
|
public function withValidator($validator): void
|
|
{
|
|
$validator->after(function ($validator) {
|
|
// 检查时间冲突
|
|
if ($this->has('start_time') && $this->has('end_time')) {
|
|
$startTime = $this->input('start_time');
|
|
$endTime = $this->input('end_time');
|
|
$id = $this->input('id');
|
|
|
|
$hasConflict = VisitTime::where('id', '!=', $id)
|
|
->where(function ($query) use ($startTime, $endTime) {
|
|
$query->where(function ($q) use ($startTime, $endTime) {
|
|
// 新时段开始时间在现有时段内
|
|
$q->where('start_time', '<=', $startTime)
|
|
->where('end_time', '>', $startTime);
|
|
})->orWhere(function ($q) use ($startTime, $endTime) {
|
|
// 新时段结束时间在现有时段内
|
|
$q->where('start_time', '<', $endTime)
|
|
->where('end_time', '>=', $endTime);
|
|
})->orWhere(function ($q) use ($startTime, $endTime) {
|
|
// 新时段完全包含现有时段
|
|
$q->where('start_time', '>=', $startTime)
|
|
->where('end_time', '<=', $endTime);
|
|
});
|
|
})
|
|
->exists();
|
|
|
|
if ($hasConflict) {
|
|
$validator->errors()->add('start_time', __('visit_time.time_conflict'));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|