lrf пре 1 година
родитељ
комит
c95b620b00
1 измењених фајлова са 122 додато и 102 уклоњено
  1. 122 102
      app/service/teaplan.js

+ 122 - 102
app/service/teaplan.js

@@ -31,17 +31,11 @@ class TeaplanService extends CrudService {
     }
     const term = await trainplan.termnum.id(termid);
     if (!term) {
-      throw new BusinessError(
-        ErrorCode.DATA_NOT_EXIST,
-        '全年计划内期信息不存在'
-      );
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内期信息不存在');
     }
     const batch = await term.batchnum.id(batchid);
     if (!batch) {
-      throw new BusinessError(
-        ErrorCode.DATA_NOT_EXIST,
-        '全年计划内批次信息不存在'
-      );
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内批次信息不存在');
     }
     const newheadteachers = [];
     // 遍历班主任信息
@@ -53,11 +47,7 @@ class TeaplanService extends CrudService {
       if (teaplan) {
         // 取得所有不能排班的日期列表
         const nodates = teaplan.nodate;
-        const iswork = await this.teacheriswork(
-          nodates,
-          batch.startdate,
-          batch.enddate
-        );
+        const iswork = await this.teacheriswork(nodates, batch.startdate, batch.enddate);
         if (iswork) {
           newheadteachers.push(headteacher);
         } else {
@@ -100,91 +90,134 @@ class TeaplanService extends CrudService {
   }
 
   async divide({ trainplanid }) {
-    const data = [];
     // 根据全年计划表id查出对应的全年计划详细信息
     const trainplan = await this.tmodel.findById(trainplanid);
     // 查询本培训计划中班主任全年计划表的全部信息
-    const teaplanList = await this.model.find({ trainplanid });
+    const teaplanList = await this.model.find({ trainplanid }).lean();
     // 查询所有班主任信息
-    const headteacherList = await this.hmodel.find();
-    // for (const teaplan of teaplanList) {
-    //   // 过滤出班主任信息表中id为此班主任全年计划信息班主任id的数据
-    //   const headteacherInfo = _.filter(headteacherList, item => item.id === teaplan.headteacherid);
-    //   // 如果班主任信息列表中班主任上报了班主任全年计划,将其移除
-    //   if (headteacherInfo.length === 0) {
-    //     _.remove(headteacherList, item => item.id === teaplan.headteacherid);
-    //   }
-    // }
+    const headteacherList = await this.hmodel.find().lean();
+
+    // 将班主任排班次数填入数组中
+    let teaListAll_ = [];
+    for (const teac of headteacherList) {
+      // 计算出每个班主任担任过班主任的次数 bug:这里统计的是全库的,应该是改成 当前培训计划范围内
+      const teacount = await this.cmodel.count({ headteacherid: teac.id, planid: trainplanid });
+      // 查出每个班主任上报的日子,也合并在教师数据里
+      const teaplan = teaplanList.find(f => f.headteacherid === (teac.id || teac._id));
+      let nodateList = [];
+      if (teaplan) {
+        const { nodate = [] } = teaplan;
+        nodateList = nodate;
+      }
+      const newdata = { ...JSON.parse(JSON.stringify(teac)), teacount, nodateList };
+      teaListAll_.push(newdata);
+    }
+    // 现在teaListAll_中有班主任信息及他们在当前培训计划中已经排了多少次班了
     // 遍历所有批次信息
     // 将全年计划中的批次信息取出放在一个数组中
+    // 已经安排的时间表,{tea:教师;s:开始时间,e:结束时间,c:班级id}
+    const arrangeList = [];
     for (const term of trainplan.termnum) {
+      // 获取每个部门
+      const deptInfo = this.departmentcount(teaListAll_);
       for (const batch of term.batchnum) {
-        const teaListAll = _.cloneDeep(
-          JSON.parse(JSON.stringify(headteacherList))
-        );
-        for (const teaplan of teaplanList) {
-          if (teaplan.nodate && teaplan.nodate.length > 0) {
-            // 遍历班主任全年计划表中的不能上课的日期
-            for (const nodate of teaplan.nodate) {
-              // 如果不能上课的日期在该批次的开始时间和结束时间中,将该班主任从班主任列表中删除
-              if (
-                moment(nodate).isBetween(
-                  batch.startdate,
-                  batch.enddate,
-                  null,
-                  '[]'
-                )
-              ) {
-                // const headteacher_ = await this.hmodel.findById(teaplan.headteacherid);
-                // if (headteacher_) {
-                //   teaListAll.push(headteacher_);
-                // }
-                _.remove(
-                  teaListAll,
-                  item => item.id === teaplan.headteacherid
-                );
-              }
-            }
-          }
-        }
-        // 将班主任排班次数填入数组中
-        let teaListAll_ = [];
-        for (const teac of teaListAll) {
-          // 计算出每个班主任担任过班主任的次数
-          const teacount = await this.cmodel.count({ headteacherid: teac.id });
-          const newdata = { ...JSON.parse(JSON.stringify(teac)), teacount };
-          teaListAll_.push(newdata);
-        }
+        // 临时教师数组,为了下面处理用而不影响原来的
         // 将班主任按排次数排序
         teaListAll_ = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
+        let tempTeaList = [];
+        // 过滤出不能在该批次上课的班主任
+        for (const tea of teaListAll_) {
+          if (tea.nodateList.length <= 0) {
+            tempTeaList.push(tea);
+            continue;
+          }
+          // 找所有的请假日期在不在当前的批次开始-结束的范围内
+          const is_in = tea.nodateList.find(f => moment(f).isBetween(batch.startdate, batch.enddate, null, '[]'));
+          if (is_in) continue;
+          // 还得过滤下,已经安排过的数据中,有没有重合的
+          const teaArrange = arrangeList.find(f => f.tea === (tea.id || tea._id) && !this.isrepeat(batch.startdate, batch.enddate, f.s, f.e));
+          if (!teaArrange) tempTeaList.push(tea);
+        }
+        const cantUseList = [];
         // 查出该批次下所有的班级
-
         const classList = await batch.class;
         for (const _class of classList) {
-          // 取出所有部门和部门可用人数
-          const departmentList = await this.departmentcount(teaListAll_);
-          let index = 0;
-          for (const _tea of teaListAll_) {
-            // 过滤出当前教师的部门和部门可用人数
-            const deptinfo = _.find(departmentList, function(o) {
-              return o.deparmentid === _tea.department;
-            });
-            if (deptinfo.dcount - index > 1) {
-              _class.headteacherid = _tea.id;
-              _.remove(teaListAll_, item => item.id === _tea.id);
-              _.remove(teaListAll, item => item.id === _tea.id);
-              index = index + 1;
-              break;
-            }
+          // 清除班级已经安排过的班主任
+          _class.headteacherid = null;
+          let selectTea;
+          // 重新排列下
+          tempTeaList = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
+          // 循环,直到找到人为止
+          while (!selectTea) {
+            const tempSelect = this.findTeacher(tempTeaList, deptInfo, cantUseList);
+            // 2. 确定该人排完班之后,本期中,他所在的部门还是否有1个人了
+            // 找选中的班主任的部门
+            const dept = deptInfo.find(f => f.dept === tempSelect.department);
+            if (dept.dcount - 1 >= 1) {
+              // 且再查下这个班主任有没有同时带2个班
+              const is_in = classList.find(f => f.headteacherid === (tempSelect.id || tempSelect._id));
+              if (!is_in) selectTea = tempSelect;
+            } else cantUseList.push(tempSelect.id || tempSelect._id);
+            // 分不了了.没人了
+            if (cantUseList.length === tempTeaList.length) break;
           }
+          // 确定人后,修改 教师带班次数 和 重计算本期部门用人百分比
+          const inListTeaData = teaListAll_.find(f => (f.id || f._id) === (selectTea.id || selectTea._id));
+          // 没找到教师略过
+          if (!inListTeaData) continue;
+          inListTeaData.teacount = inListTeaData.teacount + 1;
+          const inDeptData = deptInfo.find(f => f.dept === selectTea.department);
+          inDeptData.notUse = inDeptData.notUse - 1;
+          const newPer = this.computedPer(inDeptData.notUse, inDeptData.dcount);
+          inDeptData.per = newPer;
+          _class.headteacherid = selectTea.id || selectTea._id;
+          arrangeList.push({ teaName: inListTeaData.name, tea: inListTeaData.id || inListTeaData._id, s: batch.startdate, e: batch.enddate, c: _class.id || _class._id });
         }
       }
     }
+    // 检查最后结果
+    // const t1 = _.groupBy(arrangeList, 'tea');
+    // for (const id in t1) {
+    //   const head = _.head(t1[id]);
+    //   console.log(`${head.teaName}: ${t1[id].length}`);
+    // }
     return await trainplan.save();
   }
 
-  // 判断两个时间段是否有重合部分
-  async isrepeat(startdate1, enddate1, startdate2, enddate2) {
+  computedPer(n1, total) {
+    return _.floor(_.multiply(_.divide(n1, total), 100));
+  }
+  /**
+   * 选择最优教师
+   * @param {Array} tempTeaList 可以安排的教师列表
+   * @param {Array} deptInfo 部门情况列表
+   * @param {Array} cantUseList 分配之后发现不能用的教师
+   */
+  findTeacher(tempTeaList, deptInfo, cantUseList) {
+    // 1. 找教师排班次数最少的.多人并且就查看部门的使用人员的百分比和总人数排名,再多就取第一个
+    tempTeaList = tempTeaList.filter(f => !cantUseList.includes(f.id || f._id));
+    const fir = _.head(tempTeaList);
+    const times = fir.teacount;
+    const sameTimesTea = tempTeaList.filter(f => f.teacount === times);
+    // 满足条件的老师
+    let selectTea;
+    if (sameTimesTea.length > 0) {
+      // 同时有多个人次数都为最少的次数,那就看部门:闲人占部门的百分比和闲人的人数降序排列
+      const deptOrderList = _.orderBy(deptInfo, [ 'per', 'notUse' ], [ 'desc', 'desc' ]);
+      // 根据排序后的部门,从上往下选个人就行
+      for (const deptData of deptOrderList) {
+        selectTea = sameTimesTea.find(f => f.department === deptData.dept);
+        if (selectTea) break;
+      }
+    } else if (sameTimesTea.length === 1) {
+      // 那就是这个老师了.就这个人满足 排班次数最少的老师
+      selectTea = _.head(sameTimesTea);
+    }
+    return selectTea;
+  }
+
+  // 判断两个时间段是否有重合部分: true 重合;false不重合
+  isrepeat(startdate1, enddate1, startdate2, enddate2) {
     let result = true;
     if (moment(enddate1).isSameOrBefore(startdate2)) {
       result = false;
@@ -195,32 +228,19 @@ class TeaplanService extends CrudService {
     return result;
   }
 
-  // 查询传递的班主任列表中每个部门的人数以及每个部门的总人数
-  // teaList 传递的教师列表  dnum 传递的教师列表中各部门的人数  dcount 各部门的总人数
-  async departmentcount(teaList) {
-    let departmentList = [];
-    for (const teaplan of teaList) {
-      departmentList.push({ deparmentid: teaplan.department });
+  // 整理当前批次 每个部门有多少人
+  // dept 部门id  dcount 各部门的总人数 per 未使用人员的百分比; notUse 未使用的人数
+  departmentcount(teaList) {
+    const group = _.groupBy(teaList, 'department');
+    let result = [];
+    for (const dept in group) {
+      const obj = { dept, dcount: group[dept].length, notUse: group[dept].length, per: 100 };
+      result.push(obj);
     }
-    // 将数组去重
-    departmentList = _.uniqWith(departmentList, _.isEqual);
-    for (const department of departmentList) {
-      // 计算出能该批次能上课的班主任中各部门有多少人
-      const dnum = _.filter(
-        teaList,
-        item => item.department === department.deparmentid
-      ).length;
-      department.dnum = dnum;
-      // 计算出各部门有多少人
-      const dcount = await this.hmodel.count({
-        department: department.deparmentid,
-      });
-      department.dcount = dcount;
-    }
-    return departmentList;
+    // 按未使用人员的百分比,总数给部门排序
+    result = _.orderBy(result, [ 'per', 'notUse', 'dcount' ]);
+    return result;
   }
-
-
 }
 
 module.exports = TeaplanService;