school.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 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 ];
  59. const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
  60. // const theadRule = [ '序号', '姓名', '性别', '民族', '身份证号', '学校名称', '院系', '专业', '入学年份', '毕业年份', '在校曾担任何种职务', '手机号', 'QQ号', '家庭所在地', '家庭是否困难', '是否获得过助学金' ];
  61. if (!params) return [];
  62. let i = 0;
  63. const length = params.length;
  64. const _datas = [];
  65. let data = {};
  66. for (i; i < length; i++) {
  67. data = params[i];
  68. _datas.push({
  69. name: data[theadRule[1]],
  70. gender: data[theadRule[2]],
  71. nation: data[theadRule[3]],
  72. id_number: data[theadRule[4]],
  73. school_name: data[theadRule[5]],
  74. faculty: data[theadRule[6]],
  75. major: data[theadRule[7]],
  76. entry_year: data[theadRule[8]],
  77. finish_year: data[theadRule[9]],
  78. school_job: data[theadRule[10]],
  79. phone: data[theadRule[11]],
  80. qq: data[theadRule[12]],
  81. family_place: data[theadRule[13]],
  82. family_is_hard: data[theadRule[14]],
  83. have_grant: data[theadRule[15]],
  84. termid,
  85. schid,
  86. planid,
  87. planyearid,
  88. });
  89. }
  90. exceldata = [ ...exceldata, ..._datas ];
  91. return exceldata;
  92. }
  93. // 获取导入的XLSX文件中的数据
  94. async datacheck(studatas) {
  95. let errorcode = '0';
  96. const errormsg = [];
  97. for (const data of studatas) {
  98. // 判断是否为空
  99. if (!data.name) {
  100. errorcode = '1';
  101. data.msg = data.msg + '姓名不允许为空,';
  102. }
  103. if (!data.gender) {
  104. errorcode = '1';
  105. data.msg = data.msg + '性别不允许为空,';
  106. }
  107. if (!data.id_number) {
  108. errorcode = '1';
  109. data.msg = data.msg + '身份证号不允许为空,';
  110. }
  111. if (!data.school_name) {
  112. errorcode = '1';
  113. data.msg = data.msg + '学校名称不允许为空,';
  114. }
  115. if (!data.phone) {
  116. errorcode = '1';
  117. data.msg = data.msg + '手机号不允许为空,';
  118. }
  119. if (!/^\d{11}$/i.test(data.phone)) {
  120. errorcode = '1';
  121. data.msg = data.msg + '手机号不正确,';
  122. }
  123. const res = await this.model.findOne({ id_number: data.id_number });
  124. if (res) {
  125. errorcode = '1';
  126. data.msg = data.msg + '学生已经存在请检查,';
  127. }
  128. if (errorcode === '1') {
  129. errormsg.push(data);
  130. }
  131. }
  132. return { errorcode, errormsg };
  133. }
  134. }
  135. module.exports = SchoolService;