230 lines
7.7 KiB
PHP
230 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace plugin\admin\app\controller;
|
|
|
|
use app\common\model\Student;
|
|
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\StudentSchedule;
|
|
use plugin\admin\app\controller\Crud;
|
|
use support\exception\BusinessException;
|
|
use think\Exception;
|
|
|
|
/**
|
|
* 学生课表
|
|
*/
|
|
class StudentScheduleController extends Crud
|
|
{
|
|
|
|
/**
|
|
* @var StudentSchedule
|
|
*/
|
|
protected $model = null;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->model = new StudentSchedule;
|
|
}
|
|
|
|
/**
|
|
* 浏览
|
|
* @return Response
|
|
*/
|
|
public function index(): Response
|
|
{
|
|
//获取所有老师
|
|
$teacher = Teacher::order('id asc')->field('id,teacher_name,account')->select()->toArray();
|
|
//所有课程
|
|
$subject = Subject::order('sort desc,id asc')->field('id,subject_name,english_name')->select()->toArray();
|
|
$student = Student::order('id asc')->field('id,student_name,account')->select()->toArray();
|
|
|
|
return view('student-schedule/index', ['teacher' => $teacher, 'subject' => $subject, 'student' => $student]);
|
|
}
|
|
|
|
|
|
public function select(Request $request): Response
|
|
{
|
|
try {
|
|
$data = $request->get();
|
|
$student_schedule = \app\common\model\StudentSchedule::order('id desc');
|
|
|
|
if (isset($data['student_id']) && $data['student_id']) {
|
|
$student_schedule->where(['student_id' => $data['student_id']]);
|
|
}
|
|
if (isset($data['teacher_id']) && $data['teacher_id']) {
|
|
$student_schedule->where(['teacher_id' => $data['teacher_id']]);
|
|
}
|
|
if (isset($data['subject_id']) && $data['subject_id']) {
|
|
$student_schedule->where(['subject_id' => $data['subject_id']]);
|
|
}
|
|
if (isset($data['date']) && $data['date']) {
|
|
if ($data['date'][0] && $data['date'][1]) {
|
|
$student_schedule->whereBetweenTime('date', $data['date'][0], $data['date'][1]);
|
|
}
|
|
}
|
|
if (isset($data['month']) && $data['month']) {
|
|
$student_schedule->where(['month' => $data['month']]);
|
|
}
|
|
if (isset($data['is_publish']) && $data['is_publish'] !== '') {
|
|
$student_schedule->where(['is_publish' => $data['is_publish']]);
|
|
}
|
|
|
|
|
|
$limit = (int)$request->get('limit', 10);
|
|
$limit = $limit <= 0 ? 10 : $limit;
|
|
$page = (int)$request->get('page');
|
|
$page = $page > 0 ? $page : 1;
|
|
|
|
$total = $student_schedule->count();
|
|
$list = $student_schedule->with(['teacher', 'student', 'subject'])->page($page, $limit)->select();
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'data' => $list,
|
|
'count' => $total,
|
|
'msg' => 'success'
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 更改学生课表状态
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function changePublishStatus(Request $request)
|
|
{
|
|
try {
|
|
$data = $request->post();
|
|
$res = \app\common\model\StudentSchedule::where(['id' => $data['ids']])->save([
|
|
'is_publish' => $data['status']
|
|
]);
|
|
if (!$res) {
|
|
throw new Exception('操作失败');
|
|
}
|
|
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
|
|
* @return Response
|
|
* @throws BusinessException
|
|
*/
|
|
public function insert(Request $request): Response
|
|
{
|
|
if ($request->method() === 'POST') {
|
|
return parent::insert($request);
|
|
}
|
|
return view('student-schedule/insert');
|
|
}
|
|
|
|
/**
|
|
* 更新
|
|
* @param Request $request
|
|
* @return Response
|
|
* @throws BusinessException
|
|
*/
|
|
public function update(Request $request): Response
|
|
{
|
|
if ($request->method() === 'POST') {
|
|
return parent::update($request);
|
|
}
|
|
return view('student-schedule/update');
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 添加学生排课
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function addStudentSchedule(Request $request)
|
|
{
|
|
try {
|
|
$data = $request->post();
|
|
$teacher_schedule_time = TeacherScheduleTime::where(['id' => $data['teacher_schedule_time_id']])->findOrEmpty();
|
|
|
|
if ($teacher_schedule_time->isEmpty()) {
|
|
throw new Exception('未找到教师排课,添加失败');
|
|
}
|
|
|
|
if ($data['type'] == 'add') {
|
|
if (empty($teacher_schedule_time->subject_id)) {
|
|
throw new Exception('请先排课再添加学生');
|
|
}
|
|
//判断学生时间和教师时间是否冲突
|
|
$conflict = \app\common\model\StudentSchedule::where(['student_id' => $data['student_id']])->whereRaw("
|
|
(start_time <= ? AND end_time >= ?) OR
|
|
(start_time <= ? AND end_time >= ?) OR
|
|
(? <= start_time AND ? >= end_time)
|
|
", [$teacher_schedule_time->start_time, $teacher_schedule_time->start_time,
|
|
$teacher_schedule_time->end_time, $teacher_schedule_time->end_time,
|
|
$teacher_schedule_time->start_time, $teacher_schedule_time->end_time,
|
|
])->with(['teacher', 'subject'])->findOrEmpty();
|
|
|
|
if (!$conflict->isEmpty()) {
|
|
throw new Exception("该学生课程和【{$conflict->teacher_name}】的【{$conflict->subject_name} {$conflict->date} {$conflict->time} 】有冲突");
|
|
}
|
|
|
|
$res = \app\common\model\StudentSchedule::create([
|
|
'student_id' => $data['student_id'],
|
|
'teacher_schedule_time_id' => $teacher_schedule_time->id,
|
|
'teacher_schedule_time_detail' => '',
|
|
'teacher_id' => $teacher_schedule_time->teacher_id,
|
|
'subject_id' => $teacher_schedule_time->subject_id,
|
|
'date' => $teacher_schedule_time->date,
|
|
'time' => $teacher_schedule_time->time,
|
|
'hour' => $teacher_schedule_time->hour,
|
|
'start_time' => $teacher_schedule_time->start_time,
|
|
'end_time' => $teacher_schedule_time->end_time,
|
|
'month' => $teacher_schedule_time->month,
|
|
]);
|
|
if (!$res) {
|
|
throw new Exception('添加失败');
|
|
}
|
|
|
|
} else {
|
|
$student_schedule = \app\common\model\StudentSchedule::where(['student_id' => $data['student_id'], 'teacher_schedule_time_id' => $teacher_schedule_time->id])->findOrEmpty();
|
|
|
|
$student_schedule->delete();
|
|
}
|
|
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'msg' => '添加成功',
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
}
|