lrf402788946 4 år sedan
förälder
incheckning
31320fa520
2 ändrade filer med 111 tillägg och 14 borttagningar
  1. 108 13
      app/service/apply.js
  2. 3 1
      app/service/score.js

+ 108 - 13
app/service/apply.js

@@ -137,7 +137,9 @@ class ApplyService extends CrudService {
           continue;
         } else {
           // 条件3:尽量不让一个教师在一个班出现2次及其以上,但是如果没人,之前又排完了,还是会出现一个教师教1个班2天的情况,所以需要最后收尾检查
-          const alreadyTeach = afterList.find(f => f.classid === classid && f.teaid === atea.teacherid);
+          const alreadyTeach = afterList.find(
+            f => f.classid === classid && f.teaid === atea.teacherid
+          );
           if (alreadyTeach) {
             // 已经教过这个班了,也暂时放到2次以上列表中,将次数记录下来
             const obj = { ...atea, times: r.length };
@@ -174,19 +176,112 @@ class ApplyService extends CrudService {
       }
       afterList.push(l);
     }
+    // 需要按classid分组,然后查每组下,有没有重复教师,如果有重复教师,提取出这个教师的日期,科目,分2种情况:
+    // 1)可以和其他班教师换下位置;2)没法换,只能排下个教师
+    // 查找这天,这科的 课程安排 且 这些课程的教师 没给这个班级上过课 (undefined也算,每排教师)
+    //  =>如果有满足条件的教师, 找到该班, 然后查看,现在需要换的这个教师是否也满足 没给这个班上过课 的条件
+    //    =>满足条件,两个班级的教师对调
+    //    =>不满足条件直至没有教师,需要整理出 applyList,然后将现在这个教师排除掉(放到一个空间),然后找其他满足条件的教师,如果没有,那就只能有他了
+    const checkList = afterList.filter(f => ids.includes(f.termid) && f.type === classtype && f.teaid);
+    const groupObj = _.groupBy(checkList, 'classid');
+    const keys = Object.keys(groupObj);
+    const exchangeList = [];
+    for (const key of keys) {
+      const arr = groupObj[key];
+      const teaids = arr.map(i => _.pick(i, [ 'day', 'subid', 'teaid' ]));
+      for (const l of arr) {
+        const { day, subid, teaid, teaname, termid } = l;
+        // 找下这个教师在这个班级中,是否教了多科
+        let findres = teaids.filter(f => f.teaid === teaid);
+        // 2科以下,跳过不看
+        if (findres.length < 2) continue;
+        // 2科以上,需要找到每一天的申请名单,可最多的那天先换
+        // 先查下同一天,同一科的教师能不能换
+        const toDaySubjectList = afterList.filter(f => f.subid === subid && f.day === day && f.teaid !== teaid);
+        let sameSubChange = false;
+        for (const tdstea of toDaySubjectList) {
+          // 找一下,这个教师能不能换到这个班和当前重复教一个班的教师能不能带这个教师的班
+          // 为什么这么找:因为到这里之前,所有需要安排的课程,已经按照1,2原则排完,是最优解,所以尽可能在最优解之间进行调换,
+          // 如果最优解没有办法调换,那就只能往下找了,对于两个班的情况可能常见,但是对于三个班来说基本就很少了,如果开4个班,基本就不存在这个问题了,
+          // 循环的教师,需要在teaids中找有没有这个教师
+          const first = teaids.find(f => f.teaid === tdstea.teaid);
+          if (first) continue;
+          else {
+            // 如果这个教师可以,需要进行反查,查当前有问题的这个教师,在没在要和他换的教师班级里讲过课
+            const { classid } = tdstea;
+            const tdsLessonClass = groupObj[classid];
+            const sec = tdsLessonClass.find(f => f.teaid === teaid);
+            // 如果这个教师教过要调换教师那个班,就继续找看下个老师
+            if (sec) continue;
+            sameSubChange = tdstea;
+          }
+        }
+        // 判断sameSubChange,如果不为false,则说明当天教师可以替换,接着continue就行
+        if (sameSubChange) {
+          const { teaid: oteaid, teaname: oteaname } = sameSubChange;
+          // 要替换的索引
+          const oindex = afterList.findIndex(f => _.isEqual(f, sameSubChange));
+          // 重复老师的索引
+          const tindex = afterList.findIndex(f => _.isEqual(f, l));
+          // 交换
+          afterList[oindex].teaid = teaid;
+          afterList[oindex].teaname = teaname;
+          afterList[tindex].teaid = oteaid;
+          afterList[tindex].teaname = oteaname;
+          continue;
+        }
+        // 如果到这里了,也就是说同天同科的教师都替换不了,那就是一点血找也没有了
+        for (const les of findres) {
+          // 申请的教师
+          let applyList = teaplanList.filter(
+            f => f.date === les.day && f.subid === les.subid && f.teacherid !== les.teaid
+          );
+          applyList = applyList.map(i => {
+            let obj = { ...JSON.parse(JSON.stringify(i)) };
+            const r = teacherList.find(f => i.teacherid === f._id);
+            if (r) {
+              const { name: teaname, xsscore: score } = r;
+              i.teaname = teaname;
+              i.score = score * 1;
+              obj = { ...obj, teaname, score };
+            }
+            return obj;
+          });
+          // 过滤出没有分数的,不排
+          applyList = applyList.filter(f => f.score);
+          // 过滤出当天有课的教师
+          applyList = applyList.filter(f => !(afterList.find(af => af.day === f.day && af.teaid === f.teaid)));
+          // 过滤出这期有几次
+          applyList = applyList.map(i => {
+            // 找到这个教师在这期有几次
+            const r = afterList.filter(f => f.termid === termid && f.teaid === i.teaid);
+            i.times = r.length;
+            return i;
+          });
+          // 按成绩排序
+          applyList = _.orderBy(applyList, [ 'times', 'score' ], [ 'asc', 'desc' ]);
+          // 整理出教师教这个班的所有科目的其他可选教师(排除已经排完课的教师)
+          les.applyList = applyList;
+          les.applynum = applyList.length;
+        }
+        // 按照申请人数排序,然后看看人最多那天是不是这天,如果是这天,就处理
+        findres = _.orderBy(findres, [ 'applynum' ], [ 'desc' ]);
+        const head = _.head(findres);
+        if (head.day === day) {
+          // 如果当前日期,是申请人数最多的,就处理它
+          const { applyList } = head;
+          if (applyList.length >= 0) {
+            // 将排完序的第一个教师拿出来(一定是符合要求的)
+            const apply = _.head(applyList);
+            const { teaid, teaname } = apply;
+            const tindex = afterList.findIndex(f => _.isEqual(f, l));
+            afterList[tindex].teaid = teaid;
+            afterList[tindex].teaname = teaname;
+          }
+        }
+      }
+    }
     // 将afterList还原回正常的termnum;
-    // const test = afterList.filter(f => ids.includes(f.termid) && f.type === classtype && f.teaid);
-    // const test2 = _.groupBy(test, 'name');
-    // const keys = Object.keys(test2);
-    // console.log(test);
-    // for (const key of keys) {
-    //   console.group(`${key}班`);
-    //   for (const t of test2[key]) {
-    //     console.log(`${t.day}-${t.subname}-${t.teaname}`);
-    //   }
-    //   console.groupEnd();
-    // }
-
     const newTermnum = this.returnTermnum(afterList, termnum);
     // 保存至计划
     trainplan.termnum = newTermnum;

+ 3 - 1
app/service/score.js

@@ -45,7 +45,7 @@ class ScoreService extends CrudService {
       i.score = i.score * 1;
       return i;
     });
-    teaScoreList = _.uniqBy(teaScoreList, 'studentid');
+    teaScoreList = _.uniqBy(teaScoreList, 'stuid');
     teaScoreList = _.orderBy(teaScoreList, [ 'score' ], [ 'desc' ]);
     teaScoreList = _.drop(teaScoreList);
     teaScoreList = _.dropRight(teaScoreList);
@@ -57,6 +57,8 @@ class ScoreService extends CrudService {
       score = _.round(_.divide(stuScoreTotal * 1, teaScoreList.length), 2);
     }
     teacher.xsscore = score;
+    console.log(stuScoreTotal, teaScoreList.length);
+    console.log(`${teacher.name}:${teacher.xsscore}`);
     await teacher.save();
 
   }