lrf402788946 4 年之前
父节点
当前提交
9e35739e13
共有 1 个文件被更改,包括 148 次插入1 次删除
  1. 148 1
      app/service/apply.js

+ 148 - 1
app/service/apply.js

@@ -12,12 +12,16 @@ class ApplyService extends CrudService {
     super(ctx, 'apply');
     this.model = this.ctx.model.Apply;
     this.tmodel = this.ctx.model.Teacher;
+    this.submodel = this.ctx.model.Subject;
+    this.trainmodel = this.ctx.model.Trainplan;
   }
 
   // 查询
   async queryteacher(query) {
     const { termid, subid, date } = query;
-    const data = await this.model.find({ termid, subid, date }).sort({ msscore: -1 });
+    const data = await this.model
+      .find({ termid, subid, date })
+      .sort({ msscore: -1 });
     const teachers = [];
     for (const _data of data) {
       const teacherid = _data.teacherid;
@@ -27,6 +31,149 @@ class ApplyService extends CrudService {
     return teachers;
   }
 
+  async arrangeteacher({ planid }) {
+    const trainplan = await this.trainmodel.findById(planid);
+    if (!trainplan) {
+      throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在');
+    }
+    // 查找所有教师列表
+    const teacherList = await this.tmodel.find({ xsscore: { $exists: true } });
+    // 查找所有教师上报列表
+    const teaplanList = await this.model.find();
+    // 课程
+    const subjectList = await this.submodel.find();
+    const termList = _.cloneDeep(trainplan);
+    const { termnum } = termList;
+    if (!termnum) return;
+    // 整理出课表
+    const arr = [];
+    for (const t of termnum) {
+      const { batchnum, term, _id: termid } = t;
+      // 班级和课程一一匹配显示在列表上
+      for (const b of batchnum) {
+        const { class: classes, lessons, startdate, enddate, _id: batchid } = b;
+        const claslesList = this.setList(
+          term,
+          termid,
+          batchid,
+          classes,
+          lessons
+        );
+        arr.push(...claslesList);
+      }
+    }
+    // 安排后的课表
+    const afterList = [];
+    // 排课
+    for (const l of arr) {
+      const { termid, subid, day: date, teaid } = l;
+      if (teaid) {
+        afterList.push(l);
+        continue;
+      }
+      const subject = subjectList.find(f => ObjectId(subid).equals(f._id));
+      if (subject.need_teacher !== '0') continue;
+      // 申请该天,该科目的教师,并查出教师的名字,分数;并按分数排序   &&
+      let applyList = teaplanList.filter(
+        f => f.date === date && f.subid === subid && ObjectId(f.termid).equals(termid)
+
+      );
+      applyList = applyList.map(i => {
+        const r = teacherList.find(f =>
+          ObjectId(i.teacherid).equals(f._id)
+        );
+        if (r) {
+          const { name: teaname, xsscore: score } = r;
+          i.teaname = teaname;
+          i.score = score * 1;
+        }
+        return i;
+      });
+      applyList = applyList.filter(f => f.score);
+      applyList = _.orderBy(applyList, [ 'score' ], [ 'desc' ]);
+      // 依次循环申请的教师列表,往这个课程安排中放教师
+      for (const atea of applyList) {
+        // 先查询,该教师,是否在今天有安排
+        const tr = afterList.find(
+          f => f.teaid === atea.teacherid && f.day === atea.date
+        );
+        if (tr) continue;
+        else {
+          l.teaid = atea.teacherid;
+          l.teaname = atea.teaname;
+          break;
+        }
+      }
+      afterList.push(l);
+    }
+    // 将afterList还原回正常的termnum
+    let newTermnum = [];
+    for (const l of afterList) {
+      const { termid, batchid, classid, ...info } = l;
+      const updata = _.pick(info, [
+        'day',
+        'subid',
+        'subname',
+        'teaid',
+        'teaname',
+        'time',
+      ]);
+      newTermnum = termnum.map(t => {
+        // 找到期
+        if (ObjectId(termid).equals(t._id)) {
+          t.batchnum = t.batchnum.map(b => {
+            if (ObjectId(batchid).equals(b._id)) {
+              // 找到批次
+              b.class = b.class.map(c => {
+                if (ObjectId(classid).equals(c._id)) {
+                  if (c.lessons) {
+                    // 说明有课程安排,找有没有重复的,没有就推进去,有就更改,subid查
+                    const r = c.lessons.find(f => f.subid === updata.subid);
+                    if (r) {
+                      const rindex = c.lessons.findIndex(
+                        f => f.subid === updata.subid
+                      );
+                      c.lessons[rindex] = updata;
+                    } else {
+                      c.lessons.push(updata);
+                    }
+                  } else {
+                    // 说明没有课程安排,放进去一条保存
+                    c.lessons = [ updata ];
+                  }
+                }
+                return c;
+              });
+            }
+            return b;
+          });
+        }
+        return t;
+      });
+    }
+    // 保存至计划
+    trainplan.termnum = newTermnum;
+    await trainplan.save();
+    // return trainplan;
+  }
+
+  setList(term, termid, batchid, classes, lessonTemplate) {
+    const arr = [];
+    // 班级和课程匹配
+    for (const cla of classes) {
+      cla.term = term;
+      cla.termid = termid;
+      cla.batchid = batchid;
+      const { lessons } = cla;
+      if (!lessons) cla.lessons = lessonTemplate;
+      cla.lessons.map(i => {
+        let obj = _.omit(cla, [ 'lessons' ]);
+        obj = { ...obj, ...i, classid: _.clone(cla._id) };
+        arr.push(obj);
+      });
+    }
+    return arr;
+  }
 }
 
 module.exports = ApplyService;