forked from wangziqi/gongxue-base
fix: add Class repo to AttendanceService, fix getClasses API
This commit is contained in:
@@ -21,6 +21,8 @@ export class AttendanceService {
|
|||||||
private attendanceRepo: Repository<AttendanceRecord>,
|
private attendanceRepo: Repository<AttendanceRecord>,
|
||||||
@InjectRepository(DingAttendanceRaw)
|
@InjectRepository(DingAttendanceRaw)
|
||||||
private dingRawRepo: Repository<DingAttendanceRaw>,
|
private dingRawRepo: Repository<DingAttendanceRaw>,
|
||||||
|
@InjectRepository(Class)
|
||||||
|
private classRepo: Repository<Class>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
// ── Batch create attendance records ──
|
// ── Batch create attendance records ──
|
||||||
@@ -182,15 +184,19 @@ export class AttendanceService {
|
|||||||
|
|
||||||
// ── Get distinct classes with attendance records ──
|
// ── Get distinct classes with attendance records ──
|
||||||
async getClasses() {
|
async getClasses() {
|
||||||
const qb = this.attendanceRepo
|
const rows = await this.attendanceRepo
|
||||||
.createQueryBuilder('ar')
|
.createQueryBuilder('ar')
|
||||||
.leftJoin('ar.class', 'class')
|
|
||||||
.select('DISTINCT ar.classId', 'classId')
|
.select('DISTINCT ar.classId', 'classId')
|
||||||
.addSelect('class.name', 'className')
|
|
||||||
.where('ar.classId IS NOT NULL')
|
.where('ar.classId IS NOT NULL')
|
||||||
.orderBy('ar.classId', 'ASC');
|
.orderBy('ar.classId', 'ASC')
|
||||||
|
.getRawMany();
|
||||||
|
|
||||||
return qb.getRawMany();
|
const classIds = rows.map((r) => r.classId).filter(Boolean) as number[];
|
||||||
|
if (classIds.length === 0) return [];
|
||||||
|
|
||||||
|
const classes = await this.classRepo.find({ where: { id: In(classIds) } });
|
||||||
|
const nameMap = new Map(classes.map((c) => [c.id, c.name]));
|
||||||
|
return classIds.map((id) => ({ classId: id, className: nameMap.get(id) || `班级${id}` }));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── DingAttendance raw records ──
|
// ── DingAttendance raw records ──
|
||||||
|
|||||||
Reference in New Issue
Block a user