trainplan.js 30 KB

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