lrf402788946 4 anni fa
parent
commit
905f1c9b38
1 ha cambiato i file con 69 aggiunte e 40 eliminazioni
  1. 69 40
      app/service/task.js

+ 69 - 40
app/service/task.js

@@ -16,7 +16,18 @@ class TaskService extends CrudService {
   async export({ range, subid }) {
     console.log(range, subid);
     // 找到学生
-    let { data: studentList } = await this.ctx.service.student.query(range);
+    // 修改条件,termid变成数组了,需要一个一个查出来
+    const { planid, termid, batchid, classid } = range;
+    const queryObject = {};
+    if (planid) queryObject.planid = planid;
+    if (batchid) queryObject.batchid = batchid;
+    if (classid) queryObject.classid = classid;
+    let studentList = [];
+    for (const t of termid) {
+      queryObject.termid = t;
+      const { data: stuList } = await this.ctx.service.student.query(queryObject);
+      studentList.push(...stuList);
+    }
     if (studentList.length <= 0) {
       throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息');
     }
@@ -68,7 +79,7 @@ class TaskService extends CrudService {
     } else if (batchid) {
       lessonList = await this.ctx.model.Lesson.find({ batchid });
     } else if (termid) {
-      lessonList = await this.ctx.model.Lesson.find({ termid });
+      lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termid } });
     } else if (planid) {
       const trainPlan = await this.ctx.model.Trainplan.findById(planid);
       if (!trainPlan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息'); }
@@ -108,23 +119,7 @@ class TaskService extends CrudService {
   async getFnDir(range) {
     const { planid, termid, batchid, classid } = range;
     let fn = '作业';
-    const prefix = {};
-    const getData = (termid, batchid, termnum) => {
-      let res = '';
-      if (termid) {
-        const termInfo = termnum.id(termid);
-        if (!termInfo) return res;
-        const { term, batchnum } = termInfo;
-        if (term) res = `第${term}期${res}`;
-        if (batchid && batchnum) {
-          const batchInfo = batchnum.id(batchid);
-          if (!batchInfo) return res;
-          const { batch } = batchInfo;
-          res = `${res}第${batch}批`;
-        }
-      }
-      return res;
-    };
+    const prefix = { class: true };
     // 只有班级
     if (classid) {
       const cla = await this.ctx.service.class.fetch({ id: classid });
@@ -133,35 +128,69 @@ class TaskService extends CrudService {
         if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
         if (term) fn = `第${term}期${fn}`;
       }
-    } else {
-      const condition = {};
-      if (planid) condition._id = ObjectId(planid);
-      if (termid) condition['termnum._id'] = ObjectId(termid);
-      if (batchid) condition['termnum.batchnum._id'] = ObjectId(batchid);
-      const trainPlan = await this.ctx.model.Trainplan.findOne(condition);
-      const { termnum, title } = trainPlan;
-      if (!termnum) return;
-      if (termid || batchid) {
-        const r = getData(termid, batchid, termnum);
-        fn = `${r}${fn}`;
-        prefix.class = true;
-        if (batchid) {
-          prefix.term = true;
-          prefix.batch = true;
-        } else {
-          prefix.term = true;
+    } else if (batchid) {
+      const tid = _.head(termid);
+      const obj = await this.toGetFn(tid, batchid);
+      if (obj) {
+        const { term, batch } = obj;
+        if (batch) fn = `第${batch}批${fn}`;
+        if (term) fn = `第${term}期${fn}`;
+      }
+      prefix.term = true;
+      prefix.batch = true;
+    } else if (termid) {
+      if (termid.length === 1) {
+        const obj = await this.toGetFn(_.head(termid));
+        if (obj) {
+          const { term } = obj;
+          if (term) fn = `第${term}期${fn}`;
         }
       } else {
-        fn = `${title}${fn}`;
-        prefix.term = true;
-        prefix.batch = true;
-        prefix.class = true;
+        let tStr = '';
+        for (let i = 0; i < termid.length; i++) {
+          const tid = termid[i];
+          const obj = await this.toGetFn(tid);
+          if (obj) {
+            const { term } = obj;
+            if (term) {
+              if (i === 0) { tStr += `${term}期`; } else { tStr += `,${term}期`; }
+            }
+          }
+        }
+        fn = `${tStr}${fn}`;
+      }
+      prefix.term = true;
+    } else {
+      const trainPlan = await this.ctx.model.Trainplan.findById(planid);
+      if (trainPlan) {
+        const { title } = trainPlan;
+        if (title) fn = `${title}${fn}`;
       }
+      prefix.term = true;
+      prefix.batch = true;
     }
 
     return { fn, prefix };
+  }
 
+  async toGetFn(termid, batchid) {
+    const trainPlan = await this.ctx.model.Trainplan.findOne({ 'termnum._id': ObjectId(termid) });
+    if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息');
+    const { termnum } = trainPlan;
+    if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划的期信息');
+    const obj = {};
+    if (termid) {
+      const term = termnum.id(termid);
+      if (term) obj.term = term.term;
+      if (batchid) {
+        const { batchnum } = term;
+        const batch = batchnum.id(batchid);
+        if (batch) obj.batch = batch.batch;
+      }
+    }
+    return obj;
   }
+
 }
 
 module.exports = TaskService;