course/plugin/admin/app/controller/TeacherScheduleTimeController.php
2024-08-13 15:34:04 +08:00

533 lines
19 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\TeacherFreeTime;
use app\constant\ResponseCode;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use plugin\admin\app\model\TeacherSchedule;
use support\Request;
use support\Response;
use plugin\admin\app\model\TeacherScheduleTime;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use think\Exception;
use DateTime;
use think\facade\Db;
/**
* 教师空闲时间
*/
class TeacherScheduleTimeController extends Crud
{
/**
* @var TeacherScheduleTime
*/
protected $model = null;
/**
* 构造函数
* @return void
*/
public function __construct()
{
$this->model = new TeacherScheduleTime;
}
/**
* 浏览
* @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();
return view('teacher-schedule-time/index', ['teacher' => $teacher, 'subject' => $subject]);
}
public function select(Request $request): Response
{
try {
$data = $request->get();
$teacherSchedule = \app\common\model\TeacherScheduleTime::order('id desc');
if (isset($data['teacher_id']) && $data['teacher_id']) {
$teacherSchedule->where(['teacher_id' => $data['teacher_id']]);
}
if (isset($data['date']) && $data['date']) {
$teacherSchedule->where(['date' => $data['date']]);
}
if (isset($data['month']) && $data['month']) {
$teacherSchedule->where(['month' => $data['month']]);
}
if (isset($data['subject_id']) && $data['subject_id']) {
$teacherSchedule->where(['subject_id' => $data['subject_id']]);
}
if (isset($data['is_publish']) && $data['is_publish'] !== '') {
$teacherSchedule->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 = $teacherSchedule->count();
$list = $teacherSchedule->with(['subject', 'teacher'])->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()
]);
}
}
/**
* 插入
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function insert(Request $request): Response
{
if ($request->method() === 'POST') {
return parent::insert($request);
}
return view('teacher-schedule-time/insert');
}
/**
* @desc 添加教师排课时间
* @param Request $request
* @return Response
* @throws \Exception
*/
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_en_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->en_start_time) <= $start_time && strtotime($free_time->en_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 $index => $item) {
$time_period = explode(' - ', $item);
$en_time_period = explode(' - ', $data['schedule_en_time'][$index]);
$start_time = $free_time->date . ' ' . trim($time_period[0]);
$end_time = $free_time->date . ' ' . trim($time_period[1]);
$en_start_time = $free_time->date . ' ' . trim($en_time_period[0]);
$en_end_time = $free_time->date . ' ' . trim($en_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 = trim($time_period[0]) . ' - ' . trim($time_period[1]);
\app\common\model\TeacherScheduleTime::create([
'teacher_id' => $free_time->teacher_id,
'date' => $free_time->date,
'time' => $time,
'en_time' => $data['schedule_en_time'][$index],
'hour' => $hour,
'start_time' => $start_time,
'end_time' => $end_time,
'en_start_time' => $en_start_time,
'en_end_time' => $en_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
* @return Response
* @throws BusinessException
*/
public function update(Request $request): Response
{
if ($request->method() === 'POST') {
return parent::update($request);
}
$teacher = [];
$init_date = '';
if ($request->get('id')) {
$teacher_schedule_time = \app\common\model\TeacherScheduleTime::where(['id' => $request->get('id')])->findOrEmpty();
$init_date = $teacher_schedule_time->date;
$teacher = Teacher::where(['id' => $teacher_schedule_time->teacher_id])->field('id,teacher_name,account')->findOrEmpty()->toArray();
}
return view('teacher-schedule-time/update', ['teacher' => $teacher, 'init_date' => $init_date]);
}
/**
* @desc 时间列表
* @param Request $request
* @return Response
*/
public function getScheduleTime(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\TeacherScheduleTime::where(['teacher_id' => $teacher_id])
->whereBetweenTime('start_time', $start_date, $end_date)
->with(['subject', 'studentSchedule'])
->select()
->toArray();
$schedule_time = [];
foreach ($list as $item) {
$time_period = explode('-', $item['time']);
if ($item['subject_id']) {
$title = $item['time'] . ' - ' . $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()
]);
}
}
/**
* 更新
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function scheduleTimeSet(Request $request): Response
{
if ($request->method() === 'POST') {
if ($request->post('is_publish')) {
$request->post('is_publish', 0);
}
// parent::update($request);
return parent::update($request);
}
$schedule_time_id = $request->get('schedule_time_id');
$schedule_time = \app\common\model\TeacherScheduleTime::where(['id' => $schedule_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();
$student_schedule = StudentSchedule::where(['teacher_schedule_time_id' => $schedule_time_id])->select()->toArray();
$student_schedule_id = [];
if ($student_schedule) {
$student_schedule_id = array_column($student_schedule, 'student_id');
}
return view('teacher-schedule-time/schedule_time_setting', [
'schedule_time' => $schedule_time,
'project' => $project,
'student' => $student,
'student_schedule_id' => $student_schedule_id
]);
}
/**
* @desc 更改教师排课数据
* @param Request $request
* @return Response
*/
public function changeScheduleData(Request $request)
{
try {
$data = $request->post();
$teacher_schedule_time = \app\common\model\TeacherScheduleTime::where(['id' => $data['teacher_schedule_time_id']])->findOrEmpty();
if ($teacher_schedule_time->isEmpty()) {
throw new Exception('未找到教师排课时间');
}
$changeData = [];
if (isset($data['subject_id'])) {
$teacher_schedule_time->subject_id = $data['subject_id'];
}
if (isset($data['is_publish'])) {
$teacher_schedule_time->is_publish = $data['is_publish'];
}
$teacher_schedule_time->save();
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'data' => [],
'msg' => 'success'
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
}
/**
* @desc 导出排课页面
* @param Request $request
* @return Response
*/
public function exportScheduleIndex(Request $request)
{
return view('teacher-schedule-time/export_schedule_index');
}
/**
* @desc 所有教师排课汇总
* @param Request $request
*/
public function getTeacherScheduleTimeSummary(Request $request)
{
try {
$summary = \app\common\model\TeacherScheduleTime::order('ts.month desc')->alias('ts')
->leftJoin('teacher t', 'ts.teacher_id = t.id')
->leftJoin('student_schedule ss', 'ts.id = ss.teacher_schedule_time_id and t.id = ss.teacher_id')
// ->where(['ts.is_publish' => 1])
->field('
ts.month,
COUNT(DISTINCT ts.teacher_id) AS teacher_count,
SUM(ts.hour) AS total_scheduled_hours,
SUM(CASE WHEN ts.is_publish = 1 THEN ts.hour ELSE 0 END) AS publish_scheduled_hours,
COUNT(DISTINCT ts.id) AS total_scheduled_classes,
COUNT(DISTINCT CASE WHEN ts.is_publish = 1 THEN ts.id END) AS publish_scheduled_classes,
SUM(ss.hour) AS total_student_hours,
SUM(CASE WHEN ss.is_publish = 1 THEN ss.hour ELSE 0 END) AS publish_student_hours,
COUNT(DISTINCT ss.teacher_schedule_time_id) AS total_student_classes,
COUNT(DISTINCT CASE WHEN ss.is_publish = 1 THEN ss.teacher_schedule_time_id END ) AS publish_student_classes
')
->group('ts.month');
$limit = (int)$request->get('limit', 10);
$limit = $limit <= 0 ? 10 : $limit;
$page = (int)$request->get('page');
$page = $page > 0 ? $page : 1;
$total = $summary->count();
$list = $summary->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 exportSummary(Request $request)
{
try {
$month = $request->get('month');
$summary = \app\common\model\TeacherScheduleTime::order('ts.start_time asc')->alias('ts')
->leftJoin('teacher t', 'ts.teacher_id = t.id')
->leftJoin('student_schedule ss', 'ts.id = ss.teacher_schedule_time_id and t.id = ss.teacher_id')
->leftJoin('subject sb', 'ts.subject_id = sb.id')
->where(['ts.is_publish' => 1, 'ts.month' => $month])
->field('
t.teacher_name,
ts.time,
ts.en_time,
ts.date,
ts.month,
ts.hour,
ts.start_time,
ts.end_time,
sb.subject_name,
sb.english_name,
ss.student_name
')
->group('ts.id')
->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', 'UK Time');
$worksheet->setCellValue('E1', 'Subject');
$worksheet->setCellValue('F1', 'Tutor');
$worksheet->setCellValue('G1', 'Duration');
$worksheet->setCellValue('H1', 'Note');
$worksheet->getStyle('A1:H1')->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setRGB('4f81bd');
$month_days = get_dates_in_month(date('Y', strtotime($month)), date('m', strtotime($month)));
// $column = Coordinate::columnIndexFromString(2);
$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 = '';
$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'];
$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, $hour);
$worksheet->setCellValue('H' . $row, '');
}
$writer = new Xlsx($spreadsheet);
// $writer->save(public_path('/export_file/' . $month . '-' . time() . '.xlsx'));
$file_name = $month . '-' . 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,
'msg' => $e->getMessage()
]);
}
}
}