school.js 8.1 KB

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