trainplan.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { CrudService } = require('naf-framework-mongoose/lib/service');
  4. const assert = require('assert');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. const XLSX = require('xlsx');
  7. const utils = require('../utils/utils.js');
  8. const moment = require('moment');
  9. const XLSXStyle = require('xlsx-style');
  10. class TrainplanService extends CrudService {
  11. constructor(ctx) {
  12. super(ctx, 'trainplan');
  13. this.model = this.ctx.model.Trainplan;
  14. this.clamodel = this.ctx.model.Class;
  15. this.umodel = this.ctx.model.User;
  16. this.smodel = this.ctx.model.School;
  17. this.tmodel = this.ctx.model.Teacher;
  18. this.stumodel = this.ctx.model.Student;
  19. this.schmodel = this.ctx.model.Schtime;
  20. this.lmmodel = this.ctx.model.Lessonmode;
  21. }
  22. async create(data) {
  23. const { planyearid, year, title } = data;
  24. assert(planyearid, '缺少大批次信息');
  25. assert(year, '缺少年度');
  26. assert(title, '缺少标题');
  27. const res = await this.model.create(data);
  28. let planid = '';
  29. if (res) planid = res._id;
  30. const schoolList = await this.smodel.find();
  31. const schtimeArr = [];
  32. for (const sch of schoolList) {
  33. const { code } = sch;
  34. const obj = { schid: code, year, planid };
  35. const schtimeres = await this.schmodel.create(obj);
  36. if (schtimeres) schtimeArr.push(schtimeres);
  37. }
  38. if (!schtimeArr.every(e => e)) { throw new BusinessError(ErrorCode.DATA_INVALID, '学校计划生成失败'); } else return res;
  39. }
  40. async update({ id }, data) {
  41. const trainplan = await this.model.findById(id);
  42. // 保存原数据
  43. const trainplanold = _.cloneDeep(trainplan);
  44. const { year, title, termnum, festivals, status, school } = data;
  45. if (year) {
  46. trainplan.year = year;
  47. }
  48. if (title) {
  49. trainplan.title = title;
  50. }
  51. if (termnum) {
  52. trainplan.termnum = termnum;
  53. }
  54. if (school) {
  55. trainplan.school = school;
  56. }
  57. if (festivals) {
  58. trainplan.festivals = festivals;
  59. }
  60. if (status === '1') {
  61. trainplan.status = status;
  62. }
  63. // 日历安排中添加课表信息,查询每种班级类型的课表,然后显示
  64. if (trainplan.termnum) {
  65. // trainplan.termnum =
  66. trainplan.termnum = await this.termGetLesson(trainplan.termnum);
  67. // 重新查看每个班级是否有课程
  68. trainplan.termnum = this.setClassLesson(trainplan.termnum);
  69. }
  70. // 如果培训计划状态改为发布,发送培训计划信息,并自动生成班级
  71. const res = await trainplan.save();
  72. if (res) {
  73. if (status === '1') {
  74. // 自动生成班级
  75. // await this.autoclass(res, trainplanold);
  76. // await this.autoclassNew(res, trainplanold);
  77. // 将生成的班级重新将班级排班名
  78. // await this.autoclassname(res);
  79. // 发送培训计划信息通知给相应人员
  80. // 查询所有入库的教师
  81. // 不需要给教师发信息
  82. // const teachers = await this.tmodel.find({ status: '4' });
  83. // for (const teacher of teachers) {
  84. // const teacherid = teacher._id;
  85. // const _teacher = await this.umodel.findOne({
  86. // uid: teacherid,
  87. // type: '3',
  88. // });
  89. // const openid = _teacher.openid;
  90. // const detail = trainplan.title + '已发布,请注意查收!';
  91. // const date = await this.ctx.service.util.updatedate();
  92. // const remark = '感谢您的使用';
  93. // if (openid) {
  94. // this.ctx.service.weixin.sendTemplateMsg(
  95. // this.ctx.app.config.REVIEW_TEMPLATE_ID,
  96. // openid,
  97. // '您有一个新的通知',
  98. // detail,
  99. // date,
  100. // remark
  101. // );
  102. // }
  103. // }
  104. // 查询所有学校用户
  105. // const schools = await this.umodel.find({ type: '2' });
  106. // for (const school of schools) {
  107. // const openid = school.openid;
  108. // const detail = trainplan.title + '已发布,请注意查收!';
  109. // const date = await this.ctx.service.util.updatedate();
  110. // const remark = '感谢您的使用';
  111. // if (openid) {
  112. // this.ctx.service.weixin.sendTemplateMsg(
  113. // this.ctx.app.config.REVIEW_TEMPLATE_ID,
  114. // openid,
  115. // '您有一个新的通知',
  116. // detail,
  117. // date,
  118. // remark
  119. // );
  120. // }
  121. // }
  122. }
  123. // 查哪个学校schtime表没有生成,就给生成了
  124. const schoolList = await this.smodel.find();
  125. for (const school of schoolList) {
  126. const r = await this.schmodel.findOne({ year, planid: id, schid: school.code });
  127. if (r) continue;
  128. const obj = { schid: school.code, year, planid: id };
  129. await this.schmodel.create(obj);
  130. }
  131. }
  132. return res;
  133. }
  134. async termGetLesson(termnum) {
  135. const lessonModelList = await this.lmmodel.find();
  136. for (const term of termnum) {
  137. for (const batch of term.batchnum) {
  138. const { class: classes, startdate, enddate } = batch;
  139. // 获取每批次下每个班的班级类型
  140. const typeList = _.uniq(classes.map(i => i.type));
  141. const h = _.head(typeList);
  142. if (!h) continue;
  143. const tem = lessonModelList.find(f => f.type === h);
  144. if (!tem) continue;
  145. let { lessons } = tem;
  146. if (!lessons) continue;
  147. lessons = JSON.parse(lessons);
  148. // 过滤出上课的时间段
  149. lessons = lessons.filter(f => {
  150. const keys = Object.keys(f).filter(f => f.includes('subid'));
  151. return keys.length > 0;
  152. });
  153. // 记录上课的时间
  154. const times = [];
  155. // 记录所有的科目
  156. let subject = [];
  157. lessons.map(i => {
  158. times.push(i.time);
  159. const keys = Object.keys(i);
  160. let arr = [];
  161. for (const key of keys) {
  162. if (key.match(/\d/g)) arr.push(_.head(key.match(/\d/g)));
  163. }
  164. arr = _.uniq(arr);
  165. for (const ai of arr) {
  166. if (i[`day${ai}subid`]) {
  167. subject.push({
  168. subname: i[`day${ai}`],
  169. subid: i[`day${ai}subid`],
  170. day: ai,
  171. });
  172. }
  173. }
  174. return i;
  175. });
  176. // 去重
  177. subject = _.uniqBy(subject, 'subid');
  178. // 获得天列表
  179. const dnum = moment(enddate).diff(moment(startdate), 'days') + 1;
  180. const dayList = [];
  181. for (let ind = 0; ind < dnum; ind++) {
  182. dayList.push(moment(startdate).add(ind, 'd').format('YYYY-MM-DD'));
  183. }
  184. // 将subject中的day换成日期
  185. for (const sub of subject) {
  186. sub.day = dayList[sub.day * 1 - 1];
  187. sub.time = times;
  188. }
  189. batch.lessons = subject;
  190. }
  191. }
  192. return termnum;
  193. }
  194. // 设置课表模板到班级上
  195. setClassLesson(termnum) {
  196. // 为班级设置lessons,有的就不设置了
  197. for (const t of termnum) {
  198. const { batchnum } = t;
  199. if (!batchnum || !_.isArray(batchnum)) {
  200. this.$message.error(`第${t.term}期的批次数据错误!`);
  201. continue;
  202. }
  203. for (const b of t.batchnum) {
  204. const { class: cla, lessons: ltemplate } = b;
  205. if (!cla) {
  206. console.warn(`${t.term}期${b.batch}批次没有班级列表`);
  207. continue;
  208. }
  209. if (!ltemplate) {
  210. console.warn(`${t.term}期${b.batch}批次没有课表`);
  211. continue;
  212. }
  213. for (const c of b.class) {
  214. const { lessons } = c;
  215. if (lessons) {
  216. if (lessons.length <= 0) c.lessons = ltemplate;
  217. }
  218. }
  219. }
  220. }
  221. return termnum;
  222. }
  223. // 自动生成班级私有方法
  224. async autoclassNew(res) {
  225. // 删除所有计划下的班级
  226. await this.clamodel.deleteMany({ planid: res.id });
  227. // 循环出所有班级进行添加操作
  228. for (const term of res.termnum) {
  229. for (const batch of term.batchnum) {
  230. const classs = await batch.class;
  231. for (const cla of classs) {
  232. const newdata = {
  233. name: cla.name,
  234. number: cla.number,
  235. batchid: batch.id,
  236. termid: term.id,
  237. planid: res.id,
  238. type: cla.type,
  239. headteacherid: cla.headteacherid,
  240. };
  241. await this.clamodel.create(newdata);
  242. }
  243. }
  244. }
  245. }
  246. // 自动生成班级私有方法
  247. async autoclass(res, trainplanold) {
  248. // 首先比较当前数据和原数据的值是否有不同
  249. // 保存后所有期id
  250. const tremid_res = _.map(res.termnum, 'id');
  251. // 保存前所有期id
  252. const tremid_old = _.map(trainplanold.termnum, 'id');
  253. // 取得要删除的期id,进行班级中删除已删除期的班级
  254. const deltrem = _.difference(tremid_old, tremid_res);
  255. // 循环删除已经删除期的所有班级
  256. for (const elm of deltrem) {
  257. await this.clamodel.deleteMany({ termid: elm });
  258. }
  259. // 取得所有新加期id
  260. const addtrem = _.difference(tremid_res, tremid_old);
  261. // 清空后循环取得所有期进行批次操作
  262. const terms = res.termnum;
  263. for (const el of terms) {
  264. // 判断是否新加期
  265. if (_.indexOf(addtrem, el.id) !== -1) {
  266. // 循环当前新加期的批次列表,根据批次id和班级数生成班级信息
  267. const batchnums = el.batchnum;
  268. for (const batchnum of batchnums) {
  269. // 取得当前批次的班级数
  270. const classnum = batchnum.class;
  271. for (const cla of classnum) {
  272. const newdata = {
  273. name: cla.name,
  274. number: cla.number,
  275. batchid: batchnum.id,
  276. termid: el.id,
  277. planid: res.id,
  278. type: cla.type,
  279. };
  280. await this.clamodel.create(newdata);
  281. }
  282. }
  283. } else {
  284. // 不是新加期,更新期信息
  285. // 保存后所有期id
  286. const batchid_res = _.map(el.batchnum, 'id');
  287. // 保存前所有期id
  288. const batchid_old = _.map(
  289. trainplanold.termnum.id(el.id).batchnum,
  290. 'id'
  291. );
  292. // 取得要删除的期id,进行班级中删除已删除期的班级
  293. const delbatchs = _.difference(batchid_old, batchid_res);
  294. // 循环删除已经删除期的所有班级
  295. for (const delba of delbatchs) {
  296. await this.clamodel.deleteMany({ termid: el.id, batchid: delba });
  297. }
  298. // 取得所有新加期id
  299. const addbatch = _.difference(batchid_res, batchid_old);
  300. const batchnums = el.batchnum;
  301. for (const batchnum of batchnums) {
  302. // 取得当前批次是否有删除
  303. // 判断是否新加期
  304. if (_.indexOf(addbatch, batchnum.id) !== -1) {
  305. // 取得当前批次的班级数
  306. const classnum = batchnum.class;
  307. for (const cla of classnum) {
  308. const newdata = {
  309. name: cla.name,
  310. number: cla.number,
  311. batchid: batchnum.id,
  312. termid: el.id,
  313. planid: res.id,
  314. type: cla.type,
  315. };
  316. await this.clamodel.create(newdata);
  317. }
  318. } else {
  319. if (
  320. batchnum.class ===
  321. trainplanold.termnum.id(el.id).batchnum.id(batchnum.id).class
  322. ) {
  323. // 编辑只会针对班级人数进行修改。
  324. const _class = await this.clamodel.find({
  325. termid: el.id,
  326. batchid: batchnum.id,
  327. });
  328. if (_class.length !== 0) {
  329. for (const ee of _class) {
  330. ee.number = batchnum.number;
  331. await ee.save();
  332. }
  333. } else {
  334. const classnum = batchnum.class;
  335. for (const cla of classnum) {
  336. const newdata = {
  337. name: cla.name,
  338. number: cla.number,
  339. batchid: batchnum.id,
  340. termid: el.id,
  341. planid: res.id,
  342. type: cla.type,
  343. };
  344. await this.clamodel.create(newdata);
  345. }
  346. }
  347. } else {
  348. // 当班级数有更改时
  349. // 删除所有班级 并重新生成班级
  350. await this.clamodel.deleteMany({
  351. termid: el.id,
  352. batchid: batchnum.id,
  353. });
  354. const classnum = batchnum.class;
  355. for (const cla of classnum) {
  356. const newdata = {
  357. name: cla.name,
  358. number: cla.number,
  359. batchid: batchnum.id,
  360. termid: el.id,
  361. planid: res.id,
  362. type: cla.type,
  363. };
  364. await this.clamodel.create(newdata);
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. // // 将分好的班级重新编排名字
  373. // async autoclassname(res) {
  374. // // 取得所有期id
  375. // const tremid_res = _.map(res.termnum, 'id');
  376. // for (const termid of tremid_res) {
  377. // const classs = await this.clamodel.find({ planid: res.id, termid });
  378. // let i = 0;
  379. // for (const cla of classs) {
  380. // i = i + 1;
  381. // cla.name = i;
  382. // await cla.save();
  383. // }
  384. // }
  385. // }
  386. async exportExcel({ trainplanIds }) {
  387. const nowDate = new Date().getTime();
  388. const path =
  389. 'D:\\wwwroot\\service\\service-file\\upload\\train\\' + nowDate + '.xlsx';
  390. const respath =
  391. 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
  392. const wb = {
  393. SheetNames: [],
  394. Sheets: {},
  395. };
  396. for (let i = 0; i < trainplanIds.length; i++) {
  397. // 批次期次都在这里面
  398. const trainplan = await this.model.findOne({ _id: trainplanIds[i] });
  399. // 这个计划下所有的学生
  400. const studentList = await this.stumodel.find({ planid: trainplanIds[i] });
  401. // 计划名称
  402. const trainplandName = trainplan.title;
  403. // 在计划中找到这个学生在哪期以及哪期下的哪批次
  404. for (const student of studentList) {
  405. student.isComming = utils.getIsNot(student.isComming);
  406. student.trainplandName = trainplandName;
  407. // 期次
  408. const term = trainplan.termnum.filter(term => {
  409. return term.id === student.termid;
  410. });
  411. if (term.length > 0) {
  412. student.termName = term[0].term;
  413. }
  414. // 批次
  415. if (term.length !== 0) {
  416. const batch = term[0].batchnum.filter(batch => {
  417. return batch.id === student.batchid;
  418. });
  419. if (batch.length > 0) {
  420. student.batchName = JSON.parse(JSON.stringify(batch[0])).name;
  421. }
  422. }
  423. student.is_fine = utils.getIsNot(student.is_fine);
  424. }
  425. const _headers = [
  426. { key: 'trainplandName', title: '计划标题' },
  427. { key: 'termName', title: '期次' },
  428. { key: 'batchName', title: '批次' },
  429. { key: 'school_name', title: '学校' },
  430. { key: 'faculty', title: '院系' },
  431. { key: 'major', title: '专业' },
  432. { key: 'name', title: '姓名' },
  433. { key: 'id_number', title: '身份证号' },
  434. { key: 'phone', title: '手机号' },
  435. { key: 'gender', title: '性别' },
  436. { key: 'nation', title: '民族' },
  437. { key: 'edua_level', title: '学历层次' },
  438. { key: 'edua_system', title: '学制' },
  439. { key: 'entry_year', title: '入学年份' },
  440. { key: 'finish_year', title: '毕业年份' },
  441. { key: 'school_job', title: '在校职务' },
  442. { key: 'qq', title: 'QQ号' },
  443. { key: 'email', title: '邮箱' },
  444. // { key: 'openid', title: '微信openid' },
  445. { key: 'family_place', title: '家庭位置' },
  446. { key: 'family_is_hard', title: '是否困难' },
  447. { key: 'have_grant', title: ' 是否获得过助学金' },
  448. // { key: 'job', title: '职务' },
  449. { key: 'bedroom', title: '寝室号' },
  450. { key: 'is_fine', title: '是否优秀' },
  451. { key: 'isComming', title: '是否签到' },
  452. { key: 'selfscore', title: '个人分' },
  453. { key: 'score', title: '总分' },
  454. ];
  455. // 需要打出的列表
  456. const _data = studentList;
  457. const headers = _headers
  458. .map(({ title }) => title)
  459. .map((v, i) =>
  460. Object.assign({}, { v, position: String.fromCharCode(65 + i) + 1 })
  461. )
  462. .reduce(
  463. (prev, next) =>
  464. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  465. {}
  466. );
  467. const data = _data
  468. .map((v, i) =>
  469. _headers.map(({ key }, j) =>
  470. Object.assign(
  471. {},
  472. { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) }
  473. )
  474. )
  475. )
  476. .reduce((prev, next) => prev.concat(next))
  477. .reduce(
  478. (prev, next) =>
  479. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  480. {}
  481. );
  482. // 合并 headers 和 data
  483. const output = Object.assign({}, headers, data);
  484. // 获取所有单元格的位置
  485. const outputPos = Object.keys(output);
  486. // 计算出范围
  487. const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
  488. // 构建 workbook 对象
  489. wb.SheetNames.push('sheet' + i);
  490. wb.Sheets['sheet' + i] = Object.assign({}, output, { '!ref': ref });
  491. }
  492. // 导出 Excel
  493. XLSX.writeFile(wb, path);
  494. return respath;
  495. }
  496. // 导出学校大表
  497. async exportSchool({ trainplanId }) {
  498. // 备注
  499. const remarks = [];
  500. // 期数
  501. let termCount = [];
  502. // 班级数
  503. const classCount = [];
  504. // 日期
  505. const studyTime = [];
  506. // 合并单元格坐标
  507. const colRows = [];
  508. // 列起始
  509. let colzb = 3;
  510. // 行起始
  511. const rowzb = 1;
  512. // const colRow = {
  513. // s: { c: 3, r: rowzb },
  514. // e: { c: 6, r: rowzb },
  515. // };
  516. // colRows.push(colRow);
  517. const shcoolList = [];
  518. // 计划表
  519. const trainplan = await this.model.findOne({ _id: trainplanId });
  520. // 学校报名表
  521. const schtime = await this.schmodel.find({ planid: trainplanId });
  522. // 期次
  523. const termnums = trainplan.termnum;
  524. // 学校学校数据
  525. // const schools = trainplan.school;
  526. const schools = await this.smodel.find({});
  527. // 组装学校数据
  528. for (let i = 0; i < schools.length; i++) {
  529. // 学校数据
  530. const shcool = [];
  531. // 序号
  532. shcool.push(i + 1);
  533. // 学校名
  534. shcool.push(schools[i].name);
  535. // 总人数
  536. shcool.push('');
  537. for (const termnum of termnums) {
  538. // 批次
  539. const batchnum = termnum.batchnum;
  540. // 期次所占的格(期占格)
  541. const qizhange = batchnum.length - 1;
  542. /**
  543. * 合并单元格元素(decode_range方法解析数据格式)
  544. {
  545. s: { //s start 开始
  546. c: 1,//cols 开始列
  547. r: 0 //rows 开始行
  548. },
  549. e: {//e end 结束
  550. c: 4,//cols 结束列
  551. r: 0 //rows 结束行
  552. }
  553. }
  554. */
  555. // 添加坐标
  556. const colRow = {
  557. s: { c: colzb, r: rowzb },
  558. e: { c: colzb + qizhange, r: rowzb },
  559. };
  560. // colzb为上一次终止,那么起始需+1,
  561. colzb = colzb + qizhange + 1;
  562. colRows.push(colRow);
  563. // 向其中加入空格,以备合并单元格使用
  564. const qi = [];
  565. qi.push(termnum.term);
  566. for (let index = 0; index < qizhange; index++) {
  567. qi.push('');
  568. }
  569. termCount = [ ...termCount, ...qi ];
  570. // 循环
  571. for (const batch of batchnum) {
  572. // 把班级数与日期放入数组中
  573. classCount.push(batch.class.length);
  574. let startDate = batch.startdate;
  575. startDate = startDate.substr(5, 2) + '.' + startDate.substr(8, 2);
  576. let endDate = batch.enddate;
  577. endDate = endDate.substr(5, 2) + '.' + endDate.substr(8, 2);
  578. studyTime.push(startDate + '-' + endDate);
  579. // 拿着batch的id去schtime表中的arrange中查remark,将结果存入remarks中即可完成备注数组
  580. let remark = '';
  581. for (const sch of schtime) {
  582. // 计划中学校的code=上报时的code
  583. if (schools[i].code === sch.schid) {
  584. for (const arrange of sch.arrange) {
  585. if (arrange.batchid === batch.id) {
  586. remark = arrange.remark;
  587. // 查到了退出即可因为是个数组
  588. // 总人数
  589. shcool.push(arrange.number);
  590. }
  591. }
  592. }
  593. }
  594. remarks.push(remark);
  595. }
  596. }
  597. shcoolList.push(shcool);
  598. }
  599. const wscols = [
  600. { wpx: 50 }, // 第一列宽度设置单位px
  601. ];
  602. let xuhao = [ XLSX.utils.decode_range('A1:A4') ];
  603. const xuexiao = [ XLSX.utils.decode_range('B1:B4') ];
  604. xuhao = [ ...xuhao, ...xuexiao, ...colRows ];
  605. const data = [];
  606. // 第一行
  607. const row0 = [ '序号', '学校名称', '备注' ].concat(remarks);
  608. data.push(row0);
  609. // 第二行
  610. const row1 = [ '', '', '期数' ].concat(termCount);
  611. data.push(row1);
  612. // 第三行
  613. const row2 = [ '', '', '班级数' ].concat(classCount);
  614. data.push(row2);
  615. // 第四行
  616. const row3 = [ '', '', '日期' ].concat(studyTime);
  617. data.push(row3);
  618. for (const shcoolL of shcoolList) {
  619. let count = 0;
  620. for (let i = 3; i < shcoolL.length; i++) {
  621. count += parseInt(shcoolL[i]);
  622. }
  623. // 计算出总人数,开始总认识默认的是'',这里赋值
  624. shcoolL[2] = count;
  625. data.push(shcoolL);
  626. }
  627. // ...以此类推即可
  628. /** 头部-行列信息*/
  629. const ws = XLSX.utils.aoa_to_sheet(data);
  630. // 构建 workbook 对象
  631. const nowDate = new Date().getTime();
  632. const path =
  633. 'D:\\wwwroot\\service\\service-file\\upload\\train\\' + nowDate + '.xlsx';
  634. const respath =
  635. 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
  636. // 导出
  637. const wb = XLSX.utils.book_new();
  638. XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
  639. ws['!cols'] = wscols;
  640. // xuhao.push(XLSX.utils.decode_range('B1:D1')) // 测试数据 仓库1模拟数据
  641. ws['!merges'] = xuhao;
  642. XLSX.writeFile(wb, path);
  643. return respath;
  644. }
  645. // 导出学校大表
  646. async exportPlan({ trainplanId }) {
  647. const wscols = [
  648. { wpx: 50 }, // 第一列宽度设置单位px
  649. ];
  650. const monthList = [];
  651. // 月份合并单元格
  652. const colzb = 0;
  653. let rowzb = 1;
  654. // 头部合并单元格
  655. const coltb = 0;
  656. let rowtb = 0;
  657. // 人数合并单元格
  658. const colrs = 0;
  659. let rowrs = 1;
  660. // 人数数量合并单元格
  661. const colrssl = 0;
  662. let rowrssl = 3;
  663. // 班级数合并单元格
  664. const colbjs = 0;
  665. let rowbjs = 1;
  666. // 班级数数量合并单元格
  667. const colbjssl = 0;
  668. let rowbjssl = 3;
  669. // 数据
  670. const data = [];
  671. let colRowBJSSL = {};
  672. // 这里是头部颜色
  673. const tatleCell = { v: '', s: { fill: { fgColor: { rgb: '191970' } } } };
  674. // 坐标
  675. const tatleCellstyle = 0;
  676. let tatleRowstyle = 0;
  677. const styleTatle = [];
  678. // 这里是月份颜色
  679. const monthCell = [
  680. { v: '一月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  681. { v: '二月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  682. { v: '三月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  683. { v: '四月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  684. { v: '五月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  685. { v: '六月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  686. { v: '七月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  687. { v: '八月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  688. { v: '九月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  689. { v: '十月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  690. { v: '十一月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  691. { v: '十二月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } },
  692. ];
  693. // 坐标
  694. const monthCellstyle = 0;
  695. let monthRowstyle = 1;
  696. const styleMonth = [];
  697. // 这里是假期颜色
  698. const festivalsCell = {
  699. v: '',
  700. s: { fill: { fgColor: { rgb: 'A2CD5A' } } },
  701. };
  702. // 坐标
  703. const festivalsCellstyle = 0;
  704. let festivalsRowstyle = 0;
  705. const stylefestivals = [];
  706. // 计划表
  707. const trainplan = await this.model.findOne({ _id: trainplanId });
  708. const termnum = trainplan.termnum;
  709. let classNum = 0;
  710. // 获取最大的班级数,也就是月份的高
  711. for (const term of termnum) {
  712. if (term.classnum > classNum) {
  713. classNum = parseInt(term.classnum);
  714. }
  715. }
  716. // 得到所有的节日日期的数组,然后循环时注意得到一个删除一个
  717. const festivals = trainplan.festivals;
  718. const festivalList = this.getfestivalList(festivals);
  719. const termnumList = this.gettermnumList(termnum);
  720. // 得到所有班级的数组,然后循环时注意得到一个删除一个
  721. // 循环12个月,得到12个月以及每个月的数据
  722. for (let index = 1; index < 13; index++) {
  723. // 这里增加表格头部下标
  724. const tatleCells = XLSX.utils.encode_cell({
  725. c: tatleCellstyle,
  726. r: tatleRowstyle,
  727. });
  728. tatleRowstyle = tatleRowstyle + classNum + 3;
  729. styleTatle.push(tatleCells);
  730. // 这里是月份颜色
  731. const monthCells = XLSX.utils.encode_cell({
  732. c: monthCellstyle,
  733. r: monthRowstyle,
  734. });
  735. monthRowstyle = monthRowstyle + classNum + 3;
  736. styleMonth.push(monthCells);
  737. for (let j = 0; j < festivalList.length; j++) {
  738. const festival = festivalList[j];
  739. // 如果月份相同时才会增加
  740. const yue = parseInt(festival.substr(5, 2));
  741. const lie = parseInt(festival.substr(8, 2));
  742. if (index === yue) {
  743. // 这里是假期颜色这一列列7行都是这个颜色
  744. for (let k = 0; k < classNum; k++) {
  745. const festivalsCells = XLSX.utils.encode_cell({
  746. // 列
  747. c: festivalsCellstyle + lie,
  748. r: festivalsRowstyle + k + 3,
  749. });
  750. stylefestivals.push(festivalsCells);
  751. }
  752. }
  753. }
  754. festivalsRowstyle = festivalsRowstyle + classNum + 3;
  755. // 添加月份坐标
  756. const colRow = {
  757. s: { c: colzb, r: rowzb },
  758. // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行
  759. e: { c: colzb, r: rowzb + classNum + 1 },
  760. };
  761. // rowzb为上一次终止,那么起始需+1,这里加3,代表头部+月份+星期,所以+3
  762. rowzb = rowzb + classNum + 3;
  763. monthList.push(colRow);
  764. // 添加头部坐标
  765. const colRowTB = {
  766. s: { c: coltb, r: rowtb },
  767. // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行
  768. e: { c: coltb + 33, r: rowtb },
  769. };
  770. // rowzb为上一次终止,那么起始需+1,
  771. rowtb = rowtb + classNum + 3;
  772. monthList.push(colRowTB);
  773. // 添加人数坐标
  774. const colRowRS = {
  775. s: { c: colrs + 32, r: rowrs },
  776. // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行
  777. e: { c: colrs + 32, r: rowrs + 1 },
  778. };
  779. // rowzb为上一次终止,那么起始需+1,
  780. rowrs = rowrs + classNum + 3;
  781. monthList.push(colRowRS);
  782. // 添加人数数量坐标
  783. const colRowRSSL = {
  784. s: { c: colrssl + 32, r: rowrssl },
  785. // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行
  786. e: { c: colrssl + 32, r: rowrssl + classNum - 1 },
  787. };
  788. // rowzb为上一次终止,那么起始需+1,
  789. rowrssl = rowrssl + classNum + 3;
  790. monthList.push(colRowRSSL);
  791. // 添加班级数坐标
  792. const colRowBJS = {
  793. s: { c: colbjs + 33, r: rowbjs },
  794. // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行
  795. e: { c: colbjs + 33, r: rowbjs + 1 },
  796. };
  797. // rowzb为上一次终止,那么起始需+1,
  798. rowbjs = rowbjs + classNum + 3;
  799. monthList.push(colRowBJS);
  800. // 添加班级数数量坐标
  801. colRowBJSSL = {
  802. s: { c: colbjssl + 33, r: rowbjssl },
  803. // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行
  804. e: { c: colbjssl + 33, r: rowbjssl + classNum - 1 },
  805. };
  806. // rowzb为上一次终止,那么起始需+1,
  807. rowbjssl = rowbjssl + classNum + 3;
  808. monthList.push(colRowBJSSL);
  809. const resDate = this.makeCalendar(trainplan.year, index);
  810. data.push([ '' ]);
  811. data.push(
  812. [[ this.getBigMonth(index) + '月' ]]
  813. .concat(resDate.dlist)
  814. .concat([ '人数' ].concat([ '班级数' ]))
  815. );
  816. data.push([ '' ].concat(resDate.tlist));
  817. // 加列数组
  818. for (let i = 0; i < classNum; i++) {
  819. data.push('');
  820. }
  821. }
  822. // ...以此类推即可
  823. /** 头部-行列信息*/
  824. const ws = XLSX.utils.aoa_to_sheet(data);
  825. // 构建 workbook 对象
  826. const nowDate = new Date().getTime();
  827. const path =
  828. 'D:\\wwwroot\\service\\service-file\\upload\\train\\' + nowDate + '.xlsx';
  829. const respath =
  830. 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
  831. // 导出
  832. const wb = XLSX.utils.book_new();
  833. XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
  834. ws['!cols'] = wscols;
  835. ws['!merges'] = monthList;
  836. // 头部赋值颜色需要计算出坐标
  837. for (const tatlezb of styleTatle) {
  838. ws[tatlezb] = tatleCell;
  839. }
  840. // 月份赋值颜色需要计算出坐标
  841. for (let index = 0; index < styleMonth.length; index++) {
  842. ws[styleMonth[index]] = monthCell[index];
  843. }
  844. // 假期赋值颜色需要计算出坐标
  845. for (const festivals of stylefestivals) {
  846. ws[festivals] = festivalsCell;
  847. }
  848. XLSXStyle.writeFile(wb, path);
  849. return respath;
  850. }
  851. // 获取批次日期列表
  852. gettermnumList(termnums) {
  853. const termnumList = [];
  854. for (const termnum of termnums) {
  855. termnum.term;
  856. termnum.classnum;
  857. for (const batchnum of termnum.batchnum) {
  858. batchnum.batch;
  859. batchnum.class.length;
  860. batchnum.startdate;
  861. batchnum.enddate;
  862. }
  863. }
  864. return termnumList;
  865. }
  866. // 获取节假日集合
  867. getfestivalList(festivals) {
  868. let dateList = [];
  869. for (let index = 0; index < festivals.length; index++) {
  870. dateList = [
  871. ...dateList,
  872. ...utils.begindateEnddateSum(
  873. festivals[index].begindate,
  874. festivals[index].finishdate
  875. ),
  876. ];
  877. }
  878. return dateList;
  879. }
  880. // 获取大月份传过来的值是以1月份开始的
  881. getBigMonth(index) {
  882. const monthBig = [
  883. '一',
  884. '二',
  885. '三',
  886. '四',
  887. '五',
  888. '六',
  889. '七',
  890. '八',
  891. '九',
  892. '十',
  893. '十一',
  894. '十二',
  895. ];
  896. return monthBig[index - 1];
  897. }
  898. // 获取这个月份的所有日期1~30号或者31或者28,或者29
  899. makeCalendar(year, month = 1, month0) {
  900. month0 = month;
  901. if (month * 1 < 10) month = '0' + month;
  902. // 获取这个月份的最大值
  903. const days = moment(year + '-' + month).daysInMonth();
  904. const dlist = this.getDayList(year, month, days, month0);
  905. while (dlist.dlist.length < 31) {
  906. dlist.dlist.push('');
  907. }
  908. return dlist;
  909. }
  910. // 获取这个月份的1-30号经过加工的
  911. getDayList(year, month, days, month0) {
  912. const dlist = [];
  913. const tlist = [];
  914. const all = {};
  915. for (let index = 0; index < days; index++) {
  916. dlist.push(
  917. month0 +
  918. '月' +
  919. moment(year + '-' + month)
  920. .add(index, 'days')
  921. .format('D') +
  922. '日'
  923. );
  924. let dayy = parseInt(index + 1);
  925. if (dayy * 1 < 10) dayy = '0' + dayy;
  926. tlist.push(this.getWeekDay(year + '-' + month + '-' + dayy));
  927. }
  928. all.dlist = dlist;
  929. all.tlist = tlist;
  930. return all;
  931. }
  932. // 获取星期几
  933. getWeekDay(datestr) {
  934. const weekday = moment(datestr).weekday();
  935. if (weekday || weekday === 0) {
  936. const arr = [ '日', '一', '二', '三', '四', '五', '六' ];
  937. return '星期' + arr[weekday];
  938. }
  939. return '';
  940. }
  941. // async updateclass({ trainplanid, classid, rightHeader }) {
  942. // assert(trainplanid && classid && rightHeader, '缺少参数项');
  943. async updateclass({ trainplanid, termid, batchid, classid, rightHeader }) {
  944. assert(
  945. trainplanid && termid && batchid && classid && rightHeader,
  946. '缺少参数项'
  947. );
  948. // 根据全年计划表id查出对应的全年计划详细信息
  949. const trainplan = await this.model.findById(trainplanid);
  950. if (!trainplan) {
  951. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  952. }
  953. const term = trainplan.termnum.id(termid);
  954. if (!term) {
  955. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '期信息不存在');
  956. }
  957. const batch = term.batchnum.id(batchid);
  958. if (!batch) {
  959. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '批次信息不存在');
  960. }
  961. const class_ = await batch.class.id(classid);
  962. if (class_) {
  963. class_.headteacherid = rightHeader;
  964. }
  965. const res = await trainplan.save();
  966. if (res) {
  967. const cla_ = await this.clamodel.findOne({
  968. termid,
  969. batchid,
  970. name: class_.name,
  971. });
  972. if (cla_) {
  973. cla_.headteacherid = rightHeader;
  974. await cla_.save();
  975. }
  976. }
  977. return res;
  978. }
  979. async updatereteacher({ trainplanid, termid, reteacher }) {
  980. assert(trainplanid && termid && reteacher, '缺少参数项');
  981. // 根据全年计划表id查出对应的全年计划详细信息
  982. const trainplan = await this.model.findById(trainplanid);
  983. if (!trainplan) {
  984. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  985. }
  986. const term = await trainplan.termnum.id(termid);
  987. if (term) {
  988. term.reteacher = reteacher;
  989. }
  990. return await trainplan.save();
  991. }
  992. }
  993. module.exports = TrainplanService;