fix 导出学生课表

This commit is contained in:
Dai 2024-10-20 21:28:24 +08:00
parent ced5fb9a69
commit 1982c453f7
3 changed files with 230 additions and 26 deletions

View File

@ -8,6 +8,8 @@ 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;
@ -88,6 +90,10 @@ class StudentScheduleController extends Crud
$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,
@ -364,4 +370,160 @@ class StudentScheduleController extends Crud
}
}
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()
]);
}
}
}

View File

@ -11,7 +11,7 @@
<!-- 顶部查询表单 -->
<div class="layui-card">
<div class="layui-card-body">
<form class="layui-form top-search-from">
<form class="layui-form top-search-from" lay-filter="search-val-filter">
<div class="layui-form-item">
<label class="layui-form-label">学生</label>
@ -117,6 +117,10 @@
permission="app.admin.studentschedule.insert">
<i class="layui-icon layui-icon-down"></i>取消发布
</button>
<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="export-student-schedule"
permission="app.admin.studentschedule.insert">
<i class="layui-icon layui-icon-down"></i>导出学生课表
</button>
<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove"
permission="app.admin.studentschedule.delete">
<i class="layui-icon layui-icon-delete"></i>删除
@ -194,6 +198,28 @@
}, {
title: "id", align: "center",
field: "id",
}, {
title: "星期", align: "center",
field: "week",
width: 80
}, {
title: "日期", align: "center",
field: "date",
width: 150
}, {
title: "中国时间", align: "center",
field: "time",
width: 120
}, {
title: "本地时间", align: "center",
field: "en_time",
width: 120
}, {
title: "学科", align: "center",
field: "subject_name",
}, {
title: "教师", align: "center",
field: "teacher_name",
},{
title: "学生", align: "center",
field: "student_name",
@ -206,24 +232,6 @@
title: "排课详情", align: "center",
field: "teacher_schedule_time_detail",
hide: true
}, {
title: "教师", align: "center",
field: "teacher_name",
}, {
title: "学科", align: "center",
field: "subject_name",
}, {
title: "日期", align: "center",
field: "date",
width: 150
}, {
title: "本地时间", align: "center",
field: "en_time",
width: 120
}, {
title: "中国时间", align: "center",
field: "time",
width: 120
}, {
title: "课时", align: "center",
field: "hour",
@ -323,6 +331,8 @@
publish(obj, 1);
} else if (obj.event === "unpublished") {
publish(obj, 0);
} else if (obj.event === "export-student-schedule") {
exportStudentSchedule(obj, 0);
} else if (obj.event === "refresh") {
refreshTable();
} else if (obj.event === "batchRemove") {
@ -458,6 +468,38 @@
});
}
let exportStudentSchedule = function (obj, status) {
var data = form.val('search-val-filter');
// let loading = layer.load();
$.ajax({
url: '/app/admin/studentSchedule/exportStudentSchedule',
data: data,
dataType: "json",
type: "post",
success: function (res) {
// layer.close(loading);
if (res.code) {
return layui.popup.failure(res.msg);
}
const link = document.createElement('a');
link.style.display = 'none';
// 设置下载地址
link.setAttribute('href', res.data.file_url);
// 设置文件名
link.setAttribute('download', res.data.file_name);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
return layui.popup.success("操作成功", function () {
parent.refreshTable();
parent.layer.close(parent.layer.getFrameIndex(window.name));
});
}
})
}
// 删除一行
let remove = function (obj) {
return doRemove(obj.data[PRIMARY_KEY]);