school.js 4.2 KB

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