feat: add batch-import students to class endpoint
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, In, Like } from 'typeorm';
|
||||
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Department, Classroom } from '../entities';
|
||||
import { CreateClassDto, UpdateClassDto, QueryClassDto, AddTeacherDto, QueryClassScheduleDto, QueryClassAttendanceSummaryDto } from './dto/class.dto';
|
||||
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Department, Classroom, Student, StudentDingMapping } from '../entities';
|
||||
import { CreateClassDto, UpdateClassDto, QueryClassDto, AddTeacherDto, QueryClassScheduleDto, QueryClassAttendanceSummaryDto, BatchImportStudentsDto } from './dto/class.dto';
|
||||
|
||||
interface RawStudentCount {
|
||||
classId: string;
|
||||
@@ -24,6 +24,10 @@ export class ClassesService {
|
||||
private attendanceRepo: Repository<AttendanceRecord>,
|
||||
@InjectRepository(Department)
|
||||
private deptRepo: Repository<Department>,
|
||||
@InjectRepository(Student)
|
||||
private studentRepo: Repository<Student>,
|
||||
@InjectRepository(StudentDingMapping)
|
||||
private studentDingMappingRepo: Repository<StudentDingMapping>,
|
||||
) {}
|
||||
|
||||
async findAll(query: QueryClassDto) {
|
||||
@@ -93,7 +97,7 @@ export class ClassesService {
|
||||
}
|
||||
|
||||
async create(dto: CreateClassDto) {
|
||||
const { studentIds, teachers, ...classData } = dto;
|
||||
const { studentIds, teachers, dingUserIds, ...classData } = dto;
|
||||
|
||||
const cls = this.classRepo.create(classData);
|
||||
const saved = await this.classRepo.save(cls);
|
||||
@@ -117,9 +121,56 @@ export class ClassesService {
|
||||
await this.syncClassTeacherIds(saved.id);
|
||||
}
|
||||
|
||||
// batch import students by dingUserIds
|
||||
if (dingUserIds?.length) {
|
||||
await this.batchImportStudents(saved.id, dingUserIds);
|
||||
}
|
||||
|
||||
return this.findOne(saved.id);
|
||||
}
|
||||
|
||||
|
||||
async batchImportStudents(classId: number, dingUserIds: string[]): Promise<{ imported: number; skipped: number }> {
|
||||
const classEntity = await this.classRepo.findOne({ where: { id: classId } });
|
||||
if (!classEntity) throw new NotFoundException('班级不存在');
|
||||
|
||||
let imported = 0;
|
||||
let skipped = 0;
|
||||
|
||||
for (const dingUserId of dingUserIds) {
|
||||
let mapping = await this.studentDingMappingRepo.findOne({ where: { dingUserId } });
|
||||
let studentId: number;
|
||||
|
||||
if (mapping) {
|
||||
studentId = mapping.studentId;
|
||||
} else {
|
||||
const student = this.studentRepo.create({
|
||||
name: `dd_${dingUserId}`,
|
||||
status: 'active',
|
||||
departmentId: classEntity.departmentId ?? undefined,
|
||||
});
|
||||
const saved = await this.studentRepo.save(student);
|
||||
studentId = saved.id;
|
||||
mapping = this.studentDingMappingRepo.create({ dingUserId, studentId });
|
||||
await this.studentDingMappingRepo.save(mapping);
|
||||
}
|
||||
|
||||
const existing = await this.classStudentRepo.findOne({
|
||||
where: { classId, studentId },
|
||||
});
|
||||
if (existing) { skipped++; continue; }
|
||||
|
||||
await this.classStudentRepo.save(
|
||||
this.classStudentRepo.create({
|
||||
classId, studentId, status: 'active',
|
||||
joinDate: new Date().toISOString().slice(0, 10),
|
||||
}),
|
||||
);
|
||||
imported++;
|
||||
}
|
||||
|
||||
return { imported, skipped };
|
||||
}
|
||||
async update(id: number, dto: UpdateClassDto) {
|
||||
const cls = await this.classRepo.findOne({ where: { id } });
|
||||
if (!cls) throw new NotFoundException('班级不存在');
|
||||
|
||||
Reference in New Issue
Block a user