trainplan.js 33 KB

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