feat(attendance,elective): 实现所有 P2 长期改进项
P2 修复(来自审计报告): - 2.4.4: Server Action 错误消息 i18n 化(attendance/elective 全部 Action) - 2.5.3: 抽取 AttendancePageLayout 组件复用(admin/teacher 页面) - 2.5.4: 抽取 ElectivePageLayout 组件复用(admin/teacher 列表页) - 2.6.3: 考勤月历键盘导航(tabIndex + 方向键 + Home/End + role=grid) - 2.8.2: getStudentAttendanceSummary 分页优化(SQL 聚合统计 + LIMIT 分页) - 2.8.3: resolveCourseDisplayNames 缓存优化(React cache 去重) - 2.1.4: elective data-access 跨模块依赖接口抽象(resolvers.ts 可注入) P2 建议项: - 选课时间冲突检测(parseSchedule + isScheduleConflict 纯函数 + checkScheduleConflict) - 学分上限校验(MAX_CREDIT_PER_TERM + checkCreditLimit) - 考勤/选课数据导出 Excel(export.ts + API 路由扩展) 新增文件: - src/modules/attendance/components/attendance-page-layout.tsx - src/modules/elective/components/elective-page-layout.tsx - src/modules/elective/resolvers.ts - src/modules/attendance/export.ts - src/modules/elective/export.ts 校验: - npm run lint 通过(exit 0) - npx tsc --noEmit attendance/elective/parent 相关零错误
This commit is contained in:
102
src/modules/elective/export.ts
Normal file
102
src/modules/elective/export.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import "server-only"
|
||||
|
||||
import { getTranslations } from "next-intl/server"
|
||||
|
||||
import { exportToExcel } from "@/shared/lib/excel"
|
||||
|
||||
import { getElectiveCourses } from "./data-access"
|
||||
import { getCourseSelections } from "./data-access-selections"
|
||||
|
||||
/**
|
||||
* 导出选修课课程列表到 Excel
|
||||
* Sheet 1: 课程明细
|
||||
*/
|
||||
export async function exportElectiveCoursesToExcel(params: {
|
||||
status?: string
|
||||
teacherId?: string
|
||||
}): Promise<Buffer> {
|
||||
const t = await getTranslations("elective")
|
||||
|
||||
const courses = await getElectiveCourses({
|
||||
status: params.status as "draft" | "open" | "closed" | "cancelled" | undefined,
|
||||
teacherId: params.teacherId,
|
||||
})
|
||||
|
||||
const rows = courses.map((c) => ({
|
||||
[t("fields.name")]: c.name,
|
||||
[t("fields.teacher")]: c.teacherName ?? "",
|
||||
[t("fields.subject")]: c.subjectName ?? "",
|
||||
[t("fields.grade")]: c.gradeName ?? "",
|
||||
[t("fields.capacity")]: c.capacity,
|
||||
[t("fields.enrolled")]: c.enrolledCount,
|
||||
[t("fields.classroom")]: c.classroom ?? "",
|
||||
[t("fields.schedule")]: c.schedule ?? "",
|
||||
[t("fields.credit")]: c.credit,
|
||||
[t("fields.selectionMode")]: t(`selectionMode.${c.selectionMode}`),
|
||||
status: t(`status.${c.status}`),
|
||||
[t("fields.startDate")]: c.startDate ?? "",
|
||||
[t("fields.endDate")]: c.endDate ?? "",
|
||||
}))
|
||||
|
||||
return exportToExcel({
|
||||
sheets: [
|
||||
{
|
||||
name: t("title.adminList"),
|
||||
columns: [
|
||||
{ header: t("fields.name"), key: t("fields.name"), width: 24 },
|
||||
{ header: t("fields.teacher"), key: t("fields.teacher"), width: 16 },
|
||||
{ header: t("fields.subject"), key: t("fields.subject"), width: 14 },
|
||||
{ header: t("fields.grade"), key: t("fields.grade"), width: 12 },
|
||||
{ header: t("fields.capacity"), key: t("fields.capacity"), width: 10 },
|
||||
{ header: t("fields.enrolled"), key: t("fields.enrolled"), width: 10 },
|
||||
{ header: t("fields.classroom"), key: t("fields.classroom"), width: 14 },
|
||||
{ header: t("fields.schedule"), key: t("fields.schedule"), width: 20 },
|
||||
{ header: t("fields.credit"), key: t("fields.credit"), width: 8 },
|
||||
{ header: t("fields.selectionMode"), key: t("fields.selectionMode"), width: 16 },
|
||||
{ header: "Status", key: "status", width: 12 },
|
||||
{ header: t("fields.startDate"), key: t("fields.startDate"), width: 14 },
|
||||
{ header: t("fields.endDate"), key: t("fields.endDate"), width: 14 },
|
||||
],
|
||||
rows,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出课程选课名单到 Excel
|
||||
* Sheet 1: 选课名单
|
||||
*/
|
||||
export async function exportCourseSelectionsToExcel(params: {
|
||||
courseId: string
|
||||
}): Promise<Buffer> {
|
||||
const t = await getTranslations("elective")
|
||||
|
||||
const selections = await getCourseSelections(params.courseId)
|
||||
|
||||
const rows = selections.map((s, idx) => ({
|
||||
"#": idx + 1,
|
||||
[t("fields.name")]: s.studentName ?? "",
|
||||
status: t(`selectionStatus.${s.status}`),
|
||||
priority: s.priority ?? 1,
|
||||
selectedAt: s.selectedAt.split("T")[0],
|
||||
enrolledAt: s.enrolledAt ? s.enrolledAt.split("T")[0] : "",
|
||||
}))
|
||||
|
||||
return exportToExcel({
|
||||
sheets: [
|
||||
{
|
||||
name: t("student.mySelections"),
|
||||
columns: [
|
||||
{ header: "#", key: "#", width: 6 },
|
||||
{ header: t("fields.name"), key: t("fields.name"), width: 18 },
|
||||
{ header: "Status", key: "status", width: 12 },
|
||||
{ header: "Priority", key: "priority", width: 10 },
|
||||
{ header: "Selected At", key: "selectedAt", width: 14 },
|
||||
{ header: "Enrolled At", key: "enrolledAt", width: 14 },
|
||||
],
|
||||
rows,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user