course/plugin/admin/app/controller/TeacherFreeTimeController.php

408 lines
15 KiB
PHP

<?php
namespace plugin\admin\app\controller;
use app\common\model\Student;
use app\common\model\StudentSchedule;
use app\common\model\Subject;
use app\common\model\Teacher;
use app\common\model\TeacherScheduleTime;
use app\constant\ResponseCode;
use support\Request;
use support\Response;
use plugin\admin\app\model\TeacherFreeTime;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use think\Exception;
use DateTime;
/**
* 空闲时间
*/
class TeacherFreeTimeController extends Crud
{
/**
* @var TeacherFreeTime
*/
protected $model = null;
/**
* 构造函数
* @return void
*/
public function __construct()
{
$this->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') {
$postData = $request->post();
try {
$teacher = Teacher::where(['id'=> $request->post('teacher_id')])->findOrEmpty();
if($teacher->isEmpty()){
throw new Exception('未找到教师信息');
}
$teacher_free_time = \app\common\model\TeacherFreeTime::where([
'teacher_id' => $postData['teacher_id'],
'date' => $postData['date'],
'en_date' => $postData['en_date'],
'month' => $postData['month'],
'en_month' => $postData['en_month'],
'time' => $postData['time'],
'en_time' => $postData['en_time'],
'time_zone_id' => $teacher['time_zone_id'],
'time_zone_name' => $teacher['time_zone_name'],
'time_zone_offset' => $teacher['time_zone_offset'],
])
->findOrEmpty();
if (!$teacher_free_time->isEmpty()) {
throw new Exception('该空闲时间段存在');
}
$time_period = explode('-', $postData['time']);
$en_time_period = explode('-', $postData['en_time']);
$firstDate = new DateTime($postData['date'] . ' ' . trim($time_period[0]));
$secondDate = new DateTime($postData['date'] . ' ' . trim($time_period[1]));
$enFirstDate = new DateTime($postData['en_date'] . ' ' . trim($en_time_period[0]));
$enSecondDate = new DateTime($postData['en_date'] . ' ' . trim($en_time_period[1]));
//根据时区计算教师国内时间
$start_time = date('Y-m-d H:i:s', strtotime( $teacher['time_zone_offset'] . ' hour', $enFirstDate->getTimestamp()));
$end_time = date('Y-m-d H:i:s', strtotime( $teacher['time_zone_offset'] . ' hour', $enSecondDate->getTimestamp()));
\app\common\model\TeacherFreeTime::create([
'teacher_id' => $request->post('teacher_id'),
'date' => $request->post('date'),
'en_date' => $request->post('en_date'),
'month' => $request->post('month'),
'en_month' => $request->post('en_month'),
'time' => $request->post('time'),
'en_time' => $request->post('en_time'),
'hour' => $request->post('hour'),
'start_time' => $start_time,
'end_time' => $end_time,
'en_start_time' => date('Y-m-d H:i:s', $enFirstDate->getTimestamp()),
'en_end_time' => date('Y-m-d H:i:s', $enSecondDate->getTimestamp()),
'time_zone_id' => $teacher['time_zone_id'],
'time_zone_name' => $teacher['time_zone_name'],
'time_zone_offset' => $teacher['time_zone_offset'],
]);
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => 'success',
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
return parent::insert($request);
}
//所有教师
$teacher = \app\common\model\Teacher::order('id desc')->select()->toArray();
return view('teacher-free-time/insert', ['teacher' => $teacher]);
}
/**
* 更新
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function update(Request $request): Response
{
if ($request->method() === 'POST') {
try {
$postData = $request->post();
$teacher_free_time = \app\common\model\TeacherFreeTime::where(['id' => $postData['id']])->findOrEmpty();
if ($teacher_free_time->isEmpty()) {
throw new Exception('未找到空闲时间');
}
$time_period = explode('-', $postData['time']);
$en_time_period = explode('-', $postData['en_time']);
$firstDate = new DateTime($postData['date'] . ' ' . trim($time_period[0]));
$secondDate = new DateTime($postData['date'] . ' ' . trim($time_period[1]));
$enFirstDate = new DateTime($postData['en_date'] . ' ' . trim($en_time_period[0]));
$enSecondDate = new DateTime($postData['en_date'] . ' ' . trim($en_time_period[1]));
//根据时区计算教师国内时间
$start_time = date('Y-m-d H:i:s', strtotime( $teacher_free_time['time_zone_offset'] . ' hour', $enFirstDate->getTimestamp()));
$end_time = date('Y-m-d H:i:s', strtotime( $teacher_free_time['time_zone_offset'] . ' hour', $enSecondDate->getTimestamp()));
$teacher_free_time->save([
'teacher_id' => $postData['teacher_id'],
'date' => $request->post('date'),
'en_date' => $request->post('en_date'),
'month' => $request->post('month'),
'en_month' => $request->post('en_month'),
'time' => $request->post('time'),
'en_time' => $request->post('en_time'),
'hour' => $request->post('hour'),
'start_time' => $start_time,
'end_time' => $end_time,
'en_start_time' => date('Y-m-d H:i:s', $enFirstDate->getTimestamp()),
'en_end_time' => date('Y-m-d H:i:s', $enSecondDate->getTimestamp()),
]);
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => 'success',
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
return parent::update($request);
}
$teacher_free_time = \app\common\model\TeacherFreeTime::where(['id' => $request->get('id')])
->with(['teacher'])
->findOrEmpty()->toArray();
//获取所有老师
$teacher = Teacher::order('id asc')->field('id,teacher_name,account')->select()->toArray();
return view('teacher-free-time/update', ['teacher_free_time' => $teacher_free_time, 'teacher' => $teacher]);
}
/**
* @desc 获取教师空闲时间统计
* @param Request $request
* @return Response
*/
public function getTeacherFreeTimeMonth(Request $request)
{
try {
$data = $request->get();
$schedule_time = \app\common\model\TeacherFreeTime::order('month desc')->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(['teacherScheduleTime'])
->select()
->toArray();
$free_time = [];
foreach ($list as $item) {
$title = $item['time'] . '-' . $item['hour'] . '/h';
$color = 'red';
if (isset($item['teacherScheduleTime']) && !empty($item['teacherScheduleTime'])) {
$color = 'green';
}
$free_time[] = [
'free_time_id' => $item['id'],
'title' => $title,
'start' => date('Y-m-d H:i', strtotime($item['start_time'])),
'color' => $color,
];
}
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['en_time']);
$limit_time = [
'start_time' => date('H:i', strtotime($time_period[0])),
'end_time' => date('H:i', strtotime($time_period[1])),
];
//获取已排课时间
$teacher_schedule_time = TeacherScheduleTime::where(['free_time_id' => $free_time_id])->with(['oneStudentSchedule'])->select()->toArray();
// print '<pre>';
// print_r($teacher_schedule_time);
// die;
// $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', [
'free_time' => $free_time,
'teacher_schedule_time' => $teacher_schedule_time,
'project' => $project,
'student' => $student,
'limit_time' => $limit_time,
// 'student_schedule_id' => $student_schedule_id
]);
}
}