school.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 SchoolService 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. assert(filepath, 'filepath不能为空');
  16. assert(termid, 'termid不能为空');
  17. assert(schid, 'schid不能为空');
  18. // 取得excle中数据
  19. const _filepath = this.ctx.app.config.baseUrl + filepath;
  20. const studatas = await this.getImportXLSXData(_filepath, termid, schid);
  21. // 将得到的数据校验
  22. const datacheck = await this.datacheck(studatas);
  23. if (datacheck.errorcode === '1') {
  24. return datacheck;
  25. }
  26. // 将数据存入数据库中
  27. for (const stu of studatas) {
  28. await this.model.create(stu);
  29. }
  30. return datacheck;
  31. }
  32. // 获取导入的XLSX文件中的数据
  33. async getImportXLSXData(filepath, termid, schid) {
  34. const workbook = XLSX.readFile(filepath);
  35. // 读取内容
  36. let exceldata = [];
  37. const sheetNames = workbook.SheetNames; // 获取表名
  38. const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
  39. 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 ];
  40. const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
  41. // const theadRule = [ '序号', '姓名', '性别', '民族', '身份证号', '学校名称', '院系', '专业', '入学年份', '毕业年份', '在校曾担任何种职务', '手机号', 'QQ号', '家庭所在地', '家庭是否困难', '是否获得过助学金' ];
  42. console.log(params);
  43. if (!params) return [];
  44. let i = 0;
  45. const length = params.length;
  46. const _datas = [];
  47. let data = {};
  48. for (i; i < length; i++) {
  49. data = params[i];
  50. _datas.push({
  51. name: data[theadRule[1]],
  52. gender: data[theadRule[2]],
  53. nation: data[theadRule[3]],
  54. id_number: data[theadRule[4]],
  55. school_name: data[theadRule[5]],
  56. faculty: data[theadRule[6]],
  57. major: data[theadRule[7]],
  58. entry_year: data[theadRule[8]],
  59. finish_year: data[theadRule[9]],
  60. school_job: data[theadRule[10]],
  61. phone: data[theadRule[10]],
  62. qq: data[theadRule[11]],
  63. family_place: data[theadRule[12]],
  64. family_is_hard: data[theadRule[13]],
  65. have_grant: data[theadRule[14]],
  66. termid,
  67. schid,
  68. });
  69. }
  70. exceldata = [ ...exceldata, ..._datas ];
  71. return exceldata;
  72. }
  73. // 获取导入的XLSX文件中的数据
  74. async datacheck(studatas) {
  75. let errorcode = '0';
  76. const errormsg = [];
  77. for (const data of studatas) {
  78. // 判断是否为空
  79. if (!data.name) {
  80. errorcode = '1';
  81. data.msg = '姓名不允许为空';
  82. errormsg.push(data);
  83. }
  84. if (!data.id_number) {
  85. errorcode = '1';
  86. data.msg = '身份证号不允许为空';
  87. errormsg.push(data);
  88. }
  89. if (!data.school_name) {
  90. errorcode = '1';
  91. data.msg = '姓学校名称不允许为空';
  92. errormsg.push(data);
  93. }
  94. if (!data.phone) {
  95. errorcode = '1';
  96. data.msg = '手机号不允许为空';
  97. errormsg.push(data);
  98. }
  99. if (!/^\d{11}$/i.test(data.phone)) {
  100. errorcode = '1';
  101. data.msg = '手机号不正确';
  102. errormsg.push(data);
  103. }
  104. const res = await this.model.findOne({ id_number: data.id_number });
  105. if (res) {
  106. errorcode = '1';
  107. data.msg = '学生已经存在请检查';
  108. errormsg.push(data);
  109. }
  110. }
  111. return { errorcode, errormsg };
  112. }
  113. }
  114. module.exports = SchoolService;