school.js 5.6 KB

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