forked from wangziqi/gongxue-base
fix: use real name and phone from frontend when importing students
This commit is contained in:
@@ -94,7 +94,7 @@ export class ClassesController {
|
||||
@Param('id') id: string,
|
||||
@Body() dto: BatchImportStudentsDto,
|
||||
) {
|
||||
return this.service.batchImportStudents(+id, dto.dingUserIds);
|
||||
return this.service.batchImportStudents(+id, dto.users);
|
||||
}
|
||||
|
||||
/** 归档班级 */
|
||||
|
||||
@@ -94,7 +94,7 @@ export class ClassesService {
|
||||
}
|
||||
|
||||
async create(dto: CreateClassDto) {
|
||||
const { studentIds, teachers, dingUserIds, ...classData } = dto;
|
||||
const { studentIds, teachers, users, ...classData } = dto;
|
||||
|
||||
|
||||
const cls = this.classRepo.create(classData);
|
||||
@@ -120,19 +120,23 @@ export class ClassesService {
|
||||
}
|
||||
|
||||
// batch import students by dingUserIds
|
||||
if (dingUserIds?.length) {
|
||||
await this.batchImportStudents(saved.id, dingUserIds);
|
||||
if (users?.length) {
|
||||
await this.batchImportStudents(saved.id, users);
|
||||
}
|
||||
|
||||
return this.findOne(saved.id);
|
||||
}
|
||||
|
||||
|
||||
async batchImportStudents(classId: number, dingUserIds: string[]): Promise<{ imported: number; skipped: number }> {
|
||||
async batchImportStudents(classId: number, users: Array<{
|
||||
dingUserId: string; name: string; mobile?: string;
|
||||
}>): Promise<{ imported: number; skipped: number }> {
|
||||
const classEntity = await this.classRepo.findOne({ where: { id: classId } });
|
||||
if (!classEntity) throw new NotFoundException('班级不存在');
|
||||
|
||||
if (dingUserIds.length === 0) return { imported: 0, skipped: 0 };
|
||||
if (users.length === 0) return { imported: 0, skipped: 0 };
|
||||
|
||||
const dingUserIds = users.map(u => u.dingUserId);
|
||||
|
||||
// 1. Fetch all existing ding mappings in one query
|
||||
const existingMappings = await this.studentDingMappingRepo.find({
|
||||
@@ -141,23 +145,24 @@ export class ClassesService {
|
||||
const dingToStudentId = new Map(existingMappings.map(m => [m.dingUserId, m.studentId]));
|
||||
|
||||
// 2. Batch create students for new dingUserIds
|
||||
const newDingUserIds = dingUserIds.filter(id => !dingToStudentId.has(id));
|
||||
if (newDingUserIds.length > 0) {
|
||||
const newStudents = newDingUserIds.map(dingUserId =>
|
||||
const newUsers = users.filter(u => !dingToStudentId.has(u.dingUserId));
|
||||
if (newUsers.length > 0) {
|
||||
const newStudents = newUsers.map(u =>
|
||||
this.studentRepo.create({
|
||||
name: `dd_${dingUserId}`,
|
||||
name: u.name,
|
||||
phone: u.mobile || `dt_${u.dingUserId}`,
|
||||
status: 'active',
|
||||
})
|
||||
);
|
||||
const savedStudents = await this.studentRepo.save(newStudents);
|
||||
|
||||
const newMappings = savedStudents.map((s, i) =>
|
||||
this.studentDingMappingRepo.create({ dingUserId: newDingUserIds[i], studentId: s.id })
|
||||
this.studentDingMappingRepo.create({ dingUserId: newUsers[i].dingUserId, studentId: s.id })
|
||||
);
|
||||
await this.studentDingMappingRepo.save(newMappings);
|
||||
|
||||
for (let i = 0; i < newDingUserIds.length; i++) {
|
||||
dingToStudentId.set(newDingUserIds[i], savedStudents[i].id);
|
||||
for (let i = 0; i < newUsers.length; i++) {
|
||||
dingToStudentId.set(newUsers[i].dingUserId, savedStudents[i].id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user