school.js 4.8 KB

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