lrf402788946 4 年 前
コミット
1ab7c9d43d
1 ファイル変更60 行追加51 行削除
  1. 60 51
      app/service/student.js

+ 60 - 51
app/service/student.js

@@ -487,19 +487,43 @@ class StudentService extends CrudService {
       }
     });
     model = _.compact(model);
-    // 请求数据
-    // 修改条件,termid变成数组了,需要一个一个查出来
-    const { planid, termid, batchid, classid } = data;
-    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.query(queryObject);
-      studentList.push(...stuList);
+    const { planid, termid: tids, batchid, classid } = data;
+    if (tids && !_.isArray(tids)) throw new BusinessError(ErrorCode.DATA_INVALID, '期数据不正确');
+    const condition = {};
+    if (planid) condition.planid = planid;
+    if (batchid)condition.batchid = batchid;
+    if (classid)condition.classid = classid;
+    const res = [];
+    if (tids.length === 1) {
+      condition.termid = _.head(tids);
+      const r = await this.dataToExcel(condition, model);
+      if (r) res.push(r);
+    }
+    if (res.length === 1) return _.head(res);
+    else if (res.length > 1) {
+      let fn = '学生名单';
+      let tStr = '';
+      for (let i = 0; i < tids.length; i++) {
+        const tid = tids[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}`;
+      return await this.ctx.service.util.toZip(res, fn);
     }
+
+  }
+
+  // 准备数据
+  async dataToExcel(condition, model) {
+    const { planid, termid, batchid, classid } = condition;
+    // 请求数据
+    let { data: studentList } = await this.query(condition);
     studentList = JSON.parse(JSON.stringify(studentList));
     let ids = studentList.map(i => i.classid);
     ids = _.uniq(ids);
@@ -517,23 +541,6 @@ class StudentService extends CrudService {
     }
     let fn = '学生名单';
     // 因为是递进下来, batchid和classid并列,并非递进
-    const trainPlanInfo = async (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;
-    };
     if (classid) {
       // 文件名称: 期, 班
       // 班级名上面有直接拽来
@@ -542,7 +549,7 @@ class StudentService extends CrudService {
         const { name, termid } = cla;
         if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
         if (termid) {
-          const obj = await trainPlanInfo(termid);
+          const obj = await this.toGetFn(termid);
           if (obj) {
             const { term } = obj;
             fn = `第${term}期${fn}`;
@@ -551,8 +558,7 @@ class StudentService extends CrudService {
       }
     } else if (batchid) {
       // 文件名称,期,批
-      const tid = _.head(termid);
-      const obj = await trainPlanInfo(tid, batchid);
+      const obj = await this.toGetFn(termid, batchid);
       if (obj) {
         const { term, batch } = obj;
         if (batch) fn = `第${batch}批${fn}`;
@@ -560,25 +566,10 @@ class StudentService extends CrudService {
       }
     } else if (termid) {
       // 文件名称: 期
-      if (termid.length === 1) {
-        const obj = await trainPlanInfo(_.head(termid));
-        if (obj) {
-          const { term } = obj;
-          if (term) fn = `第${term}期${fn}`;
-        }
-      } else {
-        let tStr = '';
-        for (let i = 0; i < termid.length; i++) {
-          const tid = termid[i];
-          const obj = await trainPlanInfo(tid);
-          if (obj) {
-            const { term } = obj;
-            if (term) {
-              if (i === 0) { tStr += `${term}期`; } else { tStr += `,${term}期`; }
-            }
-          }
-        }
-        fn = `${tStr}${fn}`;
+      const obj = await this.toGetFn(termid);
+      if (obj) {
+        const { term } = obj;
+        if (term) fn = `第${term}期${fn}`;
       }
     } else if (planid) {
       // 文件名称:该计划标题
@@ -591,6 +582,24 @@ class StudentService extends CrudService {
     return await this.ctx.service.util.toExcel(studentList, model, fn);
   }
 
+  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;
+  }
+
   // 确认学生打印证书
   async printCert({ ids }) {
     const res = await this.model.updateMany({ _id: { $in: ids.map(i => ObjectId(i)) } }, { cert: '1' });