536 lines
19 KiB
PHP
536 lines
19 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\common\service\SendMsgCronJobService;
|
|
use app\constant\ResponseCode;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
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', 'subject'])->page($page, $limit)->select();
|
|
|
|
foreach ($list as &$item) {
|
|
$item['week'] = date('D', strtotime($item['date']));
|
|
}
|
|
|
|
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') {
|
|
$result = parent::update($request);
|
|
|
|
$data = $request->post();
|
|
if (isset($data['is_publish']) && $data['is_publish']) {
|
|
//发送消息给学生
|
|
$student_schedule = \app\common\model\StudentSchedule::where(['id' => $data['id']])->find();
|
|
(new SendMsgCronJobService())->teacherScheduleTimePublishMsgToStudent($student_schedule->teacher_schedule_time_id);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
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} 】有冲突");
|
|
}
|
|
//删除其它学生的课程
|
|
$student_schedule = StudentSchedule::where(['teacher_schedule_time_id' => $teacher_schedule_time->id])->delete();
|
|
|
|
$student = Student::where(['id' => $data['student_id']])->findOrEmpty();
|
|
$res = \app\common\model\StudentSchedule::create([
|
|
'student_id' => $data['student_id'],
|
|
'student_name' => $student->student_name,
|
|
'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,
|
|
'en_time' => $teacher_schedule_time->en_time,
|
|
'hour' => $teacher_schedule_time->hour,
|
|
'start_time' => $teacher_schedule_time->start_time,
|
|
'end_time' => $teacher_schedule_time->end_time,
|
|
'en_start_time' => $teacher_schedule_time->en_start_time,
|
|
'en_end_time' => $teacher_schedule_time->en_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()
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 教师月份课程安排日历
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function studentSchedule(Request $request)
|
|
{
|
|
$data = $request->get();
|
|
$student_id = $data['student_id'];
|
|
$month = $data['month'];
|
|
$student = \app\common\model\Student::where(['id' => $student_id])->findOrEmpty()->toArray();
|
|
return view('student/student_schedule', ['student' => $student, 'month' => $month]);
|
|
}
|
|
|
|
/**
|
|
* @desc 学生排课日历数据
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function getStudentSchedule(Request $request)
|
|
{
|
|
try {
|
|
$student_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\StudentSchedule::where(['student_id' => $student_id])
|
|
->whereBetweenTime('start_time', $start_date, $end_date)
|
|
->with(['subject', 'teacher'])
|
|
->select()
|
|
->toArray();
|
|
|
|
$schedule_time = [];
|
|
foreach ($list as $item) {
|
|
$time_period = explode('-', $item['time']);
|
|
if ($item['subject_id']) {
|
|
$title = $item['time'] . PHP_EOL . $item['teacher_name'] . PHP_EOL . $item['subject_name'] . '/' . $item['english_name'];
|
|
if ($item['is_publish'] == 0) {
|
|
$color = 'orange';
|
|
} else {
|
|
$color = 'green';
|
|
}
|
|
} else {
|
|
$color = 'red';
|
|
$title = $item['time'];
|
|
}
|
|
$schedule_time[] = [
|
|
'schedule_time_id' => $item['id'],
|
|
'title' => $title,
|
|
'start' => $item['date'] . ' ' . trim($time_period[0]),
|
|
'color' => $color,
|
|
];
|
|
}
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'data' => $schedule_time,
|
|
'msg' => 'success'
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 设置学生排课
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function studentScheduleTimeSet(Request $request)
|
|
{
|
|
|
|
$data = $request->get();
|
|
$schedule_time = \app\common\model\StudentSchedule::where(['id' => $data['schedule_time_id']])->with(['teacher', 'subject',])->findOrEmpty()->toArray();
|
|
|
|
return view('student/schedule_time_setting', ['schedule_time' => $schedule_time]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 更改学生排课信息
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function changeScheduleData(Request $request)
|
|
{
|
|
try {
|
|
$data = $request->post();
|
|
$student_schedule_time = \app\common\model\StudentSchedule::where(['id' => $data['schedule_time_id']])->findOrEmpty();
|
|
if ($student_schedule_time->isEmpty()) {
|
|
throw new Exception('未找到排课时间');
|
|
}
|
|
$changeData = [];
|
|
if (isset($data['is_publish'])) {
|
|
$student_schedule_time->is_publish = $data['is_publish'];
|
|
if ($data['is_publish']) {
|
|
(new SendMsgCronJobService())->teacherScheduleTimePublishMsgToStudent($student_schedule_time->teacher_schedule_time_id);
|
|
}
|
|
}
|
|
|
|
$student_schedule_time->save();
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'data' => [],
|
|
'msg' => 'success'
|
|
]);
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'data' => [],
|
|
'msg' => 'success'
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 导出学生课表
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function exportStudentSchedule(Request $request)
|
|
{
|
|
try {
|
|
$data = $request->post();
|
|
|
|
if (!isset($data['student_id']) || empty($data['student_id'])) {
|
|
throw new Exception('请选择学生后导出');
|
|
}
|
|
|
|
if (!isset($data['month']) || empty($data['month'])) {
|
|
throw new Exception('请选择月份后导出');
|
|
}
|
|
|
|
$student_schedule = \app\common\model\StudentSchedule::order('start_time asc');
|
|
|
|
if (isset($data['student_id']) && !empty($data['student_id'])) {
|
|
$student_schedule->where(['student_id' => $data['student_id']]);
|
|
}
|
|
if (isset($data['teacher_id']) && !empty($data['teacher_id'])) {
|
|
$student_schedule->where(['teacher_id' => $data['teacher_id']]);
|
|
}
|
|
if (isset($data['subject_id']) && !empty($data['subject_id'])) {
|
|
$student_schedule->where(['subject_id' => $data['subject_id']]);
|
|
}
|
|
|
|
if (isset($data['month']) && !empty($data['month'])) {
|
|
$student_schedule->where(['month' => $data['month']]);
|
|
}
|
|
|
|
$publish_status = 'all';
|
|
|
|
if (isset($data['is_publish']) && !empty($data['is_publish'])) {
|
|
if($data['is_publish'] == 1){
|
|
$publish_status = 'published';
|
|
}else{
|
|
$publish_status = 'unpublished';
|
|
}
|
|
$student_schedule->where(['is_publish' => $data['is_publish']]);
|
|
}
|
|
$summary = $student_schedule->with(['teacher', 'subject'])->select()->toArray();
|
|
|
|
$export_data = [];
|
|
foreach ($summary as $item) {
|
|
$export_data[$item['date']][$item['time']][] = $item;
|
|
}
|
|
|
|
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
//设置工作表标题名称
|
|
$worksheet->setTitle('学生排课汇总');
|
|
$worksheet->setCellValue('A1', 'Week');
|
|
$worksheet->setCellValue('B1', 'Date');
|
|
$worksheet->setCellValue('C1', 'China TIme');
|
|
$worksheet->setCellValue('D1', 'Local Time(Tutor)');
|
|
$worksheet->setCellValue('E1', 'Subject');
|
|
$worksheet->setCellValue('F1', 'Tutor');
|
|
$worksheet->setCellValue('G1', 'Student');
|
|
$worksheet->setCellValue('H1', 'Duration');
|
|
$worksheet->setCellValue('I1', 'Note');
|
|
$worksheet->getStyle('A1:I1')->getFill()
|
|
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
|
->getStartColor()->setRGB('4f81bd');
|
|
|
|
|
|
$month_days = get_dates_in_month(date('Y', strtotime($data['month'])), date('m', strtotime($data['month'])));
|
|
|
|
// $column = Coordinate::columnIndexFromString(2);
|
|
|
|
$global_student_name = '';
|
|
$offset_row = 2;
|
|
foreach ($month_days as $index => $date) {
|
|
$week = date('l', strtotime($date));
|
|
|
|
if ($week == 'Sunday' && $index != 0) {
|
|
//合并单元格
|
|
$merge_offset = $index + $offset_row;
|
|
$worksheet->mergeCells("A{$merge_offset}:H{$merge_offset}");
|
|
$offset_row++;
|
|
}
|
|
|
|
$merge_count = 1;
|
|
$china_time = '';
|
|
$en_time = '';
|
|
$subject_name = '';
|
|
$teacher_name = '';
|
|
$student_name = '';
|
|
$row_publish_status = '';
|
|
$hour = '';
|
|
if (isset($export_data[$date])) {
|
|
$merge_count = count($export_data[$date]);
|
|
foreach ($export_data[$date] as $schedule) {
|
|
foreach ($schedule as $schedule_time) {
|
|
$china_time = $schedule_time['time'];
|
|
$en_time = $schedule_time['en_time'];
|
|
$subject_name = $schedule_time['english_name'];
|
|
$teacher_name = $schedule_time['teacher_name'];
|
|
$student_name = $schedule_time['student_name'];
|
|
$global_student_name = $student_name;
|
|
$row_publish_status = $schedule_time['is_publish']? 'published' : 'unpublished';
|
|
$hour = $schedule_time['hour'];
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
$row = $index + $offset_row;
|
|
if ($merge_count > 1) {
|
|
//合并单元格
|
|
$worksheet->mergeCells("A{$row}:A" . ($row + $merge_count - 1));
|
|
}
|
|
|
|
$worksheet->setCellValue('A' . $row, $week);
|
|
$worksheet->setCellValue('B' . $row, date('m/d', strtotime($date)));
|
|
$worksheet->setCellValue('C' . $row, $china_time);
|
|
$worksheet->setCellValue('D' . $row, $en_time);
|
|
$worksheet->setCellValue('E' . $row, $subject_name);
|
|
$worksheet->setCellValue('F' . $row, $teacher_name);
|
|
$worksheet->setCellValue('G' . $row, $student_name);
|
|
$worksheet->setCellValue('H' . $row, $hour);
|
|
$worksheet->setCellValue('I' . $row, $row_publish_status);
|
|
}
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
|
|
$file_name = $data['month'] . '-' . $global_student_name .'-' . $publish_status .'-' . time() . '.xlsx';
|
|
|
|
$file_path = '/export_file/';
|
|
$save_path = public_path($file_path);
|
|
if (!is_dir($save_path)) {
|
|
mkdir($save_path, 0777, true);
|
|
}
|
|
$writer->save($save_path . $file_name);
|
|
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_SUCCESS,
|
|
'data' => [
|
|
'file_url' => getenv('SERVER_DOMAIN') . $file_path . $file_name,
|
|
// 'file_url' => 'http://course.test' . $file_path . $file_name,
|
|
'file_name' => $file_name
|
|
],
|
|
'msg' => 'success'
|
|
]);
|
|
|
|
|
|
} catch (Exception $e) {
|
|
return json([
|
|
'code' => ResponseCode::WEB_API_FAIL,
|
|
'data' => [],
|
|
'msg' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
}
|