school.js 8.3 KB

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