2024-07-14 21:35:58 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\service;
|
|
|
|
|
|
|
|
use app\common\model\Teacher;
|
2024-07-15 00:09:59 +08:00
|
|
|
use app\common\model\TeacherScheduleTime;
|
2024-07-14 21:35:58 +08:00
|
|
|
use app\constant\ResponseCode;
|
|
|
|
use DateTime;
|
|
|
|
use think\Exception;
|
|
|
|
|
2024-07-15 00:09:59 +08:00
|
|
|
class TeacherScheduleTimeService
|
2024-07-14 21:35:58 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc 添加空闲时间
|
|
|
|
* @param $request
|
|
|
|
* @return array|void
|
|
|
|
*/
|
2024-07-15 17:26:13 +08:00
|
|
|
public function addScheduleTime($request)
|
2024-07-14 21:35:58 +08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (empty($request->teacher)) {
|
|
|
|
throw new Exception('请教师登陆后再设置');
|
|
|
|
}
|
|
|
|
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
|
|
|
|
|
|
|
|
if ($teacher->isEmpty()) {
|
|
|
|
throw new Exception('未找到教师信息,设置失败');
|
|
|
|
}
|
|
|
|
$data = $request->post();
|
|
|
|
|
2024-07-15 00:09:59 +08:00
|
|
|
$free_time = json_decode($data['schedule_time'], true);
|
2024-07-14 21:35:58 +08:00
|
|
|
|
|
|
|
if (empty($free_time)) {
|
|
|
|
throw new Exception('请选择时间段之后再提交');
|
|
|
|
}
|
|
|
|
foreach ($free_time as $free_date => $times) {
|
|
|
|
if ($times) {
|
|
|
|
foreach ($times as $time) {
|
2024-07-24 14:31:09 +08:00
|
|
|
$time_period = explode(' - ', $time);
|
2024-07-14 21:35:58 +08:00
|
|
|
$firstDate = new DateTime($free_date . ' ' . $time_period[0]);
|
|
|
|
$secondDate = new DateTime($free_date . ' ' . $time_period[1]);
|
|
|
|
$diff = $secondDate->diff($firstDate);
|
|
|
|
$h = $diff->h;
|
|
|
|
$m = round($diff->i / 60, 2);
|
|
|
|
$hour = round($h + $m, 2);
|
|
|
|
$free_data = [
|
|
|
|
'teacher_id' => $request->teacher->id,
|
|
|
|
'date' => $free_date,
|
|
|
|
'time' => $time,
|
|
|
|
'hour' => $hour,
|
2024-07-15 17:26:13 +08:00
|
|
|
'start_time' => date('Y-m-d H:i:s', $firstDate->getTimestamp()),
|
|
|
|
'end_time' => date('Y-m-d H:i:s', $secondDate->getTimestamp()),
|
2024-07-14 21:35:58 +08:00
|
|
|
'month' => date('Y-m', strtotime($free_date)),
|
|
|
|
];
|
2024-07-15 00:09:59 +08:00
|
|
|
//@todo:判断是否已经存在
|
|
|
|
$res = TeacherScheduleTime::create($free_data);
|
2024-07-24 14:31:09 +08:00
|
|
|
if (!$res) {
|
2024-07-14 21:35:58 +08:00
|
|
|
throw new Exception('保存失败');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
|
|
|
'msg' => '保存成功'
|
|
|
|
];
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 23:51:41 +08:00
|
|
|
/**
|
|
|
|
* @desc 获取排课列表
|
|
|
|
* @param $request
|
|
|
|
* @return array
|
|
|
|
*/
|
2024-07-15 17:26:13 +08:00
|
|
|
public function getScheduleTime($request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (empty($request->teacher)) {
|
|
|
|
throw new Exception('请教师登陆后再设置');
|
|
|
|
}
|
|
|
|
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
|
|
|
|
|
|
|
|
if ($teacher->isEmpty()) {
|
|
|
|
throw new Exception('未找到教师信息,设置失败');
|
|
|
|
}
|
|
|
|
$data = $request->get();
|
|
|
|
|
2024-07-24 14:31:09 +08:00
|
|
|
$schedule = TeacherScheduleTime::order('id desc')->where(['teacher_id' => $teacher->id]);
|
|
|
|
if (isset($data['month']) && !empty($data['month'])) {
|
|
|
|
$schedule->where('month', $data['month']);
|
|
|
|
}
|
|
|
|
if (isset($data['date']) && !empty($data['date'])) {
|
|
|
|
$schedule->where('date', $data['date']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = $schedule->field('id,teacher_id,date,time,hour,month,subject_id,is_publish')
|
2024-07-16 23:51:41 +08:00
|
|
|
->with(['subject'])
|
|
|
|
->select();
|
2024-07-15 17:26:13 +08:00
|
|
|
|
2024-07-16 23:51:41 +08:00
|
|
|
// foreach ($schedule as &$item){
|
|
|
|
// if(!$item->is_publish){
|
|
|
|
// $item->subject_name = '';
|
|
|
|
// $item->english_name = '';
|
|
|
|
// }
|
|
|
|
// }
|
2024-07-15 17:26:13 +08:00
|
|
|
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
2024-07-24 14:31:09 +08:00
|
|
|
'data' => $list,
|
2024-07-15 17:26:13 +08:00
|
|
|
'msg' => 'success',
|
|
|
|
];
|
2024-07-24 14:31:09 +08:00
|
|
|
} catch (Exception $e) {
|
2024-07-15 17:26:13 +08:00
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-14 21:35:58 +08:00
|
|
|
}
|