forked from wangziqi/gongxue-base
refactor(classes): preserve student membership history
This commit is contained in:
@@ -227,34 +227,45 @@ export class ClassesService {
|
||||
}
|
||||
|
||||
// 3. Fetch existing class-student links in one query
|
||||
const allStudentIds = Array.from(dingToStudentId.values());
|
||||
const alreadyInClass = new Set<number>();
|
||||
if (allStudentIds.length > 0) {
|
||||
const existingClassStudents = await this.classStudentRepo.find({
|
||||
where: { classId, studentId: In(allStudentIds) },
|
||||
});
|
||||
for (const cs of existingClassStudents) {
|
||||
alreadyInClass.add(cs.studentId);
|
||||
const allStudentIds = Array.from(new Set(dingToStudentId.values()));
|
||||
const existingClassStudents =
|
||||
allStudentIds.length > 0
|
||||
? await this.classStudentRepo.find({
|
||||
where: { classId, studentId: In(allStudentIds) },
|
||||
})
|
||||
: [];
|
||||
const existingByStudentId = new Map(
|
||||
existingClassStudents.map((classStudent) => [classStudent.studentId, classStudent]),
|
||||
);
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
let skipped = 0;
|
||||
const memberships = allStudentIds.flatMap((studentId) => {
|
||||
const existing = existingByStudentId.get(studentId);
|
||||
if (existing?.status === 'active') {
|
||||
skipped++;
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Batch insert new class-student records
|
||||
const newClassStudents = allStudentIds
|
||||
.filter((sid) => !alreadyInClass.has(sid))
|
||||
.map((studentId) =>
|
||||
if (existing) {
|
||||
existing.status = 'active';
|
||||
existing.joinDate = today;
|
||||
existing.leaveDate = null;
|
||||
return [existing];
|
||||
}
|
||||
return [
|
||||
this.classStudentRepo.create({
|
||||
classId,
|
||||
studentId,
|
||||
status: 'active',
|
||||
joinDate: new Date().toISOString().slice(0, 10),
|
||||
joinDate: today,
|
||||
}),
|
||||
);
|
||||
];
|
||||
});
|
||||
|
||||
if (newClassStudents.length > 0) {
|
||||
await this.classStudentRepo.save(newClassStudents);
|
||||
if (memberships.length > 0) {
|
||||
await this.classStudentRepo.save(memberships);
|
||||
}
|
||||
|
||||
return { imported: newClassStudents.length, skipped: alreadyInClass.size };
|
||||
return { imported: memberships.length, skipped };
|
||||
}
|
||||
async update(id: number, dto: UpdateClassDto) {
|
||||
const cls = await this.classRepo.findOne({ where: { id } });
|
||||
@@ -315,26 +326,61 @@ export class ClassesService {
|
||||
}
|
||||
|
||||
async addStudents(classId: number, studentIds: number[]) {
|
||||
const uniqueStudentIds = [...new Set(studentIds)];
|
||||
if (uniqueStudentIds.length === 0) return { added: 0, skipped: 0 };
|
||||
|
||||
const cls = await this.classRepo.findOne({ where: { id: classId } });
|
||||
if (!cls) throw new NotFoundException('班级不存在');
|
||||
|
||||
const students = await this.studentRepo.find({ where: { id: In(uniqueStudentIds) } });
|
||||
if (students.length !== uniqueStudentIds.length) {
|
||||
throw new NotFoundException('部分学生不存在');
|
||||
}
|
||||
|
||||
const existing = await this.classStudentRepo.find({
|
||||
where: { classId, studentId: In(studentIds) },
|
||||
where: { classId, studentId: In(uniqueStudentIds) },
|
||||
});
|
||||
const existingIds = new Set(existing.map((e) => e.studentId));
|
||||
const newIds = studentIds.filter((id) => !existingIds.has(id));
|
||||
|
||||
const entries = newIds.map((sid) =>
|
||||
this.classStudentRepo.create({
|
||||
classId,
|
||||
studentId: sid,
|
||||
joinDate: new Date().toISOString().split('T')[0],
|
||||
}),
|
||||
const existingByStudentId = new Map(
|
||||
existing.map((classStudent) => [classStudent.studentId, classStudent]),
|
||||
);
|
||||
if (entries.length) await this.classStudentRepo.save(entries);
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
let skipped = 0;
|
||||
const memberships = uniqueStudentIds.flatMap((studentId) => {
|
||||
const current = existingByStudentId.get(studentId);
|
||||
if (current?.status === 'active') {
|
||||
skipped++;
|
||||
return [];
|
||||
}
|
||||
if (current) {
|
||||
current.status = 'active';
|
||||
current.joinDate = today;
|
||||
current.leaveDate = null;
|
||||
return [current];
|
||||
}
|
||||
return [
|
||||
this.classStudentRepo.create({
|
||||
classId,
|
||||
studentId,
|
||||
status: 'active',
|
||||
joinDate: today,
|
||||
}),
|
||||
];
|
||||
});
|
||||
if (memberships.length) await this.classStudentRepo.save(memberships);
|
||||
|
||||
return { added: entries.length, skipped: studentIds.length - entries.length };
|
||||
return { added: memberships.length, skipped };
|
||||
}
|
||||
|
||||
async removeStudent(classId: number, studentId: number) {
|
||||
await this.classStudentRepo.delete({ classId, studentId });
|
||||
const membership = await this.classStudentRepo.findOne({
|
||||
where: { classId, studentId },
|
||||
});
|
||||
if (!membership) throw new NotFoundException('学生不在该班级');
|
||||
if (membership.status !== 'active') throw new BadRequestException('学生已离班');
|
||||
|
||||
membership.status = 'left';
|
||||
membership.leaveDate = new Date().toISOString().split('T')[0];
|
||||
await this.classStudentRepo.save(membership);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user