|
@@ -0,0 +1,102 @@
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+const assert = require('assert');
|
|
|
|
+const _ = require('lodash');
|
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
|
+const XLSX = require('xlsx');
|
|
|
|
+
|
|
|
|
+class SchtimeService extends CrudService {
|
|
|
|
+ constructor(ctx) {
|
|
|
|
+ super(ctx, 'schoolctrl');
|
|
|
|
+ this.model = this.ctx.model.Student;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async stuimport(data) {
|
|
|
|
+ const { filepath, termid, schid } = data;
|
|
|
|
+ // 取得excle中数据
|
|
|
|
+ const studatas = await this.getImportXLSXData(filepath, termid, schid);
|
|
|
|
+ // 将得到的数据校验
|
|
|
|
+ const datacheck = await this.datacheck(studatas);
|
|
|
|
+ if (datacheck.errorcode === '1') {
|
|
|
|
+ return datacheck;
|
|
|
|
+ }
|
|
|
|
+ // 将数据存入数据库中
|
|
|
|
+ for (const stu of studatas) {
|
|
|
|
+ await this.model.create(stu);
|
|
|
|
+ }
|
|
|
|
+ return datacheck;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取导入的XLSX文件中的数据
|
|
|
|
+ async getImportXLSXData(filepath, termid, schid) {
|
|
|
|
+ const workbook = XLSX.readFile(filepath);
|
|
|
|
+ // 读取内容
|
|
|
|
+ let exceldata = [];
|
|
|
|
+ const sheetNames = workbook.SheetNames; // 获取表名
|
|
|
|
+ const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
|
|
|
|
+ const theadRule = [ sheet.A1.v, sheet.B1.v, sheet.C1.v, sheet.D1.v, sheet.E1.v, sheet.F1.v, sheet.G1.v, sheet.H1.v, sheet.I1.v, sheet.J1.v, sheet.K1.v, sheet.L1.v, sheet.M1.v, sheet.N1.v, sheet.O1.v, sheet.P1.v ];
|
|
|
|
+ const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
|
|
|
|
+ // const theadRule = [ '序号', '姓名', '性别', '民族', '身份证号', '学校名称', '院系', '专业', '入学年份', '毕业年份', '在校曾担任何种职务', '手机号', 'QQ号', '家庭所在地', '家庭是否困难', '是否获得过助学金' ];
|
|
|
|
+ console.log(params);
|
|
|
|
+ if (!params) return [];
|
|
|
|
+ let i = 0;
|
|
|
|
+ const length = params.length;
|
|
|
|
+ const _datas = [];
|
|
|
|
+ let data = {};
|
|
|
|
+ for (i; i < length; i++) {
|
|
|
|
+ data = params[i];
|
|
|
|
+ _datas.push({
|
|
|
|
+ name: data[theadRule[1]],
|
|
|
|
+ gender: data[theadRule[2]],
|
|
|
|
+ nation: data[theadRule[3]],
|
|
|
|
+ id_number: data[theadRule[4]],
|
|
|
|
+ school_name: data[theadRule[5]],
|
|
|
|
+ faculty: data[theadRule[6]],
|
|
|
|
+ major: data[theadRule[7]],
|
|
|
|
+ entry_year: data[theadRule[8]],
|
|
|
|
+ finish_year: data[theadRule[9]],
|
|
|
|
+ school_job: data[theadRule[10]],
|
|
|
|
+ phone: data[theadRule[10]],
|
|
|
|
+ qq: data[theadRule[11]],
|
|
|
|
+ family_place: data[theadRule[12]],
|
|
|
|
+ family_is_hard: data[theadRule[13]],
|
|
|
|
+ have_grant: data[theadRule[14]],
|
|
|
|
+ termid,
|
|
|
|
+ schid,
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ exceldata = [ ...exceldata, ..._datas ];
|
|
|
|
+ return exceldata;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取导入的XLSX文件中的数据
|
|
|
|
+ async datacheck(studatas) {
|
|
|
|
+ let errorcode = '0';
|
|
|
|
+ let errormsg = '';
|
|
|
|
+ for (const data of studatas) {
|
|
|
|
+ // 判断是否为空
|
|
|
|
+ if (!data.name) {
|
|
|
|
+ errorcode = '1';
|
|
|
|
+ errormsg = errormsg + '姓名不允许为空,';
|
|
|
|
+ }
|
|
|
|
+ if (!data.id_number) {
|
|
|
|
+ errorcode = '1';
|
|
|
|
+ errormsg = errormsg + '身份证号不允许为空,';
|
|
|
|
+ }
|
|
|
|
+ if (!data.school_name) {
|
|
|
|
+ errorcode = '1';
|
|
|
|
+ errormsg = errormsg + '学校名称不允许为空,';
|
|
|
|
+ }
|
|
|
|
+ if (!data.phone) {
|
|
|
|
+ errorcode = '1';
|
|
|
|
+ errormsg = errormsg + '手机号不允许为空,';
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return { errorcode, errormsg };
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+module.exports = SchtimeService;
|