reloaded 5 éve
szülő
commit
6cf55a9d0c
2 módosított fájl, 29 hozzáadás és 15 törlés
  1. 0 1
      app/controller/student.js
  2. 29 14
      app/service/class.js

+ 0 - 1
app/controller/student.js

@@ -16,7 +16,6 @@ class StudentController extends Controller {
   // 查询
   async seek() {
     const res = await this.service.seek(this.ctx.query);
-    console.log(res);
     this.ctx.ok({ ...res });
   }
 

+ 29 - 14
app/service/class.js

@@ -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();
-    }
+
   }
 }