diff --git a/app/functions.php b/app/functions.php index 9f32749..eb1af79 100644 --- a/app/functions.php +++ b/app/functions.php @@ -60,3 +60,24 @@ if (!function_exists('random_str')) { return bin2hex(random_bytes(round($len / 2))); } } + +if (!function_exists('get_dates_in_month')) { + /** + * @desc 获取月份所有天 + * @param $year + * @param $month + * @return array + */ + function get_dates_in_month($year, $month) { + $dates = []; + $firstDayOfMonth = new DateTime("$year-$month-01"); + $lastDayOfMonth = clone $firstDayOfMonth; + $lastDayOfMonth->modify('last day of this month'); + + for ($currentDate = $firstDayOfMonth; $currentDate <= $lastDayOfMonth; $currentDate->modify('+1 day')) { + $dates[] = $currentDate->format('Y-m-d'); + } + + return $dates; + } +} diff --git a/plugin/admin/app/controller/TeacherScheduleTimeController.php b/plugin/admin/app/controller/TeacherScheduleTimeController.php index a91b50a..ab59dac 100644 --- a/plugin/admin/app/controller/TeacherScheduleTimeController.php +++ b/plugin/admin/app/controller/TeacherScheduleTimeController.php @@ -8,6 +8,10 @@ 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; @@ -15,6 +19,7 @@ use plugin\admin\app\controller\Crud; use support\exception\BusinessException; use think\Exception; use DateTime; +use think\facade\Db; /** * 教师空闲时间 @@ -306,10 +311,10 @@ class TeacherScheduleTimeController extends Crud throw new Exception('未找到教师排课时间'); } $changeData = []; - if(isset($data['subject_id'])){ + if (isset($data['subject_id'])) { $teacher_schedule_time->subject_id = $data['subject_id']; } - if(isset($data['is_publish'])){ + if (isset($data['is_publish'])) { $teacher_schedule_time->is_publish = $data['is_publish']; } @@ -328,4 +333,198 @@ class TeacherScheduleTimeController extends Crud } } + + /** + * @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')->(); + + + $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' => [ +// 'url' => getenv('SERVER_DOMAIN') . $file_path, + '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() + ]); + } + } + } diff --git a/plugin/admin/app/view/teacher-schedule-time/export_schedule_index.html b/plugin/admin/app/view/teacher-schedule-time/export_schedule_index.html new file mode 100644 index 0000000..2646d85 --- /dev/null +++ b/plugin/admin/app/view/teacher-schedule-time/export_schedule_index.html @@ -0,0 +1,369 @@ + + + +
+ +