From 6ba7f4e3d0c30d27b6f3802fa73ddf345baddb2d Mon Sep 17 00:00:00 2001 From: wangziqi Date: Wed, 5 Aug 2026 18:51:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=BD=E5=8F=96=E8=AF=BE?= =?UTF-8?q?=E7=A8=8B=E8=80=83=E5=8B=A4=E8=AE=B0=E5=BD=95=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=20helper=EF=BC=8C=E6=B6=88=E9=99=A4=E9=87=8D=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../attendance/attendance-lesson.service.ts | 139 +++++++++++------- 1 file changed, 83 insertions(+), 56 deletions(-) diff --git a/apps/server/src/attendance/attendance-lesson.service.ts b/apps/server/src/attendance/attendance-lesson.service.ts index 779c19a..ed8d4e6 100644 --- a/apps/server/src/attendance/attendance-lesson.service.ts +++ b/apps/server/src/attendance/attendance-lesson.service.ts @@ -182,36 +182,18 @@ export class AttendanceLessonService { ); for (const classStudent of classStudents) { if (existingStudentIds.has(classStudent.studentId)) continue; - const raw = selectDingTalkRecordsForLesson( - rawByStudent.get(classStudent.studentId) ?? [], - schedule, - lessonDate, - ); - const resolved = await this.resolveLessonStatus( - classStudent.studentId, - raw, - schedule, - lessonDate, - effectiveFinalize, - ); updatedRecords.push( - recordRepo.create({ - studentId: classStudent.studentId, - student: classStudent.student, - classId: schedule.classId!, + await this.buildLessonRecord( + recordRepo, + rawByStudent, + classStudent, + schedule, + lessonDate, + lessonSessionKey, + existing.id, scheduleId, - attendanceSessionId: existing.id, - attendanceDate: lessonDate, - session: lessonSessionKey, - status: resolved.status, - source: 'dingtalk', - ...getLessonPunchMetadata( - raw, - lessonDate, - schedule.startTime, - ), - remark: resolved.remark, - }), + effectiveFinalize, + ), ); } @@ -274,37 +256,19 @@ export class AttendanceLessonService { const lessonSessionKey = mapLessonScheduleTimeToSession(schedule.startTime); const records = await Promise.all( - classStudents.map(async (classStudent) => { - const raw = selectDingTalkRecordsForLesson( - rawByStudent.get(classStudent.studentId) ?? [], + classStudents.map((classStudent) => + this.buildLessonRecord( + recordRepo, + rawByStudent, + classStudent, schedule, lessonDate, - ); - const resolved = await this.resolveLessonStatus( - classStudent.studentId, - raw, - schedule, - lessonDate, - finalize, - ); - return recordRepo.create({ - studentId: classStudent.studentId, - student: classStudent.student, - classId: schedule.classId!, + lessonSessionKey, + session.id, scheduleId, - attendanceSessionId: session.id, - attendanceDate: lessonDate, - session: lessonSessionKey, - status: resolved.status, - source: 'dingtalk', - ...getLessonPunchMetadata( - raw, - lessonDate, - schedule.startTime, - ), - remark: resolved.remark, - }); - }), + finalize, + ), + ), ); const saved = await recordRepo.save(records); if (finalize) { @@ -347,6 +311,69 @@ export class AttendanceLessonService { return { status: 'absent', remark: '课程截止仍未打卡' }; } + private createLessonRecord( + recordRepo: Repository, + classStudent: ClassStudent, + raw: DingAttendanceRaw[], + options: { + schedule: Pick; + scheduleId: number; + lessonDate: string; + lessonSessionKey: string; + attendanceSessionId: number; + status: string; + remark?: string; + }, + ): AttendanceRecord { + return recordRepo.create({ + studentId: classStudent.studentId, + student: classStudent.student, + classId: options.schedule.classId!, + scheduleId: options.scheduleId, + attendanceSessionId: options.attendanceSessionId, + attendanceDate: options.lessonDate, + session: options.lessonSessionKey, + status: options.status, + source: 'dingtalk', + ...getLessonPunchMetadata(raw, options.lessonDate, options.schedule.startTime), + remark: options.remark, + }); + } + + private async buildLessonRecord( + recordRepo: Repository, + rawByStudent: Map, + classStudent: ClassStudent, + schedule: Pick, + lessonDate: string, + lessonSessionKey: string, + attendanceSessionId: number, + scheduleId: number, + finalize: boolean, + ): Promise { + const raw = selectDingTalkRecordsForLesson( + rawByStudent.get(classStudent.studentId) ?? [], + schedule, + lessonDate, + ); + const resolved = await this.resolveLessonStatus( + classStudent.studentId, + raw, + schedule, + lessonDate, + finalize, + ); + return this.createLessonRecord(recordRepo, classStudent, raw, { + schedule, + scheduleId, + lessonDate, + lessonSessionKey, + attendanceSessionId, + status: resolved.status, + remark: resolved.remark, + }); + } + private async findApprovedLeaveForStudent( studentId: number, schedule: Pick,