diff --git a/app/api/controller/TeacherFreeTimeController.php b/app/api/controller/TeacherFreeTimeController.php new file mode 100644 index 0000000..f55706f --- /dev/null +++ b/app/api/controller/TeacherFreeTimeController.php @@ -0,0 +1,25 @@ +addFreeTime($request); + return $this->json($res); + } + +} \ No newline at end of file diff --git a/app/api/controller/UploadController.php b/app/api/controller/UploadController.php index bdaf4b9..b2738e7 100644 --- a/app/api/controller/UploadController.php +++ b/app/api/controller/UploadController.php @@ -23,4 +23,16 @@ class UploadController extends BaseController return $this->json($res); } + /** + * @desc 上传图片 + * @param Request $request + * @return \support\Response + */ + public function uploadFile(Request $request) + { + $service = new UploadService(); + $res = $service->uploadImg($request->file('image')); + return $this->json($res); + } + } \ No newline at end of file diff --git a/app/common/model/TeacherFreeTime.php b/app/common/model/TeacherFreeTime.php new file mode 100644 index 0000000..112370e --- /dev/null +++ b/app/common/model/TeacherFreeTime.php @@ -0,0 +1,33 @@ +hasOne(Teacher::class,'id','teacher_id')->bind(['teacher_name','teacher_account'=>"account"]); + } + +} diff --git a/app/common/service/TeacherFreeTimeService.php b/app/common/service/TeacherFreeTimeService.php new file mode 100644 index 0000000..3eb0903 --- /dev/null +++ b/app/common/service/TeacherFreeTimeService.php @@ -0,0 +1,91 @@ +teacher)) { + throw new Exception('请教师登陆后再设置'); + } + $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); + + if ($teacher->isEmpty()) { + throw new Exception('未找到教师信息,设置失败'); + } + $data = $request->post(); + + $free_time = json_decode($data['free_time'], true); + + if (empty($free_time)) { + throw new Exception('请选择时间段之后再提交'); + } + $total_count = 0; + $exit_count = 0; + $err_free_count = 0; + + foreach ($free_time as $free_date => $times) { + if ($times) { + foreach ($times as $time) { + $time_period = explode('-', $time); + $firstDate = new DateTime($free_date . ' ' . trim($time_period[0])); + $secondDate = new DateTime($free_date . ' ' . trim($time_period[1])); + $diff = $secondDate->diff($firstDate); + $h = $diff->h; + $m = round($diff->i / 60, 2); + $hour = round($h + $m, 2); + $time = $time_period[0] . ' - ' . $time_period[1]; + $free_data = [ + 'teacher_id' => $request->teacher->id, + 'date' => $free_date, + 'time' => $time, + 'hour' => $hour, + 'start_time' => date('Y-m-d H:i:s', $firstDate->getTimestamp()), + 'end_time' => date('Y-m-d H:i:s', $secondDate->getTimestamp()), + 'month' => date('Y-m', strtotime($free_date)), + ]; + + //判断是否已经存在 + $exit = TeacherFreeTime::where($free_data)->findOrEmpty(); + if(!$exit->isEmpty()){ + continue; + } + $res = TeacherFreeTime::create($free_data); + if(!$res){ + throw new Exception('保存失败'); + } + } + } + } + + return [ + 'code' => ResponseCode::SUCCESS, + 'msg' => '保存成功' + ]; + + }catch (Exception $e){ + return [ + 'code' => ResponseCode::FAIL, + 'msg' => $e->getMessage() + ]; + } + + } + +} \ No newline at end of file diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 5c67a53..8580a01 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -38,7 +38,7 @@ class UploadService //上传到七牛云 $res = QiniuUtils::upload($filePath); - if($res['code'] == ResponseCode::FAIL){ + if ($res['code'] == ResponseCode::FAIL) { throw new Exception($res['msg']); } //删除本地图片 @@ -48,10 +48,60 @@ class UploadService 'msg' => '上传成功', 'data' => $res['data'] ]; - }else{ + } else { throw new Exception('文件无效'); } - }catch (Exception $e){ + } catch (Exception $e) { + return [ + 'code' => ResponseCode::FAIL, + 'msg' => $e->getMessage() + ]; + } + + } + + public function uploadFile($file) + { + ini_set('memory_limit', '256M'); + try { + + if ($file && $file->isValid()) { + $ext = $file->getUploadExtension(); + if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'JPG', 'JPEG', 'PBG'])) { + $type = 'images'; + } else { + $type = 'other'; + } + $fileSize = $file->getSize(); + + $uploadDir = '/files/' . $type . '/' . date('Ym') . '/'; + $filename = date('YmdHis') . rand(999, 99999); + $uploadPath = $uploadDir . $filename . '.' . $ext; + + $rootUploadDir = public_path() . $uploadDir; + + if (!is_dir($rootUploadDir)) { + mkdir($rootUploadDir, 0777, true); + } + $filePath = public_path() . $uploadPath; + $file->move($filePath); + + //上传到七牛云 + $res = QiniuUtils::upload($filePath); + if ($res['code'] == ResponseCode::FAIL) { + throw new Exception($res['msg']); + } + //删除本地图片 + unlink($filePath); + return [ + 'code' => ResponseCode::SUCCESS, + 'msg' => '上传成功', + 'data' => $res['data'] + ]; + } else { + throw new Exception('文件无效'); + } + } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() diff --git a/plugin/admin/app/controller/TeacherController.php b/plugin/admin/app/controller/TeacherController.php index 226a480..4efd6c6 100644 --- a/plugin/admin/app/controller/TeacherController.php +++ b/plugin/admin/app/controller/TeacherController.php @@ -151,6 +151,20 @@ class TeacherController extends Crud return view('teacher/update'); } + + /** + * @desc 查看教师日程安排 + * @param Request $request + * @return Response + */ + public function check_free_time(Request $request) + { + + $data = $request->get(); + + return view('teacher/check_free_time', ['teacher_id' => $data['id']]); + } + /** * @desc 查看教师日程安排 * @param Request $request @@ -161,7 +175,7 @@ class TeacherController extends Crud $data = $request->get(); - return view('teacher/check_schedule', ['teacher_id' => $data['id']]); + return view('teacher/check_free_time', ['teacher_id' => $data['id']]); } diff --git a/plugin/admin/app/controller/TeacherFreeTimeController.php b/plugin/admin/app/controller/TeacherFreeTimeController.php new file mode 100644 index 0000000..5b96bfb --- /dev/null +++ b/plugin/admin/app/controller/TeacherFreeTimeController.php @@ -0,0 +1,267 @@ +model = new TeacherFreeTime; + } + + + public function select(Request $request): Response + { + try { + $free_time = \app\common\model\TeacherFreeTime::order('id desc'); + $data = $request->get(); + if (isset($data['teacher_id']) && !empty($data['teacher_id'])) { + $free_time->where('teacher_id', $data['teacher_id']); + } + if(isset($data['month']) && !empty($data['month'])){ + $free_time->where('month', $data['month']); + } + if(isset($data['date']) && !empty($data['date'])){ + $free_time->where('date', $data['date']); + } + + $limit = (int)$request->get('limit', 10); + $limit = $limit <= 0 ? 10 : $limit; + $page = (int)$request->get('page'); + $page = $page > 0 ? $page : 1; + + $total = $free_time->count(); + $list = $free_time->page($page, $limit)->with(['teacher'])->select(); + + return json([ + 'code' => ResponseCode::WEB_API_SUCCESS, + 'data' => $list, + 'count' => $total + ]); + } catch (Exception $e) { + return json([ + 'code' => ResponseCode::WEB_API_FAIL, + 'msg' => $e->getMessage() + ]); + } + } + + /** + * 浏览 + * @return Response + */ + public function index(): Response + { + //获取所有老师 + $teacher = Teacher::order('id asc')->field('id,teacher_name,account')->select()->toArray(); + return view('teacher-free-time/index', ['teacher' => $teacher]); + } + + /** + * 插入 + * @param Request $request + * @return Response + * @throws BusinessException + */ + public function insert(Request $request): Response + { + if ($request->method() === 'POST') { + return parent::insert($request); + } + return view('teacher-free-time/insert'); + } + + /** + * 更新 + * @param Request $request + * @return Response + * @throws BusinessException + */ + public function update(Request $request): Response + { + if ($request->method() === 'POST') { + return parent::update($request); + } + return view('teacher-free-time/update'); + } + + + + /** + * @desc 获取教师空闲时间统计 + * @param Request $request + * @return Response + */ + public function getTeacherFreeTimeMonth(Request $request) + { + try { + $data = $request->get(); + $schedule_time = \app\common\model\TeacherFreeTime::where(['teacher_id' => $data['teacher_id']]) + ->field(' + teacher_id, + month, + COUNT(id) AS total_courses, + SUM(hour) AS total_hours, + SUM(CASE WHEN subject_id > 0 THEN 1 ELSE 0 END) AS has_subject, + SUM(CASE WHEN subject_id = 0 THEN 1 ELSE 0 END) AS has_no_subject + ') + ->group('month') + ->with(['teacher']); + + $limit = (int)$request->get('limit', 10); + $limit = $limit <= 0 ? 10 : $limit; + $page = (int)$request->get('page'); + $page = $page > 0 ? $page : 1; + + $total = $schedule_time->count(); + $list = $schedule_time->page($page, $limit)->select(); + + return json([ + 'code' => ResponseCode::WEB_API_SUCCESS, + 'data' => $list, + 'count' => $total, + 'msg' => 'ok' + ]); + } catch (Exception $e) { + return json([ + 'code' => ResponseCode::WEB_API_FAIL, + 'msg' => $e->getMessage() + ]); + } + } + + + /** + * @desc 教师月份空闲时间日历 + * @param Request $request + * @return Response + */ + public function teacherFreeTimeSchedule(Request $request) + { + + $data = $request->get(); + $teacher_id = $data['teacher_id']; + $month = $data['month']; + $teacher = \app\common\model\Teacher::where(['id'=>$teacher_id])->findOrEmpty()->toArray(); + //获取该老师当前分配学生 + + return view('teacher/free_time_schedule', ['teacher' => $teacher, 'month' => $month]); + } + + + /** + * @desc 日历数据接口 + * @param Request $request + * @return void + */ + public function getTeacherFreeTimeSchedule(Request $request) + { + try { + $teacher_id = $request->post('id'); + $start_date = date('Y-m-d H:i:s', strtotime($request->post('start_date'))); + $end_date = date('Y-m-d 23:59:59', strtotime($request->post('end_date'))); + $list = \app\common\model\TeacherFreeTime::where(['teacher_id' => $teacher_id]) + ->whereBetweenTime('start_time', $start_date, $end_date) +// ->with(['subject']) + ->select() + ->toArray(); + + $free_time = []; + foreach ($list as $item) { + $title = $item['time'] . ' - ' . $item['hour'] . '/h'; + $free_time[] = [ + 'free_time_id' => $item['id'], + 'title' => $title, + 'start' => date('Y-m-d H:i', strtotime($item['start_time'])), + 'color' => 'green', + ]; + } + + return json([ + 'code' => ResponseCode::WEB_API_SUCCESS, + 'data' => $free_time, + 'msg' => 'success' + ]); + } catch (Exception $e) { + return json([ + 'code' => ResponseCode::WEB_API_FAIL, + 'msg' => $e->getMessage() + ]); + } + } + + + /** + * @desc 空闲时间设置 + * @param Request $request + * @return Response + * @throws BusinessException + */ + public function freeTimeSetting(Request $request) + { + if ($request->method() === 'POST') { + + if ($request->post('is_publish')) { + $request->post('is_publish', 0); + } +// parent::update($request); + + return parent::update($request); + } + $free_time_id = $request->get('free_time_id'); + $free_time = \app\common\model\TeacherFreeTime::where(['id' => $free_time_id])->with(['teacher'])->findOrEmpty()->toArray(); + //课程 + $project = Subject::order('sort desc, id asc')->select()->toArray(); + //所有学生 + $student = Student::order('id asc')->field('id,account,student_name')->select()->toArray(); + + $time_period = explode(' - ', $free_time['time']); + $limit_time = [ + 'start_time' => date('H:i:s', strtotime($time_period[0])), + 'end_time' => date('H:i:s', strtotime($time_period[1])), + ]; + + +// $student_schedule = StudentSchedule::where(['teacher_schedule_time_id' => $free_time_id])->select()->toArray(); +// $student_schedule_id = []; +// if ($student_schedule) { +// $student_schedule_id = array_column($student_schedule, 'student_id'); +// } + + return view('teacher/free_time_setting', [ + 'schedule_time' => $free_time, + 'project' => $project, + 'student' => $student, + 'limit_time' => $limit_time, +// 'student_schedule_id' => $student_schedule_id + ]); + + } + + +} diff --git a/plugin/admin/app/controller/TeacherScheduleTimeController.php b/plugin/admin/app/controller/TeacherScheduleTimeController.php index f678d2a..250b691 100644 --- a/plugin/admin/app/controller/TeacherScheduleTimeController.php +++ b/plugin/admin/app/controller/TeacherScheduleTimeController.php @@ -6,6 +6,7 @@ use app\common\model\Student; use app\common\model\StudentSchedule; use app\common\model\Subject; use app\common\model\Teacher; +use app\common\model\TeacherFreeTime; use app\constant\ResponseCode; use support\Request; use support\Response; @@ -13,6 +14,7 @@ use plugin\admin\app\model\TeacherScheduleTime; use plugin\admin\app\controller\Crud; use support\exception\BusinessException; use think\Exception; +use DateTime; /** * 教师空闲时间 @@ -104,6 +106,64 @@ class TeacherScheduleTimeController extends Crud return view('teacher-schedule-time/insert'); } + + public function addTeacherScheduleTime(Request $request) + { + try { + $data = $request->post(); + + $free_time = TeacherFreeTime::where(['id' => $data['id']])->findOrEmpty(); + if ($free_time->isEmpty()) { + throw new Exception('教师空闲时间不存在'); + } + + foreach ($data['schedule_time'] as $item) { + $time_period = explode(' - ', $item); + $start_time = strtotime($free_time->date . ' ' . trim($time_period[0])); + $end_time = strtotime($free_time->date . ' ' . trim($time_period[1])); + if (!(strtotime($free_time->start_time) <= $start_time && strtotime($free_time->end_time) >= $end_time)) { + throw new Exception('时间' . $item . '不在老师空闲范围内'); + } + } + + \app\common\model\TeacherScheduleTime::where(['free_time_id' => $free_time->id])->select()->delete(); + foreach ($data['schedule_time'] as $item) { + $time_period = explode(' - ', $item); + $start_time = $free_time->date . ' ' . trim($time_period[0]); + $end_time = $free_time->date . ' ' . trim($time_period[1]); + + $firstDate = new DateTime($start_time); + $secondDate = new DateTime($end_time); + $diff = $secondDate->diff($firstDate); + $h = $diff->h; + $m = round($diff->i / 60, 2); + $hour = round($h + $m, 2); + $time = $time_period[0] . ' - ' . $time_period[1]; + + \app\common\model\TeacherScheduleTime::create([ + 'teacher_id' => $free_time->teacher_id, + 'date' => $free_time->date, + 'time' => $time, + 'hour' => $hour, + 'start_time' => $start_time, + 'end_time' => $end_time, + 'month' => $free_time->month, + 'free_time_id' => $free_time->id, + ]); + } + + return json([ + 'code' => ResponseCode::WEB_API_SUCCESS, + 'msg' => 'success' + ]); + } catch (Exception $e) { + return json([ + 'code' => ResponseCode::WEB_API_FAIL, + 'msg' => $e->getMessage() + ]); + } + } + /** * 更新 * @param Request $request diff --git a/plugin/admin/app/model/TeacherFreeTime.php b/plugin/admin/app/model/TeacherFreeTime.php index 66c1598..142a83a 100644 --- a/plugin/admin/app/model/TeacherFreeTime.php +++ b/plugin/admin/app/model/TeacherFreeTime.php @@ -9,7 +9,12 @@ use plugin\admin\app\model\Base; * @property integer $teacher_id 教师 * @property string $date 日期 * @property string $time 时间 - * @property string $hour 课时 + * @property mixed $hour 课时 + * @property string $start_time 开始时间 + * @property string $end_time 结束时间 + * @property string $month 月份 + * @property integer $subject_id 课程id + * @property integer $is_publish 是否发布 * @property mixed $created_at 创建时间 * @property string $updated_at 更新时间 * @property string $deleted_at diff --git a/plugin/admin/app/view/teacher-free-time/index.html b/plugin/admin/app/view/teacher-free-time/index.html new file mode 100644 index 0000000..31537ea --- /dev/null +++ b/plugin/admin/app/view/teacher-free-time/index.html @@ -0,0 +1,364 @@ + + + +
+ +