Browse Source

增加查询班主任信息

liuyu 5 years ago
parent
commit
744b219660
3 changed files with 68 additions and 27 deletions
  1. 4 4
      app/controller/teaplan.js
  2. 1 1
      app/router.js
  3. 63 22
      app/service/teaplan.js

+ 4 - 4
app/controller/teaplan.js

@@ -12,10 +12,10 @@ class TeaplanController extends Controller {
     this.service = this.ctx.service.teaplan;
   }
 
-  // async findteacher() {
-  //   const data = await this.service.findteacher(this.ctx.query);
-  //   this.ctx.ok({ data });
-  // }
+  async findteacher() {
+    const data = await this.service.findteacher(this.ctx.query);
+    this.ctx.ok({ data });
+  }
 
   async divide() {
     const data = await this.service.divide(this.ctx.query);

+ 1 - 1
app/router.js

@@ -85,7 +85,7 @@ module.exports = app => {
 
   // 班主任全年计划表设置路由
   router.get('teaplan', '/api/train/teaplan/divide', controller.teaplan.divide);
-  // router.get('teaplan', '/api/train/teaplan/findteacher', controller.teaplan.findteacher);
+  router.get('teaplan', '/api/train/teaplan/findteacher', controller.teaplan.findteacher);
   router.resources('teaplan', '/api/train/teaplan', controller.teaplan); // index、create、show、destroy
   router.post('teaplan', '/api/train/teaplan/update/:id', controller.teaplan.update);
 

+ 63 - 22
app/service/teaplan.js

@@ -18,28 +18,69 @@ class TeaplanService extends CrudService {
     this.dmodel = this.ctx.model.Department;
   }
 
-  // async findteacher({ batchid }) {
-  //   // 查询所有班主任信息
-  //   const headteachers = await this.hmodel.find();
-  //   const newheadteachers = [];
-  //   // 遍历班主任信息
-  //   for (const headteacher of headteachers) {
-  //     // 查询某班主任对应的班主任全年计划表
-  //     const teaplan = await this.model.findOne({ headteacherid: headteacher.id });
-  //     if (teaplan) {
-  //       const nobatchids = teaplan.nobatchid;
-  //       // 如果有对应的全年计划表并且该计划表中的不能上课的批次包含指定批次,则添加status='0'的标记
-  //       if (nobatchids.includes(batchid)) {
-  //         newheadteachers.push({ ...JSON.parse(JSON.stringify(headteacher)), status: '0' });
-  //       } else {
-  //         newheadteachers.push(headteacher);
-  //       }
-  //     } else {
-  //       newheadteachers.push(headteacher);
-  //     }
-  //   }
-  //   return newheadteachers;
-  // }
+  // 查询所有班主任带是否能带班标记
+  async findteacher({ planid, termid, batchid }) {
+    // 查询所有班主任信息
+    const headteachers = await this.hmodel.find();
+    // 根据批次id取得当前批次具体信息
+    const trainplan = await this.tmodel.findById(planid);
+    if (!trainplan) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
+    }
+    const term = await trainplan.termnum.id(termid);
+    if (!term) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内期信息不存在');
+    }
+    const batch = await term.batchnum.id(batchid);
+    if (!batch) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内批次信息不存在');
+    }
+    const newheadteachers = [];
+    // 遍历班主任信息
+    for (const headteacher of headteachers) {
+      // 查询某班主任对应的班主任全年计划表
+      const teaplan = await this.model.findOne({ headteacherid: headteacher.id });
+      if (teaplan) {
+        // 取得所有不能排班的日期列表
+        const nodates = teaplan.nodate;
+        const iswork = await this.teacheriswork(nodates, batch.startdate, batch.enddate);
+        if (iswork) {
+          newheadteachers.push(headteacher);
+        } else {
+          newheadteachers.push({ ...JSON.parse(JSON.stringify(headteacher)), disabled: true });
+        }
+      } else {
+        newheadteachers.push(headteacher);
+      }
+    }
+    return newheadteachers;
+  }
+
+  // 判断当前日期是否在两个日期之间
+  async betweendate(curdate, startdate, enddate) {
+    // 比较开始日期,如果小于0 为不在此区间
+    // 比较结束日期 如果大于0 为不在此区间
+    const sres = moment(curdate).diff(moment(startdate), 'days');
+    const eres = moment(curdate).diff(moment(enddate), 'days');
+    let result = true;
+    if (sres < 0 || eres > 0) {
+      result = false;
+    }
+    return result;
+  }
+
+  // 判断班主任不能带班日期是否在批次日期里
+  async teacheriswork(nodates, startdate, enddate) {
+    let result = true;
+    for (const nodate of nodates) {
+      const res = this.betweendate(nodate, startdate, enddate);
+      if (res) {
+        result = false;
+        break;
+      }
+    }
+    return result;
+  }
 
   async divide({ trainplanid }) {
     const data = [];