fix 检查时间是否有重复
This commit is contained in:
parent
7ae3eaeb26
commit
3146efae1e
@ -122,6 +122,7 @@ class SubjectHomeworkService
|
||||
try {
|
||||
$data = $request->get();
|
||||
$subject_homework = SubjectHomework::where(['teacher_schedule_time_id' => $data['teacher_schedule_time_id']])
|
||||
->where(['homework_file_is_publish' => 1])
|
||||
->with(['teacher', 'subject'])
|
||||
->findOrEmpty();
|
||||
|
||||
|
@ -81,3 +81,41 @@ if (!function_exists('get_dates_in_month')) {
|
||||
return $dates;
|
||||
}
|
||||
}
|
||||
|
||||
if(!function_exists('is_time_cross')){
|
||||
/**
|
||||
* PHP计算多个时间段是否有交集(边界重叠不算)
|
||||
* @param ...$timePeriods
|
||||
* @return bool|mixed
|
||||
*/
|
||||
function is_time_cross(array $timePeriods) {
|
||||
|
||||
$timePeriods = array_values($timePeriods);
|
||||
// 将时间字符串转换为时间戳
|
||||
$timePeriods = array_map(function ($period) {
|
||||
extract($period);
|
||||
|
||||
return [
|
||||
'begin' => strtotime($beginTime),
|
||||
'end' => strtotime($endTime)
|
||||
];
|
||||
}, $timePeriods);
|
||||
|
||||
// 递归函数来检查时间段是否有交集
|
||||
$checkCross = function ($index, $timePeriods) use (&$checkCross) {
|
||||
if ($index === count($timePeriods) - 1) {
|
||||
return true; // 最后一个时间段,没有交集
|
||||
}
|
||||
|
||||
for ($i = $index + 1; $i < count($timePeriods); $i++) {
|
||||
if ($timePeriods[$index]['end'] > $timePeriods[$i]['begin'] && $timePeriods[$i]['end'] > $timePeriods[$index]['begin']) {
|
||||
return false; // 有交集
|
||||
}
|
||||
}
|
||||
|
||||
return $checkCross($index + 1, $timePeriods); // 递归检查下一个时间段
|
||||
};
|
||||
|
||||
return $checkCross(0, $timePeriods); // 从第一个时间段开始检查
|
||||
}
|
||||
}
|
||||
|
@ -175,6 +175,21 @@ class TeacherScheduleTimeController extends Crud
|
||||
}
|
||||
}
|
||||
|
||||
//检测时间是否有相交的
|
||||
$cross_data_time = [];
|
||||
foreach ($data['schedule_time'] as $index => $item) {
|
||||
$time_period = explode(' - ', $item);
|
||||
$cross_data_time[$index] = [
|
||||
'beginTime' => $data['date'] . ' ' . $time_period[0],
|
||||
'endTime' => $data['date'] . ' ' . $time_period[1],
|
||||
];
|
||||
}
|
||||
|
||||
if (!is_time_cross($cross_data_time)) {
|
||||
throw new Exception('请确认时间是否有重叠');
|
||||
}
|
||||
|
||||
|
||||
if (isset($data['teacher_schedule_id'])) {
|
||||
$teacher_schedule_id = array_filter($data['teacher_schedule_id']);
|
||||
|
||||
@ -222,6 +237,7 @@ class TeacherScheduleTimeController extends Crud
|
||||
'en_start_time' => $en_start_time,
|
||||
'en_end_time' => $en_end_time,
|
||||
'month' => $free_time->month,
|
||||
'subject_id' => $data['subject_id'][$index],
|
||||
'free_time_id' => $free_time->id,
|
||||
]);
|
||||
} else {
|
||||
@ -236,6 +252,7 @@ class TeacherScheduleTimeController extends Crud
|
||||
'en_start_time' => $en_start_time,
|
||||
'en_end_time' => $en_end_time,
|
||||
'month' => $free_time->month,
|
||||
'subject_id' => $data['subject_id'][$index],
|
||||
'free_time_id' => $free_time->id,
|
||||
]);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user