trainplan.js 27 KB

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