school.js 4.5 KB

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