lrf402788946 4 år sedan
förälder
incheckning
82d8f4604a
5 ändrade filer med 129 tillägg och 3 borttagningar
  1. 4 0
      app/controller/.student.js
  2. 5 0
      app/router.js
  3. 90 3
      app/service/student.js
  4. 29 0
      app/service/util.js
  5. 1 0
      package.json

+ 4 - 0
app/controller/.student.js

@@ -140,4 +140,8 @@ module.exports = {
     parameters:{params: ["!id"]},
     service: "getFineStudent",
   },
+  exportStudent: {
+    requestBody: ['planid','termid','batchid', 'classid'],
+    service:"exportStudent"
+  }
 };

+ 5 - 0
app/router.js

@@ -88,6 +88,11 @@ module.exports = app => {
     controller.questionnaire.show
   );
 
+  router.post(
+    'student',
+    '/api/train/student/export',
+    controller.student.exportStudent
+  );
   // 学生表设置路由
   router.get(
     'sutdent',

+ 90 - 3
app/service/student.js

@@ -6,7 +6,7 @@ const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
-
+const moment = require('moment');
 class StudentService extends CrudService {
   constructor(ctx) {
     super(ctx, 'student');
@@ -394,7 +394,6 @@ class StudentService extends CrudService {
   // 将学生排号
   async arrangeNumber(classid) {
     const studList = await this.model.find({ classid });
-    console.log(`in function:arrangeNumber classid=>${classid}`);
     let number = 1;
     // 查每个学生的编号,如果没有,就给赋上值;有,就给number赋上值,然后继续下一位
     for (const stu of studList) {
@@ -407,7 +406,95 @@ class StudentService extends CrudService {
       }
       number = number * 1 + 1;
     }
-    console.log(`last number => ${number}`);
+    // console.log(`last number => ${number}`);
+  }
+
+  // 导出学生 目前:拓展训练保险用
+  async exportStudent(data) {
+    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 = [];
+    for (const id of ids) {
+      const cla = await this.ctx.service.class.fetch({ id });
+      if (!cla) continue;
+      cla.date = moment(cla.date).add(1, 'd').format('YYYY-MM-DD');
+      classList.push(cla);
+    }
+    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();
+    return await this.ctx.service.util.toExcel(studentList, meta, '学生名单');
+  }
+
+  metaBx() {
+    const header = [
+      {
+        header: '姓名',
+        key: 'name',
+        width: 20,
+      },
+      {
+        header: '拓训日期',
+        key: 'date',
+        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,
+      },
+    ];
+    return header;
   }
 }
 

+ 29 - 0
app/service/util.js

@@ -3,6 +3,8 @@
 
 const assert = require('assert');
 const _ = require('lodash');
+const fs = require('fs');
+const Excel = require('exceljs');
 const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
@@ -84,6 +86,33 @@ class UtilService extends CrudService {
       // await this.ctx.model.Student.findByIdAndUpdate(id, { bedroom: code, bedroomid });
     }
   }
+
+  async toExcel(dataList, meta, fn = '导出结果') {
+    // 导出excel
+    const { app } = this;
+    const nowDate = new Date().getTime();
+    const filename = `${fn}-${nowDate}.xlsx`;
+    // 取出预设存储地址
+    const rootPath = `${app.config.cdn.repos_root_path}`;
+    const rooturl = `${app.config.cdn.repos_root_url_excel}`;
+    const path = `${rootPath}${rooturl}`;
+    if (!path) {
+      throw new BusinessError(ErrorCode.BUSINESS, '服务端没有设置存储路径');
+    }
+    // 如果不存在文件夹,就创建
+    if (!fs.existsSync(path)) {
+      fs.mkdirSync(path);
+    }
+    // 生成文件
+    const filepath = `${path}${filename}`;
+    fs.createWriteStream(filepath);
+    const workbook = new Excel.Workbook();
+    const sheet = workbook.addWorksheet('sheet');
+    sheet.columns = meta;
+    sheet.addRows(dataList);
+    await workbook.xlsx.writeFile(filepath);
+    return `/files/excel/${filename}`;
+  }
 }
 
 module.exports = UtilService;

+ 1 - 0
package.json

@@ -12,6 +12,7 @@
     "egg-redis": "^2.4.0",
     "egg-scripts": "^2.11.0",
     "egg-view-nunjucks": "^2.2.0",
+    "exceljs": "^4.1.1",
     "jsonwebtoken": "^8.5.1",
     "lodash": "^4.17.15",
     "moment": "^2.27.0",