school.js 6.9 KB

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