|
@@ -23,23 +23,38 @@ class ClassService extends CrudService {
|
|
|
const classdata = { name, batchid, termid, number, type };
|
|
|
// 根据班级名称查询班级是否已存在
|
|
|
const newclass = await this.model.findOne({ name });
|
|
|
- // 如果已经存在抛出异常
|
|
|
+ // 如果已经存在
|
|
|
if (newclass) {
|
|
|
throw new BusinessError(ErrorCode.DATA_EXIST, '班级名称已存在');
|
|
|
+ // const classid = newclass._id;
|
|
|
+ // const oldstudents = this.stumodel.find({ classid });
|
|
|
+ // for (const oldstudent of oldstudents) {
|
|
|
+ // oldstudent.classid = null;
|
|
|
+ // }
|
|
|
+ // // 遍历学生id
|
|
|
+ // for (const studentid of data.students) {
|
|
|
+ // // 根据学生id找到学生数据修改其中的class字段
|
|
|
+ // const student = await this.stumodel.findById(studentid);
|
|
|
+ // student.batchid = batchid;
|
|
|
+ // student.classid = classid;
|
|
|
+ // await student.save();
|
|
|
+ // }
|
|
|
+ } else {
|
|
|
+ // 如果班级不存在则创建班级
|
|
|
+ const newdata = await this.model.create(classdata);
|
|
|
+ // 查询班级id
|
|
|
+ const classid = newdata._id;
|
|
|
+ // 遍历学生id
|
|
|
+ for (const studentid of data.students) {
|
|
|
+ // 根据学生id找到学生数据修改其中的class字段
|
|
|
+ const student = await this.stumodel.findById(studentid);
|
|
|
+ student.classid = classid;
|
|
|
+ student.batchid = batchid;
|
|
|
+ student.termid = termid;
|
|
|
+ await student.save();
|
|
|
+ }
|
|
|
}
|
|
|
- // 如果班级不存在则创建班级
|
|
|
- const newdata = await this.model.create(classdata);
|
|
|
- // 查询班级id
|
|
|
- const classid = newdata._id;
|
|
|
- // 遍历学生id
|
|
|
- for (const studentid of data.students) {
|
|
|
- // 根据学生id找到学生数据修改其中的class字段
|
|
|
- const student = await this.stumodel.findById(studentid);
|
|
|
- student.classid = classid;
|
|
|
- student.batchid = batchid;
|
|
|
- student.termid = termid;
|
|
|
- await student.save();
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|