lrf402788946 4 年之前
父节点
当前提交
1398595268
共有 7 个文件被更改,包括 103 次插入81 次删除
  1. 1 1
      app/controller/.student.js
  2. 4 0
      app/controller/util.js
  3. 4 4
      app/model/student.js
  4. 1 0
      app/router.js
  5. 1 1
      app/service/experience.js
  6. 88 75
      app/service/student.js
  7. 4 0
      app/service/util.js

+ 1 - 1
app/controller/.student.js

@@ -141,7 +141,7 @@ module.exports = {
     service: "getFineStudent",
   },
   exportStudent: {
-    requestBody: ["planid", "termid", "batchid", "classid", "type"],
+    requestBody: ["planid", "termid", "batchid", "classid", "model"],
     service: "exportStudent",
   },
   arrangeNumber: {

+ 4 - 0
app/controller/util.js

@@ -11,5 +11,9 @@ class UtilController extends Controller {
     const res = await this.service.utilMethod(this.ctx.query, this.ctx.request.body);
     this.ctx.ok({ res });
   }
+  async exportExcel() {
+    const res = await this.service.exportExcel(this.ctx.request.body);
+    this.ctx.ok({ res });
+  }
 }
 module.exports = UtilController;

+ 4 - 4
app/model/student.js

@@ -27,10 +27,10 @@ const StudentSchema = {
   have_grant: { type: String, required: false, maxLength: 200, zh: '是否获得过助学金' }, // 是否获得过助学金,0-否,1-是
   job: { type: String, required: false, maxLength: 200, default: '普通学生', zh: '职务' }, // 职务
   planyearid: { type: String, required: false, maxLength: 200, zh: '大批次id' }, // 大批次id
-  planid: { type: String, required: false, maxLength: 200, zh: '计划id' }, // 计划id
-  termid: { type: String, required: false, maxLength: 200, zh: '期id' }, // 期id
-  batchid: { type: String, required: false, maxLength: 200, zh: '批次id' }, // 批次id
-  classid: { type: String, required: false, maxLength: 200, zh: '班级id' }, // 班级id
+  planid: { type: String, required: false, maxLength: 200 }, // 计划id
+  termid: { type: String, required: false, maxLength: 200, zh: '期' }, // 期id
+  batchid: { type: String, required: false, maxLength: 200, zh: '批次' }, // 批次id
+  classid: { type: String, required: false, maxLength: 200, zh: '班级' }, // 班级id
   bedroomid: { type: String, required: false, maxLength: 200, zh: '寝室id' }, // 寝室id
   bedroom: { type: String, required: false, maxLength: 200, zh: '寝室号' }, // 寝室号
   is_fine: { type: String, required: false, maxLength: 200, default: '0', zh: '是否优秀' }, // 是否优秀,0-否,1-是,2-无资格

+ 1 - 0
app/router.js

@@ -663,6 +663,7 @@ module.exports = app => {
   router.get('/api/train/cerconfirm', controller.cerconfirm.index);
   // 工具方法
   router.post('/api/train/util', controller.util.utilMethod);
+  router.post('/api/train/exportExcel', controller.util.exportExcel);
 
   // 新人才报
   router.get('talented', '/api/train/talented/export', controller.talented.export);

+ 1 - 1
app/service/experience.js

@@ -93,7 +93,7 @@ class ExperienceService extends CrudService {
         return i;
       });
       res = r;
-      fn = `第${term}期${h.classid.name}班培训心得`;
+      fn = `第${term}期${h.classid.name.includes('班') ? h.classid.name : `${h.classid.name}班`}培训心得`;
     } else if (batchid) {
       let r = await this.model.find({ batchid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
       if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得');

+ 88 - 75
app/service/student.js

@@ -476,34 +476,104 @@ class StudentService extends CrudService {
   }
 
   // 导出学生 目前:拓展训练保险用
-  async exportStudent({ type = 'insurance', ...data }) {
-    console.log(type, data);
+  async exportStudent(body) {
+    let { model, ...data } = body;
+    // 整理model
+    model = model.map(i => {
+      const { zh, model } = i;
+      if (zh && model) {
+        const obj = {};
+        obj.header = zh;
+        if (model === 'termid') obj.key = 'termname';
+        else if (model === 'batchid') obj.key = 'batchname';
+        else if (model === 'classid') obj.key = 'classname';
+        else obj.key = model;
+        obj.width = 20;
+        return obj;
+      }
+    });
+    model = _.compact(model);
+    // 请求数据
     let { data: studentList } = await this.query(data);
     studentList = JSON.parse(JSON.stringify(studentList));
     let ids = studentList.map(i => i.classid);
     ids = _.uniq(ids);
-    const classList = [];
-    // 此处可以优化,将班级整理,查出来,循环中find
-    for (const id of ids) {
-      const cla = await this.ctx.service.class.fetch({ id });
+    ids = _.compact(ids);
+    ids = ids.map(i => ObjectId(i));
+    const classList = await this.ctx.model.Class.find({ _id: { $in: ids } });
+    for (const stu of studentList) {
+      const { classid } = stu;
+      if (!classid) continue;
+      const cla = classList.find(f => ObjectId(classid).equals(f._id));
       if (!cla) continue;
-      if (type === 'insurance') { cla.date = moment(cla.startdate).add(1, 'd').format('YYYY-MM-DD'); }
-      classList.push(cla);
+      const { startdate } = cla;
+      if (!startdate) continue;
+      stu.insurance = moment(startdate).add(1, 'd').format('YYYY-MM-DD');
     }
-    studentList = studentList.map(i => {
-      const c = classList.find(f => ObjectId(i.classid).equals(f._id));
-      if (c) i.date = c.date;
-      return i;
-    });
-    const meta = this.metaBx(type);
     let fn = '学生名单';
-    const head = _.head(classList);
-    if (head) {
-      if (type === 'class') { fn = `${head.term}期-${head.batch}批-${head.name}${fn}`; } else fn = `${head.term}期-${head.batch}批${fn}`;
+    // 因为是递进下来, batchid和classid并列,并非递进
+    const { planid, termid, batchid, classid } = data;
+    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) {
+      // 文件名称: 期, 班
+      // 班级名上面有直接拽来
+      const cla = classList.find(f => ObjectId(classid).equals(f._id));
+      if (cla) {
+        const { name, termid } = cla;
+        if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
+        if (termid) {
+          const obj = await trainPlanInfo(termid);
+          if (obj) {
+            const { term } = obj;
+            console.log(obj);
+            fn = `第${term}期${fn}`;
+          }
+        }
+      }
+    } else if (batchid) {
+      // 文件名称,期,批
+      const obj = await trainPlanInfo(termid, batchid);
+      if (obj) {
+        const { term, batch } = obj;
+        if (batch) fn = `第${batch}批${fn}`;
+        if (term) fn = `第${term}期${fn}`;
+      }
+    } else if (termid) {
+      // 文件名称: 期
+      const obj = await trainPlanInfo(termid);
+      if (obj) {
+        const { term } = obj;
+        if (term) fn = `第${term}期${fn}`;
+      }
+    } else if (planid) {
+      // 文件名称:该计划标题
+      const trainPlan = await this.ctx.model.Trainplan.findById(planid);
+      if (trainPlan) {
+        const { title } = trainPlan;
+        if (title) fn = `${title}${fn}`;
+      }
     }
-    return await this.ctx.service.util.toExcel(studentList, meta, fn);
+    return await this.ctx.service.util.toExcel(studentList, model, fn);
   }
 
+
+  // excel导出表头设置
   metaBx(type) {
     const header = [
       {
@@ -511,64 +581,7 @@ class StudentService extends CrudService {
         key: 'name',
         width: 20,
       },
-      {
-        header: '性别',
-        key: 'gender',
-        width: 10,
-      },
-      {
-        header: '民族',
-        key: 'nation',
-        width: 20,
-      },
-      {
-        header: '身份证号',
-        key: 'id_number',
-        width: 20,
-      },
-      {
-        header: '期',
-        key: 'termname',
-        width: 20,
-      },
-      {
-        header: '批次',
-        key: 'batchname',
-        width: 20,
-      },
-      {
-        header: '班级',
-        key: 'classname',
-        width: 20,
-      },
-      {
-        header: '学校',
-        key: 'school_name',
-        width: 20,
-      },
-      {
-        header: '院系',
-        key: 'faculty',
-        width: 20,
-      },
-      {
-        header: '专业',
-        key: 'major',
-        width: 20,
-      },
-      {
-        header: '手机号',
-        key: 'phone',
-        width: 20,
-      },
     ];
-    if (type === 'insurance') {
-      header.splice(1, 0, {
-        header: '拓训日期',
-        key: 'date',
-        width: 20,
-      });
-    }
     return header;
   }
 }

+ 4 - 0
app/service/util.js

@@ -132,6 +132,10 @@ class UtilService extends CrudService {
     // }
   }
 
+  async exportExcel(data) {
+    console.log(data);
+  }
+
   async toExcel(dataList, meta, fn = '导出结果') {
     // 导出excel
     const { app } = this;