trainplan.js 30 KB

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