liuyu 4 lat temu
rodzic
commit
a18c2f9b68
1 zmienionych plików z 85 dodań i 0 usunięć
  1. 85 0
      app/service/teacher.js

+ 85 - 0
app/service/teacher.js

@@ -3,6 +3,7 @@
 
 const assert = require('assert');
 const _ = require('lodash');
+const XLSX = require('xlsx');
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const stringRandom = require('string-random');
@@ -63,6 +64,90 @@ class TeacherService extends CrudService {
     }
   }
 
+  // 教室分数上传
+  async teaimport(data) {
+    const { filepath } = data;
+    assert(filepath, 'filepath不能为空');
+    // 取得excle中数据
+    const _filepath = this.ctx.app.config.baseUrl + filepath;
+    console.log(_filepath);
+    const teadatas = await this.getImportXLSXData(
+      _filepath
+    );
+    // 将得到的数据校验
+    const datacheck = await this.datacheck(teadatas);
+    if (datacheck.errorcode === '1') {
+      return datacheck;
+    }
+    // 将数据存入数据库中
+    for (const tea of teadatas) {
+      const res = await this.model.findOne({ idnumber: tea.idnumber, name: tea.name });
+      res.xsscore = tea.xsscore;
+      await res.save();
+    }
+    return datacheck;
+  }
+
+  // 获取导入的XLSX文件中的数据
+  async getImportXLSXData(filepath) {
+    console.log(filepath);
+    const file = await this.ctx.curl(filepath);
+    const workbook = XLSX.read(file.data);
+    // 读取内容
+    let exceldata = [];
+    const sheetNames = workbook.SheetNames; // 获取表名
+    const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
+    // 遍历26个字母
+    const theadRule = [];
+    const range = XLSX.utils.decode_range(sheet['!ref']);
+    const col_start = range.s.c;
+    const col_end = range.e.c;
+    for (let i = col_start; i <= col_end; i++) {
+      const addr = XLSX.utils.encode_col(i) + XLSX.utils.encode_row(0);
+      theadRule.push(sheet[addr].v);
+    }
+    const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
+    if (!params) return [];
+    const length = params.length;
+    const _datas = [];
+    let data = {};
+    for (let i = 0; i < length; i++) {
+      data = params[i];
+      _datas.push({
+        idnumber: data[theadRule[1]],
+        name: data[theadRule[2]],
+        xsscore: data[theadRule[3]],
+      });
+    }
+    exceldata = [ ...exceldata, ..._datas ];
+    return exceldata;
+  }
+
+  // 获取导入的XLSX文件中的数据
+  async datacheck(studatas) {
+    let errorcode = '0';
+    const errormsg = [];
+    for (const data of studatas) {
+      // 判断是否为空
+      if (!data.idnumber) {
+        errorcode = '1';
+        data.msg = data.msg + '身份证号不允许为空,';
+      }
+      if (!data.name) {
+        errorcode = '1';
+        data.msg = data.msg + '姓名不允许为空,';
+      }
+      if (!data.xsscore) {
+        errorcode = '1';
+        data.msg = data.msg + '评分不允许为空,';
+      }
+      if (errorcode === '1') {
+        errormsg.push(data);
+      }
+    }
+    return { errorcode, errormsg };
+  }
+
 }
 
 module.exports = TeacherService;