lrf402788946 4 سال پیش
والد
کامیت
cef888dbe3
2فایلهای تغییر یافته به همراه63 افزوده شده و 32 حذف شده
  1. 1 1
      app/model/trainplan.js
  2. 62 31
      app/service/apply.js

+ 1 - 1
app/model/trainplan.js

@@ -8,7 +8,7 @@ const classInfo = new Schema({
   number: { type: String, required: false, maxLength: 200 }, // 人数
   type: { type: String, required: false, maxLength: 200 }, // 类型-0正常,1特殊,2,3创业班
   headteacherid: { type: String, required: false, maxLength: 200 }, // 班主任id
-  lessons: { type: [ Object ], required: false }, // 班级课表
+  lessons: { type: [ Object ], required: false }, // 班级课表 => status:0-未发布; 1-已发布
   hasclass: { type: String, required: false, maxLength: 200 }, // 是否生成班级: 0未生成;1已生成
 });
 

+ 62 - 31
app/service/apply.js

@@ -2,6 +2,7 @@
 
 
 const assert = require('assert');
+const { after } = require('lodash');
 const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
@@ -36,20 +37,25 @@ class ApplyService extends CrudService {
     if (!trainplan) {
       throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在');
     }
+    // trainplan = JSON.parse(JSON.stringify(trainplan));
     // 查找所有教师列表
-    const teacherList = await this.tmodel.find({ xsscore: { $exists: true } });
+    let teacherList = await this.tmodel.find({ xsscore: { $exists: true } });
+    teacherList = JSON.parse(JSON.stringify(teacherList));
     // 查找所有教师上报列表
-    const teaplanList = await this.model.find();
+    let teaplanList = await this.model.find();
+    teaplanList = JSON.parse(JSON.stringify(teaplanList));
     // 课程
-    const subjectList = await this.submodel.find();
+    let subjectList = await this.submodel.find();
+    subjectList = JSON.parse(JSON.stringify(subjectList));
     const termList = _.cloneDeep(trainplan);
-    const { termnum } = termList;
+    let { termnum } = termList;
     if (!termnum) return;
+    termnum = JSON.parse(JSON.stringify(termnum));
     // 整理出课表
     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(
@@ -66,31 +72,39 @@ class ApplyService extends CrudService {
     const afterList = [];
     // 排课
     for (const l of arr) {
-      const { termid, subid, day: date, teaid } = l;
-      if (teaid) {
+      const { termid, subid, day: date, teaid, status, batchid } = l;
+      if (status && `${status}` === '1') {
         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)
+      if (subject.need_teacher !== '0') {
+        afterList.push(l);
+        continue;
+      }
 
+      // 申请该天,该科目的教师,并查出教师的名字,分数;并按分数排序
+      let applyList = teaplanList.filter(
+        f => f.date === date && f.subid === subid
       );
+      // console.log(applyList.length);
       applyList = applyList.map(i => {
-        const r = teacherList.find(f =>
-          ObjectId(i.teacherid).equals(f._id)
-        );
+        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 i;
+        return obj;
       });
+      // 过滤出没有分数的,不排
       applyList = applyList.filter(f => f.score);
+      // 按成绩排序
       applyList = _.orderBy(applyList, [ 'score' ], [ 'desc' ]);
+      // 本期超过2次的教师列表,如果没有人就用这里分最高的排
+      const outTwoTimesList = [];
       // 依次循环申请的教师列表,往这个课程安排中放教师
       for (const atea of applyList) {
         // 先查询,该教师,是否在今天有安排
@@ -98,15 +112,30 @@ class ApplyService extends CrudService {
           f => f.teaid === atea.teacherid && f.day === atea.date
         );
         if (tr) continue;
-        else {
+        // 查看这期内,每个申请上课的教师时候超过2天(2条记录),如果超过,则不排,但是如果最后没有人了,就得硬排了
+        const r = afterList.filter(f => f.termid === termid && f.teaid === atea.teacherid);
+        if (r.length >= 2) {
+          outTwoTimesList.push(atea);
+          continue;
+        } else {
           l.teaid = atea.teacherid;
           l.teaname = atea.teaname;
           break;
         }
       }
+      // 检查,该天,该科的课是否有教师
+      const has_teaid = _.has(l, 'teaid');
+      if (!has_teaid) {
+        // 如果没有教师,就需要在outTowTimesList列表中找分最高的教师
+        const head = _.head(_.orderBy(outTwoTimesList, [ 'score' ], [ 'desc' ]));
+        if (head) {
+          l.teaid = head.teacherid;
+          l.teaname = head.teaname;
+        }
+      }
       afterList.push(l);
     }
-    // 将afterList还原回正常的termnum
+    // 将afterList还原回正常的termnum;
     let newTermnum = [];
     for (const l of afterList) {
       const { termid, batchid, classid, ...info } = l;
@@ -120,12 +149,12 @@ class ApplyService extends CrudService {
       ]);
       newTermnum = termnum.map(t => {
         // 找到期
-        if (ObjectId(termid).equals(t._id)) {
+        if (termid === t._id) {
           t.batchnum = t.batchnum.map(b => {
-            if (ObjectId(batchid).equals(b._id)) {
+            if (batchid === b._id) {
               // 找到批次
               b.class = b.class.map(c => {
-                if (ObjectId(classid).equals(c._id)) {
+                if (classid === c._id) {
                   if (c.lessons) {
                     // 说明有课程安排,找有没有重复的,没有就推进去,有就更改,subid查
                     const r = c.lessons.find(f => f.subid === updata.subid);
@@ -154,23 +183,25 @@ class ApplyService extends CrudService {
     // 保存至计划
     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);
-      });
+      let { lessons } = cla;
+      if (!lessons) lessons = lessonTemplate;
+      for (const i of lessons) {
+        let nobj = {};
+        nobj.term = term;
+        nobj.termid = termid;
+        nobj.batchid = batchid;
+        const obj = _.omit(cla, [ 'lessons' ]);
+        nobj.classid = _.clone(cla._id);
+        nobj = _.assign(nobj, obj);
+        nobj = _.assign(nobj, i);
+        arr.push(nobj);
+      }
     }
     return arr;
   }