school.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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, batchid } = 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(
  40. _filepath,
  41. termid,
  42. schid,
  43. planid,
  44. planyearid,
  45. type,
  46. batchid
  47. );
  48. // 将得到的数据校验
  49. const datacheck = await this.datacheck(studatas);
  50. if (datacheck.errorcode === '1') {
  51. return datacheck;
  52. }
  53. const school_ = await this.model.findOne({ code: schid });
  54. let schname = '';
  55. if (school_) {
  56. schname = school_.name;
  57. }
  58. const trem_ = await plan.termnum.id(termid);
  59. if (!trem_) {
  60. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '期信息不存在');
  61. }
  62. const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
  63. if (studatas.length > num_) {
  64. const jobdata = {
  65. code: schid,
  66. name: schname,
  67. planid: plan.id,
  68. termid,
  69. term: trem_.term,
  70. batchid,
  71. filepath,
  72. studs: JSON.stringify(studatas),
  73. plannum: num_,
  74. schnum: studatas.length,
  75. isstore: '0',
  76. createtime: nowtime,
  77. type,
  78. reason: '学校上传人数超过预期人数,请联系中心管理员',
  79. };
  80. await this.jmodel.create(jobdata);
  81. throw new BusinessError(
  82. ErrorCode.SERVICE_FAULT,
  83. '学校上传人数超过预期人数,请联系中心管理员'
  84. );
  85. } else if (studatas.length < num_) {
  86. const jobdata = {
  87. code: schid,
  88. name: schname,
  89. planid: plan.id,
  90. termid,
  91. term: trem_.term,
  92. batchid,
  93. filepath,
  94. studs: JSON.stringify(studatas),
  95. plannum: num_,
  96. schnum: studatas.length,
  97. isstore: '0',
  98. createtime: nowtime,
  99. type,
  100. reason: '学校上传人数少于预期人数,请联系中心管理员',
  101. };
  102. await this.jmodel.create(jobdata);
  103. throw new BusinessError(
  104. ErrorCode.SERVICE_FAULT,
  105. '学校上传人数少于预期人数,请联系中心管理员'
  106. );
  107. }
  108. // 将数据存入数据库中
  109. for (const stu of studatas) {
  110. const res = await this.smodel.create(stu);
  111. // if (res) {
  112. // const newdata = { name: stu.name, mobile: stu.phone, type: '4', uid: res.id };
  113. // newdata.passwd = { secret: '12345678' };
  114. // await this.umodel.create(newdata);
  115. // }
  116. }
  117. return datacheck;
  118. }
  119. // 取得学校预计人数
  120. async getschnum(plan, type, schid, termid) {
  121. const schtime = await this.schmodel.findOne({ schid, planid: plan.id });
  122. let { arrange } = schtime;
  123. const { termnum } = plan;
  124. arrange = _.groupBy(arrange, 'term');
  125. const keys = Object.keys(arrange);
  126. let arr = keys.map(key => {
  127. const rt = termnum.find(f => f.term === key);
  128. let ar = arrange[key];
  129. ar = ar.map(a => {
  130. const rb = rt.batchnum.find(f => f.batch === a.batch);
  131. if (rb) {
  132. const bh = _.head(rb.class);
  133. const { type } = bh;
  134. a.type = type;
  135. return a;
  136. }
  137. });
  138. let garr = _.groupBy(ar, 'type');
  139. const gks = Object.keys(garr);
  140. garr = gks.map(gk => {
  141. const { term, termid } = _.head(garr[gk]);
  142. const number = garr[gk].reduce((p, n) => p + n.number * 1, 0);
  143. return { term, termid, number, type: gk };
  144. });
  145. return garr;
  146. });
  147. arr = arr.flat();
  148. const obj_ = _.find(arr, { termid, type });
  149. return obj_.number;
  150. }
  151. // 获取导入的XLSX文件中的数据
  152. async getImportXLSXData(filepath, termid, schid, planid, planyearid, type, batchid) {
  153. console.log(filepath);
  154. const file = await this.ctx.curl(filepath);
  155. const workbook = XLSX.read(file.data);
  156. // 读取内容
  157. let exceldata = [];
  158. const sheetNames = workbook.SheetNames; // 获取表名
  159. const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
  160. // 遍历26个字母
  161. const theadRule = [];
  162. const range = XLSX.utils.decode_range(sheet['!ref']);
  163. const col_start = range.s.c;
  164. const col_end = range.e.c;
  165. for (let i = col_start; i <= col_end; i++) {
  166. const addr = XLSX.utils.encode_col(i) + XLSX.utils.encode_row(0);
  167. theadRule.push(sheet[addr].v);
  168. }
  169. // 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 ];
  170. const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
  171. // const theadRule = [ '序号', '姓名', '性别', '民族', '身份证号', '学校名称', '院系', '专业', '入学年份', '毕业年份', '在校曾担任何种职务', '手机号', 'QQ号', '家庭所在地', '家庭是否困难', '是否获得过助学金' ];
  172. if (!params) return [];
  173. let i = 0;
  174. const length = params.length;
  175. const _datas = [];
  176. let data = {};
  177. for (i; i < length; i++) {
  178. data = params[i];
  179. const diy_ = [];
  180. if (theadRule.length > 18) {
  181. for (let j = 18; j < theadRule.length; j++) {
  182. const newdata = {
  183. itemname: theadRule[j],
  184. itemvalue: data[theadRule[j]],
  185. };
  186. diy_.push(newdata);
  187. }
  188. }
  189. _datas.push({
  190. name: data[theadRule[1]],
  191. gender: data[theadRule[2]],
  192. nation: data[theadRule[3]],
  193. id_number: data[theadRule[4]],
  194. school_name: data[theadRule[5]],
  195. faculty: data[theadRule[6]],
  196. major: data[theadRule[7]],
  197. entry_year: data[theadRule[8]],
  198. finish_year: data[theadRule[9]],
  199. school_job: data[theadRule[10]],
  200. phone: data[theadRule[11]],
  201. qq: data[theadRule[12]],
  202. family_place: data[theadRule[13]],
  203. family_is_hard: data[theadRule[14]],
  204. have_grant: data[theadRule[15]],
  205. edua_level: data[theadRule[16]],
  206. edua_system: data[theadRule[17]],
  207. diy: diy_,
  208. termid,
  209. batchid,
  210. schid,
  211. planid,
  212. planyearid,
  213. type,
  214. });
  215. }
  216. exceldata = [ ...exceldata, ..._datas ];
  217. return exceldata;
  218. }
  219. // 获取导入的XLSX文件中的数据
  220. async datacheck(studatas) {
  221. let errorcode = '0';
  222. const errormsg = [];
  223. for (const data of studatas) {
  224. // 判断是否为空
  225. if (!data.name) {
  226. errorcode = '1';
  227. data.msg = data.msg + '姓名不允许为空,';
  228. }
  229. if (!data.gender) {
  230. errorcode = '1';
  231. data.msg = data.msg + '性别不允许为空,';
  232. }
  233. if (!data.nation) {
  234. errorcode = '1';
  235. data.msg = data.msg + '民族不允许为空,';
  236. }
  237. if (!data.id_number) {
  238. errorcode = '1';
  239. data.msg = data.msg + '身份证号不允许为空,';
  240. }
  241. if (!data.school_name) {
  242. errorcode = '1';
  243. data.msg = data.msg + '学校名称不允许为空,';
  244. }
  245. if (!data.phone) {
  246. errorcode = '1';
  247. data.msg = data.msg + '手机号不允许为空,';
  248. }
  249. if (!data.faculty) {
  250. errorcode = '1';
  251. data.msg = data.msg + '院系不允许为空,';
  252. }
  253. if (!data.major) {
  254. errorcode = '1';
  255. data.msg = data.msg + '专业不允许为空,';
  256. }
  257. if (!data.entry_year) {
  258. errorcode = '1';
  259. data.msg = data.msg + '入学年份不允许为空,';
  260. }
  261. if (!data.finish_year) {
  262. errorcode = '1';
  263. data.msg = data.msg + '毕业年份不允许为空,';
  264. }
  265. if (!data.school_job) {
  266. errorcode = '1';
  267. data.msg = data.msg + '职务不允许为空,';
  268. }
  269. if (!data.qq) {
  270. errorcode = '1';
  271. data.msg = data.msg + 'QQ号不允许为空,';
  272. }
  273. if (!data.family_place) {
  274. errorcode = '1';
  275. data.msg = data.msg + '家庭所在地不允许为空,';
  276. }
  277. if (!data.family_is_hard) {
  278. errorcode = '1';
  279. data.msg = data.msg + '家庭是否困难不允许为空,';
  280. }
  281. if (!data.have_grant) {
  282. errorcode = '1';
  283. data.msg = data.msg + '是否获得过助学金不允许为空,';
  284. }
  285. if (!/^\d{11}$/i.test(data.phone)) {
  286. errorcode = '1';
  287. data.msg = data.msg + '手机号不正确,';
  288. }
  289. const res = await this.smodel.findOne({ id_number: data.id_number });
  290. if (res) {
  291. errorcode = '1';
  292. data.msg = data.msg + '学生已经存在请检查,';
  293. }
  294. if (errorcode === '1') {
  295. errormsg.push(data);
  296. }
  297. }
  298. return { errorcode, errormsg };
  299. }
  300. // 导出学校名单
  301. async exportSchool({ trainplanId }) {
  302. // 批次期次都在这里面
  303. const trainplan = await this.tmodel.find({ _id: trainplanId });
  304. console.log(trainplan);
  305. const _headers = [
  306. { key: 'title', title: '计划标题' },
  307. ];
  308. // 需要打出的列表
  309. const _data = trainplan;
  310. const headers = _headers.map(({ title }) =>
  311. title).map((v, i) =>
  312. Object.assign({}, { v, position: String.fromCharCode(65 + i) + 1 })
  313. ).reduce(
  314. (prev, next) =>
  315. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  316. {}
  317. );
  318. const data = _data.map((v, i) =>
  319. _headers.map(({ key }, j) =>
  320. Object.assign(
  321. {},
  322. { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) }
  323. )
  324. )
  325. )
  326. .reduce((prev, next) => prev.concat(next))
  327. .reduce(
  328. (prev, next) =>
  329. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  330. {}
  331. );
  332. // 合并 headers 和 data
  333. const output = Object.assign({}, headers, data);
  334. // 获取所有单元格的位置
  335. const outputPos = Object.keys(output);
  336. // 计算出范围
  337. const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
  338. // 构建 workbook 对象
  339. const nowDate = new Date().getTime();
  340. const path =
  341. 'D:\\wwwroot\\service\\service-file\\upload\\train\\' +
  342. nowDate +
  343. '.xlsx';
  344. const respath =
  345. 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
  346. const wb = {
  347. SheetNames: [ 'sheet0' ],
  348. Sheets: { sheet0: Object.assign({}, output, { '!ref': ref }) },
  349. };
  350. // 导出 Excel
  351. XLSX.writeFile(wb, path);
  352. return respath;
  353. }
  354. async updateclass({ trainplanid, classid, rightHeader }) {
  355. assert(trainplanid && classid && rightHeader, '缺少参数项');
  356. // 根据全年计划表id查出对应的全年计划详细信息
  357. const trainplan = await this.model.findById(trainplanid);
  358. if (!trainplan) {
  359. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  360. }
  361. for (const term of trainplan.termnum) {
  362. for (const batch of term.batchnum) {
  363. const class_ = await batch.class.id(classid);
  364. if (class_) {
  365. class_.headteacherid = rightHeader;
  366. }
  367. }
  368. }
  369. return await trainplan.save();
  370. }
  371. async updatereteacher({ trainplanid, termid, reteacher }) {
  372. assert(trainplanid && termid && reteacher, '缺少参数项');
  373. // 根据全年计划表id查出对应的全年计划详细信息
  374. const trainplan = await this.model.findById(trainplanid);
  375. if (!trainplan) {
  376. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  377. }
  378. const term = await trainplan.termnum.id(termid);
  379. if (term) {
  380. term.reteacher = reteacher;
  381. }
  382. return await trainplan.save();
  383. }
  384. }
  385. module.exports = SchoolService;