school.js 4.5 KB

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