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:
@@ -1,9 +1,10 @@
|
||||
import "server-only"
|
||||
|
||||
import { and, asc, desc, eq, gte, lte } from "drizzle-orm"
|
||||
import { and, asc, count, desc, eq, gte, lte, sql } from "drizzle-orm"
|
||||
|
||||
import { db } from "@/shared/db"
|
||||
import { attendanceRecords, classes, users } from "@/shared/db/schema"
|
||||
import { safeParseDate } from "@/shared/lib/action-utils"
|
||||
|
||||
import type {
|
||||
AttendanceListItem,
|
||||
@@ -23,6 +24,9 @@ const EMPTY_STATS: AttendanceStats = {
|
||||
lateRate: 0,
|
||||
}
|
||||
|
||||
/** 最近记录的默认截取数量(避免一次拉全量) */
|
||||
const DEFAULT_RECENT_LIMIT = 20
|
||||
|
||||
/**
|
||||
* 根据考勤记录行计算统计(纯函数,便于测试)。
|
||||
*/
|
||||
@@ -41,13 +45,44 @@ export const computeStats = (rows: { status: string }[]): AttendanceStats => {
|
||||
return stats
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 SQL 聚合行转换为 AttendanceStats(避免拉全量记录计算统计)。
|
||||
*/
|
||||
const statsFromAggregate = (row: {
|
||||
total: number
|
||||
present: number
|
||||
absent: number
|
||||
late: number
|
||||
earlyLeave: number
|
||||
excused: number
|
||||
}): AttendanceStats => {
|
||||
const total = Number(row.total ?? 0)
|
||||
const present = Number(row.present ?? 0)
|
||||
const late = Number(row.late ?? 0)
|
||||
return {
|
||||
total,
|
||||
present,
|
||||
absent: Number(row.absent ?? 0),
|
||||
late,
|
||||
earlyLeave: Number(row.earlyLeave ?? 0),
|
||||
excused: Number(row.excused ?? 0),
|
||||
presentRate: total > 0 ? Math.round((present / total) * 10000) / 100 : 0,
|
||||
lateRate: total > 0 ? Math.round((late / total) * 10000) / 100 : 0,
|
||||
}
|
||||
}
|
||||
|
||||
const serializeDate = (d: Date | string | null): string =>
|
||||
d ? new Date(d).toISOString().slice(0, 10) : ""
|
||||
|
||||
/**
|
||||
* 获取学生考勤汇总。
|
||||
* 优化:统计使用 SQL 聚合查询(避免拉全量记录),最近记录使用 LIMIT 分页。
|
||||
*/
|
||||
export async function getStudentAttendanceSummary(
|
||||
studentId: string,
|
||||
startDate?: string,
|
||||
endDate?: string
|
||||
endDate?: string,
|
||||
recentLimit: number = DEFAULT_RECENT_LIMIT
|
||||
): Promise<StudentAttendanceSummary | null> {
|
||||
const [student] = await db
|
||||
.select({ name: users.name })
|
||||
@@ -57,9 +92,33 @@ export async function getStudentAttendanceSummary(
|
||||
if (!student) return null
|
||||
|
||||
const conditions = [eq(attendanceRecords.studentId, studentId)]
|
||||
if (startDate) conditions.push(gte(attendanceRecords.date, new Date(startDate)))
|
||||
if (endDate) conditions.push(lte(attendanceRecords.date, new Date(endDate)))
|
||||
if (startDate) conditions.push(gte(attendanceRecords.date, safeParseDate(startDate, "开始日期")))
|
||||
if (endDate) conditions.push(lte(attendanceRecords.date, safeParseDate(endDate, "结束日期")))
|
||||
const where = and(...conditions)
|
||||
|
||||
// 统计使用 SQL 聚合,避免拉全量记录
|
||||
const [statsRow] = await db
|
||||
.select({
|
||||
total: count(),
|
||||
present: sql<number>`COALESCE(SUM(CASE WHEN ${attendanceRecords.status} = 'present' THEN 1 ELSE 0 END), 0)`,
|
||||
absent: sql<number>`COALESCE(SUM(CASE WHEN ${attendanceRecords.status} = 'absent' THEN 1 ELSE 0 END), 0)`,
|
||||
late: sql<number>`COALESCE(SUM(CASE WHEN ${attendanceRecords.status} = 'late' THEN 1 ELSE 0 END), 0)`,
|
||||
earlyLeave: sql<number>`COALESCE(SUM(CASE WHEN ${attendanceRecords.status} = 'early_leave' THEN 1 ELSE 0 END), 0)`,
|
||||
excused: sql<number>`COALESCE(SUM(CASE WHEN ${attendanceRecords.status} = 'excused' THEN 1 ELSE 0 END), 0)`,
|
||||
})
|
||||
.from(attendanceRecords)
|
||||
.where(where)
|
||||
|
||||
const stats = statsFromAggregate(statsRow ?? {
|
||||
total: 0,
|
||||
present: 0,
|
||||
absent: 0,
|
||||
late: 0,
|
||||
earlyLeave: 0,
|
||||
excused: 0,
|
||||
})
|
||||
|
||||
// 最近记录使用 LIMIT 分页,避免拉全量
|
||||
const rows = await db
|
||||
.select({
|
||||
record: attendanceRecords,
|
||||
@@ -67,12 +126,11 @@ export async function getStudentAttendanceSummary(
|
||||
})
|
||||
.from(attendanceRecords)
|
||||
.leftJoin(classes, eq(classes.id, attendanceRecords.classId))
|
||||
.where(and(...conditions))
|
||||
.where(where)
|
||||
.orderBy(desc(attendanceRecords.date))
|
||||
.limit(recentLimit)
|
||||
|
||||
const stats = computeStats(rows.map((r) => ({ status: r.record.status })))
|
||||
|
||||
const recentRecords: AttendanceListItem[] = rows.slice(0, 20).map((r) => ({
|
||||
const recentRecords: AttendanceListItem[] = rows.map((r) => ({
|
||||
id: r.record.id,
|
||||
studentId: r.record.studentId,
|
||||
studentName: student.name ?? "Unknown",
|
||||
@@ -108,8 +166,8 @@ export async function getClassAttendanceStats(
|
||||
if (!classRow) return null
|
||||
|
||||
const conditions = [eq(attendanceRecords.classId, classId)]
|
||||
if (startDate) conditions.push(gte(attendanceRecords.date, new Date(startDate)))
|
||||
if (endDate) conditions.push(lte(attendanceRecords.date, new Date(endDate)))
|
||||
if (startDate) conditions.push(gte(attendanceRecords.date, safeParseDate(startDate, "开始日期")))
|
||||
if (endDate) conditions.push(lte(attendanceRecords.date, safeParseDate(endDate, "结束日期")))
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
|
||||
Reference in New Issue
Block a user