schoolctrl.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const XLSX = require('xlsx');
  8. class SchtimeService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'schoolctrl');
  11. this.model = this.ctx.model.Student;
  12. }
  13. async stuimport(data) {
  14. const { filepath, termid, schid } = data;
  15. // 取得excle中数据
  16. const studatas = await this.getImportXLSXData(filepath, termid, schid);
  17. // 将得到的数据校验
  18. const datacheck = await this.datacheck(studatas);
  19. if (datacheck.errorcode === '1') {
  20. return datacheck;
  21. }
  22. // 将数据存入数据库中
  23. for (const stu of studatas) {
  24. await this.model.create(stu);
  25. }
  26. return datacheck;
  27. }
  28. // 获取导入的XLSX文件中的数据
  29. async getImportXLSXData(filepath, termid, schid) {
  30. const workbook = XLSX.readFile(filepath);
  31. // 读取内容
  32. let exceldata = [];
  33. const sheetNames = workbook.SheetNames; // 获取表名
  34. const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
  35. 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 ];
  36. const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
  37. // const theadRule = [ '序号', '姓名', '性别', '民族', '身份证号', '学校名称', '院系', '专业', '入学年份', '毕业年份', '在校曾担任何种职务', '手机号', 'QQ号', '家庭所在地', '家庭是否困难', '是否获得过助学金' ];
  38. console.log(params);
  39. if (!params) return [];
  40. let i = 0;
  41. const length = params.length;
  42. const _datas = [];
  43. let data = {};
  44. for (i; i < length; i++) {
  45. data = params[i];
  46. _datas.push({
  47. name: data[theadRule[1]],
  48. gender: data[theadRule[2]],
  49. nation: data[theadRule[3]],
  50. id_number: data[theadRule[4]],
  51. school_name: data[theadRule[5]],
  52. faculty: data[theadRule[6]],
  53. major: data[theadRule[7]],
  54. entry_year: data[theadRule[8]],
  55. finish_year: data[theadRule[9]],
  56. school_job: data[theadRule[10]],
  57. phone: data[theadRule[10]],
  58. qq: data[theadRule[11]],
  59. family_place: data[theadRule[12]],
  60. family_is_hard: data[theadRule[13]],
  61. have_grant: data[theadRule[14]],
  62. termid,
  63. schid,
  64. });
  65. }
  66. exceldata = [ ...exceldata, ..._datas ];
  67. return exceldata;
  68. }
  69. // 获取导入的XLSX文件中的数据
  70. async datacheck(studatas) {
  71. let errorcode = '0';
  72. let errormsg = '';
  73. for (const data of studatas) {
  74. // 判断是否为空
  75. if (!data.name) {
  76. errorcode = '1';
  77. errormsg = errormsg + '姓名不允许为空,';
  78. }
  79. if (!data.id_number) {
  80. errorcode = '1';
  81. errormsg = errormsg + '身份证号不允许为空,';
  82. }
  83. if (!data.school_name) {
  84. errorcode = '1';
  85. errormsg = errormsg + '学校名称不允许为空,';
  86. }
  87. if (!data.phone) {
  88. errorcode = '1';
  89. errormsg = errormsg + '手机号不允许为空,';
  90. }
  91. }
  92. return { errorcode, errormsg };
  93. }
  94. }
  95. module.exports = SchtimeService;