school.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 create(data) {
  20. const { code, name } = data;
  21. assert(code, '缺少学校代码');
  22. assert(name, '缺少学校名称');
  23. const res = await this.model.create(data);
  24. if (res) {
  25. const obj = { mobile: code, name, type: '2', uid: res._id, passwd: { secret: '12345678' } };
  26. await this.umodel.create(obj);
  27. }
  28. return res;
  29. }
  30. async stuimport(data) {
  31. const { filepath, termid, schid, type, batchid } = data;
  32. assert(filepath, 'filepath不能为空');
  33. assert(termid, 'termid不能为空');
  34. assert(schid, 'schid不能为空');
  35. // 根据termid取得计划信息
  36. const plan = await this.tmodel.findOne({ 'termnum._id': ObjectId(termid) });
  37. if (!plan) {
  38. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '计划信息不存在');
  39. }
  40. // 取得学校预计人数
  41. const num_ = await this.getschnum(plan, type, schid, termid, batchid);
  42. console.log('*******************');
  43. console.log(num_);
  44. console.log('*******************');
  45. const planid = plan.id;
  46. const planyearid = plan.planyearid;
  47. // 取得excle中数据
  48. const _filepath = 'http://127.0.0.1' + filepath; // this.ctx.app.config.baseUrl http://127.0.0.1 http://jytz.jilinjobs.cn
  49. const studatas = await this.getImportXLSXData(
  50. _filepath,
  51. termid,
  52. schid,
  53. planid,
  54. planyearid,
  55. type,
  56. batchid
  57. );
  58. // 将得到的数据校验
  59. const datacheck = await this.datacheck(studatas);
  60. if (datacheck.errorcode === '1') {
  61. return datacheck;
  62. }
  63. const school_ = await this.model.findOne({ code: schid });
  64. let schname = '';
  65. if (school_) {
  66. schname = school_.name;
  67. }
  68. const trem_ = await plan.termnum.id(termid);
  69. if (!trem_) {
  70. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '期信息不存在');
  71. }
  72. const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
  73. if (studatas.length > num_) {
  74. const jobdata = {
  75. code: schid,
  76. name: schname,
  77. planid: plan.id,
  78. termid,
  79. term: trem_.term,
  80. batchid,
  81. filepath,
  82. studs: JSON.stringify(studatas),
  83. plannum: num_,
  84. schnum: studatas.length,
  85. isstore: '0',
  86. createtime: nowtime,
  87. type,
  88. reason: '学校上传人数超过预期人数,请联系中心管理员',
  89. };
  90. await this.jmodel.create(jobdata);
  91. throw new BusinessError(
  92. ErrorCode.SERVICE_FAULT,
  93. '学校上传人数超过预期人数,请联系中心管理员'
  94. );
  95. } else if (studatas.length < num_) {
  96. const jobdata = {
  97. code: schid,
  98. name: schname,
  99. planid: plan.id,
  100. termid,
  101. term: trem_.term,
  102. batchid,
  103. filepath,
  104. studs: JSON.stringify(studatas),
  105. plannum: num_,
  106. schnum: studatas.length,
  107. isstore: '0',
  108. createtime: nowtime,
  109. type,
  110. reason: '学校上传人数少于预期人数,请联系中心管理员',
  111. };
  112. await this.jmodel.create(jobdata);
  113. throw new BusinessError(
  114. ErrorCode.SERVICE_FAULT,
  115. '学校上传人数少于预期人数,请联系中心管理员'
  116. );
  117. }
  118. // 将数据存入数据库中
  119. for (const stu of studatas) {
  120. const res = await this.smodel.create(stu);
  121. // if (res) {
  122. // const newdata = { name: stu.name, mobile: stu.phone, type: '4', uid: res.id };
  123. // newdata.passwd = { secret: '12345678' };
  124. // await this.umodel.create(newdata);
  125. // }
  126. }
  127. return datacheck;
  128. }
  129. // 取得学校预计人数
  130. async getschnum(plan, type, schid, termid, batchid) {
  131. const schtime = await this.schmodel.findOne({ schid, planid: plan.id });
  132. const { arrange } = schtime;
  133. const r = arrange.find(f => f.batchid === batchid);
  134. if (!r) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到该学校的计划');
  135. const { number } = r;
  136. return parseInt(number);
  137. // const { termnum } = plan;
  138. // arrange = _.groupBy(arrange, 'termid');
  139. // const keys = Object.keys(arrange);
  140. // let arr = keys.map(key => {
  141. // const rt = termnum.find(f => ObjectId(key).equals(f._id));
  142. // let ar = arrange[key];
  143. // ar = ar.map(a => {
  144. // const rb = rt.batchnum.find(f => ObjectId(a.batchid).equals(f._id));
  145. // if (rb) {
  146. // const bh = _.head(rb.class);
  147. // const { type } = bh;
  148. // a.type = type;
  149. // return a;
  150. // }
  151. // });
  152. // let garr = _.groupBy(ar, 'type');
  153. // const gks = Object.keys(garr);
  154. // garr = gks.map(gk => {
  155. // const { term, termid } = _.head(garr[gk]);
  156. // const number = garr[gk].reduce((p, n) => p + n.number * 1, 0);
  157. // return { term, termid, number, type: gk };
  158. // });
  159. // return garr;
  160. // });
  161. // arr = arr.flat();
  162. // const obj_ = _.find(arr, { termid, type });
  163. return obj_.number;
  164. }
  165. // 获取导入的XLSX文件中的数据
  166. async getImportXLSXData(filepath, termid, schid, planid, planyearid, type, batchid) {
  167. const file = await this.ctx.curl(filepath);
  168. const workbook = XLSX.read(file.data);
  169. // 读取内容
  170. let exceldata = [];
  171. const sheetNames = workbook.SheetNames; // 获取表名
  172. const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
  173. // 遍历26个字母
  174. const theadRule = [];
  175. const range = XLSX.utils.decode_range(sheet['!ref']);
  176. const col_start = range.s.c;
  177. const col_end = range.e.c;
  178. for (let i = col_start; i <= col_end; i++) {
  179. const addr = XLSX.utils.encode_col(i) + XLSX.utils.encode_row(0);
  180. theadRule.push(sheet[addr].v);
  181. }
  182. // 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 ];
  183. const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
  184. // const theadRule = [ '序号', '姓名', '性别', '民族', '身份证号', '学校名称', '院系', '专业', '入学年份', '毕业年份', '在校曾担任何种职务', '手机号', 'QQ号', '家庭所在地', '家庭是否困难', '是否获得过助学金' ];
  185. if (!params) return [];
  186. let i = 0;
  187. const length = params.length;
  188. const _datas = [];
  189. let data = {};
  190. for (i; i < length; i++) {
  191. data = params[i];
  192. const diy_ = [];
  193. if (theadRule.length > 18) {
  194. for (let j = 18; j < theadRule.length; j++) {
  195. const newdata = {
  196. itemname: theadRule[j],
  197. itemvalue: data[theadRule[j]],
  198. };
  199. diy_.push(newdata);
  200. }
  201. }
  202. _datas.push({
  203. name: data[theadRule[1]],
  204. gender: data[theadRule[2]],
  205. nation: data[theadRule[3]],
  206. id_number: data[theadRule[4]],
  207. school_name: data[theadRule[5]],
  208. faculty: data[theadRule[6]],
  209. major: data[theadRule[7]],
  210. entry_year: data[theadRule[8]],
  211. finish_year: data[theadRule[9]],
  212. school_job: data[theadRule[10]],
  213. phone: data[theadRule[11]],
  214. qq: data[theadRule[12]],
  215. family_place: data[theadRule[13]],
  216. family_is_hard: data[theadRule[14]],
  217. have_grant: data[theadRule[15]],
  218. edua_level: data[theadRule[16]],
  219. edua_system: data[theadRule[17]],
  220. diy: diy_,
  221. termid,
  222. batchid,
  223. schid,
  224. planid,
  225. planyearid,
  226. type,
  227. });
  228. }
  229. exceldata = [ ...exceldata, ..._datas ];
  230. return exceldata;
  231. }
  232. // 获取导入的XLSX文件中的数据
  233. async datacheck(studatas) {
  234. let errorcode = '0';
  235. const errormsg = [];
  236. for (const data of studatas) {
  237. // 判断是否为空
  238. if (!data.name) {
  239. errorcode = '1';
  240. data.msg = (data.msg || '') + '姓名不允许为空,';
  241. }
  242. if (!data.gender) {
  243. errorcode = '1';
  244. data.msg = (data.msg || '') + '性别不允许为空,';
  245. }
  246. if (!data.nation) {
  247. errorcode = '1';
  248. data.msg = (data.msg || '') + '民族不允许为空,';
  249. }
  250. if (!data.id_number) {
  251. errorcode = '1';
  252. data.msg = (data.msg || '') + '身份证号不允许为空,';
  253. } else {
  254. const { pass, msg } = this.idCodeValid(data.id_number);
  255. if (!pass) {
  256. errorcode = '1';
  257. data.msg = (data.msg || '') + `${msg},`;
  258. }
  259. }
  260. if (!data.school_name) {
  261. errorcode = '1';
  262. data.msg = (data.msg || '') + '学校名称不允许为空,';
  263. }
  264. if (!data.phone) {
  265. errorcode = '1';
  266. data.msg = (data.msg || '') + '手机号不允许为空,';
  267. }
  268. if (!data.faculty) {
  269. errorcode = '1';
  270. data.msg = (data.msg || '') + '院系不允许为空,';
  271. }
  272. if (!data.major) {
  273. errorcode = '1';
  274. data.msg = (data.msg || '') + '专业不允许为空,';
  275. }
  276. if (!data.entry_year) {
  277. errorcode = '1';
  278. data.msg = (data.msg || '') + '入学年份不允许为空,';
  279. }
  280. if (!data.finish_year) {
  281. errorcode = '1';
  282. data.msg = (data.msg || '') + '毕业年份不允许为空,';
  283. }
  284. if (!data.school_job) {
  285. errorcode = '1';
  286. data.msg = (data.msg || '') + '职务不允许为空,';
  287. }
  288. if (!data.qq) {
  289. errorcode = '1';
  290. data.msg = (data.msg || '') + 'QQ号不允许为空,';
  291. }
  292. if (!data.family_place) {
  293. errorcode = '1';
  294. data.msg = (data.msg || '') + '家庭所在地不允许为空,';
  295. }
  296. if (!data.family_is_hard) {
  297. errorcode = '1';
  298. data.msg = (data.msg || '') + '家庭是否困难不允许为空,';
  299. }
  300. if (!data.have_grant) {
  301. errorcode = '1';
  302. data.msg = (data.msg || '') + '是否获得过助学金不允许为空,';
  303. }
  304. if (!/^\d{11}$/i.test(data.phone)) {
  305. errorcode = '1';
  306. data.msg = (data.msg || '') + '手机号不正确,';
  307. }
  308. const res = await this.smodel.findOne({ id_number: data.id_number });
  309. if (res) {
  310. errorcode = '1';
  311. data.msg = (data.msg || '') + '学生已经存在请检查,';
  312. }
  313. if (errorcode === '1') {
  314. errormsg.push(data);
  315. }
  316. }
  317. return { errorcode, errormsg };
  318. }
  319. // 导出学校名单
  320. async exportSchool({ trainplanId }) {
  321. // 批次期次都在这里面
  322. const trainplan = await this.tmodel.find({ _id: trainplanId });
  323. const _headers = [
  324. { key: 'title', title: '计划标题' },
  325. ];
  326. // 需要打出的列表
  327. const _data = trainplan;
  328. const headers = _headers.map(({ title }) =>
  329. title).map((v, i) =>
  330. Object.assign({}, { v, position: String.fromCharCode(65 + i) + 1 })
  331. ).reduce(
  332. (prev, next) =>
  333. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  334. {}
  335. );
  336. const data = _data.map((v, i) =>
  337. _headers.map(({ key }, j) =>
  338. Object.assign(
  339. {},
  340. { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) }
  341. )
  342. )
  343. )
  344. .reduce((prev, next) => prev.concat(next))
  345. .reduce(
  346. (prev, next) =>
  347. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  348. {}
  349. );
  350. // 合并 headers 和 data
  351. const output = Object.assign({}, headers, data);
  352. // 获取所有单元格的位置
  353. const outputPos = Object.keys(output);
  354. // 计算出范围
  355. const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
  356. // 构建 workbook 对象
  357. const nowDate = new Date().getTime();
  358. const path =
  359. 'D:\\wwwroot\\service\\service-file\\upload\\train\\' +
  360. nowDate +
  361. '.xlsx';
  362. const respath =
  363. 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
  364. const wb = {
  365. SheetNames: [ 'sheet0' ],
  366. Sheets: { sheet0: Object.assign({}, output, { '!ref': ref }) },
  367. };
  368. // 导出 Excel
  369. XLSX.writeFile(wb, path);
  370. return respath;
  371. }
  372. async updateclass({ trainplanid, classid, rightHeader }) {
  373. assert(trainplanid && classid && rightHeader, '缺少参数项');
  374. // 根据全年计划表id查出对应的全年计划详细信息
  375. const trainplan = await this.model.findById(trainplanid);
  376. if (!trainplan) {
  377. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  378. }
  379. for (const term of trainplan.termnum) {
  380. for (const batch of term.batchnum) {
  381. const class_ = await batch.class.id(classid);
  382. if (class_) {
  383. class_.headteacherid = rightHeader;
  384. }
  385. }
  386. }
  387. return await trainplan.save();
  388. }
  389. async updatereteacher({ trainplanid, termid, reteacher }) {
  390. assert(trainplanid && termid && reteacher, '缺少参数项');
  391. // 根据全年计划表id查出对应的全年计划详细信息
  392. const trainplan = await this.model.findById(trainplanid);
  393. if (!trainplan) {
  394. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  395. }
  396. const term = await trainplan.termnum.id(termid);
  397. if (term) {
  398. term.reteacher = reteacher;
  399. }
  400. return await trainplan.save();
  401. }
  402. idCodeValid(code) {
  403. // 身份证号合法性验证
  404. // 支持15位和18位身份证号
  405. // 支持地址编码、出生日期、校验位验证
  406. const city = {
  407. 11: '北京',
  408. 12: '天津',
  409. 13: '河北',
  410. 14: '山西',
  411. 15: '内蒙古',
  412. 21: '辽宁',
  413. 22: '吉林',
  414. 23: '黑龙江 ',
  415. 31: '上海',
  416. 32: '江苏',
  417. 33: '浙江',
  418. 34: '安徽',
  419. 35: '福建',
  420. 36: '江西',
  421. 37: '山东',
  422. 41: '河南',
  423. 42: '湖北 ',
  424. 43: '湖南',
  425. 44: '广东',
  426. 45: '广西',
  427. 46: '海南',
  428. 50: '重庆',
  429. 51: '四川',
  430. 52: '贵州',
  431. 53: '云南',
  432. 54: '西藏 ',
  433. 61: '陕西',
  434. 62: '甘肃',
  435. 63: '青海',
  436. 64: '宁夏',
  437. 65: '新疆',
  438. 71: '台湾',
  439. 81: '香港',
  440. 82: '澳门',
  441. 91: '国外 ',
  442. };
  443. let row = {
  444. pass: true,
  445. msg: '验证成功',
  446. };
  447. if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(code)) {
  448. row = {
  449. pass: false,
  450. msg: '身份证号格式错误',
  451. };
  452. } else if (!city[code.substr(0, 2)]) {
  453. row = {
  454. pass: false,
  455. msg: '身份证号地址编码错误',
  456. };
  457. } else {
  458. // 18位身份证需要验证最后一位校验位
  459. if (code.length == 18) {
  460. code = code.split('');
  461. // ∑(ai×Wi)(mod 11)
  462. // 加权因子
  463. const factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
  464. // 校验位
  465. const parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
  466. let sum = 0;
  467. let ai = 0;
  468. let wi = 0;
  469. for (let i = 0; i < 17; i++) {
  470. ai = code[i];
  471. wi = factor[i];
  472. sum += ai * wi;
  473. }
  474. if (parity[sum % 11] != code[17].toUpperCase()) {
  475. row = {
  476. pass: false,
  477. msg: '身份证号校验位错误',
  478. };
  479. }
  480. }
  481. }
  482. return row;
  483. }
  484. }
  485. module.exports = SchoolService;