school.js 8.2 KB

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