feat: add class roster export and attendance stats export

- GET /classes/:id/roster/export — Excel export with 姓名/学号/加入日期/状态
- GET /attendance-records/export — Excel export with 姓名/班级/日期/时段/状态/来源/备注/打卡时间
- Add findAllForExport to AttendanceService (no pagination)
- Both use ExcelJS following classrooms controller downloadTemplate pattern
This commit is contained in:
2026-07-05 20:35:11 +08:00
parent 66aadfe3ff
commit 194f25c3ad
3 changed files with 137 additions and 0 deletions

View File

@@ -7,7 +7,9 @@ import {
Query,
UseGuards,
Request,
Res,
} from '@nestjs/common';
import type { Response } from 'express';
import { AttendanceService } from './attendance.service';
import {
BatchCreateAttendanceDto,
@@ -21,6 +23,7 @@ import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { OperationLogsService } from '../operation-logs/operation-logs.service';
import { extractRequestInfo } from '../common/request-utils';
import { RequirePermission } from '../auth/decorators/permission.decorator';
import * as ExcelJS from 'exceljs';
@UseGuards(JwtAuthGuard)
@Controller()
@@ -51,6 +54,61 @@ export class AttendanceController {
return result;
}
// ── Export attendance records ──
@Get('attendance-records/export')
@RequirePermission('attendance:view')
async exportRecords(
@Query() query: QueryAttendanceRecordsDto,
@Res() res: Response,
) {
const records = await this.service.findAllForExport(query);
const workbook = new ExcelJS.Workbook();
const ws = workbook.addWorksheet('考勤统计报表');
ws.columns = [
{ header: '姓名', key: 'studentName', width: 15 },
{ header: '班级', key: 'className', width: 20 },
{ header: '日期', key: 'attendanceDate', width: 15 },
{ header: '时段', key: 'session', width: 15 },
{ header: '状态', key: 'status', width: 10 },
{ header: '来源', key: 'source', width: 10 },
{ header: '备注', key: 'remark', width: 30 },
{ header: '打卡时间', key: 'createdAt', width: 20 },
];
ws.getRow(1).font = { bold: true };
ws.getRow(1).fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFE0E0E0' } };
for (const record of records) {
ws.addRow({
studentName: record.student?.name || '',
className: record.class?.name || '',
attendanceDate: record.attendanceDate || '',
session: record.session || '',
status: record.status || '',
source: record.source || '',
remark: record.remark || '',
createdAt: record.createdAt
? record.createdAt.toISOString().replace('T', ' ').substring(0, 19)
: '',
});
}
const dateRange = [query.dateFrom, query.dateTo]
.filter(Boolean)
.join('-') || '全部';
res.setHeader(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
);
res.setHeader(
'Content-Disposition',
`attachment; filename=${encodeURIComponent(`考勤统计报表-${dateRange}`)}.xlsx`,
);
await workbook.xlsx.write(res);
res.end();
}
// ── List attendance records with filters ──
@Get('attendance-records')
@RequirePermission('attendance:view')