|
@@ -19,49 +19,42 @@ class ClassService extends CrudService {
|
|
|
}
|
|
|
|
|
|
async divide(data) {
|
|
|
- const { classname, number, type, termid, batchid } = data;
|
|
|
- const classdata = { name: classname, batchid, termid, number, type };
|
|
|
- console.log(classdata);
|
|
|
- // 根据班级名称查询班级是否已存在
|
|
|
- const newclass = await this.model.findOne({ name: classname });
|
|
|
- console.log(newclass);
|
|
|
- // 如果已经存在
|
|
|
- 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);
|
|
|
+ const { planid, termid } = data;
|
|
|
+ assert(planid, '计划id为必填项');
|
|
|
+ assert(termid, '期id为必填项');
|
|
|
+ // 根据计划id与期id查询所有批次下的班级
|
|
|
+ const newclass = await this.model.find({ planid, termid });
|
|
|
+ if (!newclass) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
|
|
|
+ }
|
|
|
+ // 根据计划和期查询所有上报的学生 并按照学校排序
|
|
|
+ const newstudent = await this.stumodel.find({ termid }).sort({ schid: 1 });
|
|
|
+ if (!newstudent) {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
|
|
|
+ }
|
|
|
+ let _students = _.map(newstudent, 'id');
|
|
|
+ for (const _class of newclass) {
|
|
|
+ // 取得班级人数
|
|
|
+ const stunum = _class.number;
|
|
|
+ // 取出班级人数下的学生id
|
|
|
+ const beforestu = _.take(_students, stunum);
|
|
|
+ // 将取出的数据调用更新学生的班级信息方法
|
|
|
+ await this.studentup(_class.id, beforestu);
|
|
|
+ // 取出的学生数据从原数据中删除
|
|
|
+ _students = _.difference(_students, beforestu);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据传入的学生列表和班级id更新学生信息
|
|
|
+ async studentup(classid, beforestu) {
|
|
|
+ // 循环学生id
|
|
|
+ for (const stuid of beforestu) {
|
|
|
+ const student = await this.stumodel.findById(stuid);
|
|
|
+ if (student) {
|
|
|
student.classid = classid;
|
|
|
- student.batchid = batchid;
|
|
|
- student.termid = termid;
|
|
|
await student.save();
|
|
|
}
|
|
|
- for (let i = 1; i <= 7; i++) {
|
|
|
- const groupname = i + '组';
|
|
|
- const groupdata = { termid, batchid, classid, name: groupname };
|
|
|
- await this.gmodel.create(groupdata);
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
async notice(data) {
|