trainplan.js 33 KB

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