'use strict'; const _ = require('lodash'); const { CrudService } = require('naf-framework-mongoose/lib/service'); const assert = require('assert'); const { BusinessError, ErrorCode } = require('naf-core').Error; const XLSX = require('xlsx'); const utils = require('../utils/utils.js'); const moment = require('moment'); const XLSXStyle = require('xlsx-style'); const Excel = require('exceljs'); const { sep } = require('path'); const { ObjectId } = require('mongoose').Types; class TrainplanService extends CrudService { constructor(ctx) { super(ctx, 'trainplan'); this.model = this.ctx.model.Trainplan; this.clamodel = this.ctx.model.Class; this.umodel = this.ctx.model.User; this.smodel = this.ctx.model.School; this.tmodel = this.ctx.model.Teacher; this.stumodel = this.ctx.model.Student; this.schmodel = this.ctx.model.Schtime; this.lmmodel = this.ctx.model.Lessonmode; this.ctmodel = this.ctx.model.Classtype; } async create(data) { const { planyearid, year, title } = data; assert(planyearid, '缺少大批次信息'); assert(year, '缺少年度'); assert(title, '缺少标题'); const res = await this.model.create(data); let planid = ''; if (res) planid = res._id; const schoolList = await this.smodel.find(); const schtimeArr = []; // 为学校创建不可以培训的数据 for (const sch of schoolList) { const { code } = sch; // 没有学校编码就下一个 if (!code) continue; const obj = { schid: code, year, planid }; let schtimeres; schtimeres = await this.schmodel.findOne(obj); if (!schtimeres) schtimeres = await this.schmodel.create(obj); if (schtimeres) schtimeArr.push(schtimeres); } // if (!schtimeArr.every((e) => e)) { // throw new BusinessError(ErrorCode.DATA_INVALID, '学校计划生成失败'); // } else return res; } async update({ id }, data) { const trainplan = await this.model.findById(id); // 保存原数据 const trainplanold = _.cloneDeep(trainplan); const { year, title, termnum, festivals, status, school } = data; if (year) { trainplan.year = year; } if (title) { trainplan.title = title; } if (termnum) { trainplan.termnum = termnum; } if (school) { trainplan.school = school; } if (festivals) { trainplan.festivals = festivals; } if (status === '1') { trainplan.status = status; } // 日历安排中添加课表信息,查询每种班级类型的课表,然后显示 if (trainplan.termnum) { // trainplan.termnum = trainplan.termnum = await this.termGetLesson(trainplan.termnum); // 重新查看每个班级是否有课程 trainplan.termnum = this.setClassLesson(trainplan.termnum); } // 如果培训计划状态改为发布,发送培训计划信息,并自动生成班级 const res = await trainplan.save(); if (res) { if (status === '1') { // 自动生成班级 // await this.autoclass(res, trainplanold); // await this.autoclassNew(res, trainplanold); // 将生成的班级重新将班级排班名 // await this.autoclassname(res); // 发送培训计划信息通知给相应人员 // 查询所有入库的教师 // 不需要给教师发信息 // const teachers = await this.tmodel.find({ status: '4' }); // for (const teacher of teachers) { // const teacherid = teacher._id; // const _teacher = await this.umodel.findOne({ // uid: teacherid, // type: '3', // }); // const openid = _teacher.openid; // const detail = trainplan.title + '已发布,请注意查收!'; // const date = await this.ctx.service.util.updatedate(); // const remark = '感谢您的使用'; // if (openid) { // this.ctx.service.weixin.sendTemplateMsg( // this.ctx.app.config.REVIEW_TEMPLATE_ID, // openid, // '您有一个新的通知', // detail, // date, // remark // ); // } // } // 查询所有学校用户 // const schools = await this.umodel.find({ type: '2' }); // for (const school of schools) { // const openid = school.openid; // const detail = trainplan.title + '已发布,请注意查收!'; // const date = await this.ctx.service.util.updatedate(); // const remark = '感谢您的使用'; // if (openid) { // this.ctx.service.weixin.sendTemplateMsg( // this.ctx.app.config.REVIEW_TEMPLATE_ID, // openid, // '您有一个新的通知', // detail, // date, // remark // ); // } // } } // 查哪个学校schtime表没有生成,就给生成了 const schoolList = await this.smodel.find(); for (const school of schoolList) { const r = await this.schmodel.findOne({ year: trainplan.year, planid: id, schid: school.code }); if (r) continue; // 学校没有编码直接跳过 if (!school.code) continue; const obj = { schid: school.code, year: trainplan.year, planid: id }; await this.schmodel.create(obj); } } return res; } async termGetLesson(termnum) { const lessonModelList = await this.lmmodel.find(); for (const term of termnum) { for (const batch of term.batchnum) { const { class: classes, startdate, enddate } = batch; // 获取每批次下每个班的班级类型 const typeList = _.uniq(classes.map((i) => i.type)); const h = _.head(typeList); if (!h) continue; const tem = lessonModelList.find((f) => f.type === h); if (!tem) continue; let { lessons } = tem; if (!lessons) continue; lessons = JSON.parse(lessons); // 过滤出上课的时间段 lessons = lessons.filter((f) => { const keys = Object.keys(f).filter((f) => f.includes('subid')); return keys.length > 0; }); // 记录上课的时间 const times = []; // 记录所有的科目 let subject = []; lessons.map((i) => { times.push(i.time); const keys = Object.keys(i); let arr = []; for (const key of keys) { if (key.match(/\d/g)) arr.push(_.head(key.match(/\d/g))); } arr = _.uniq(arr); for (const ai of arr) { if (i[`day${ai}subid`]) { subject.push({ subname: i[`day${ai}`], subid: i[`day${ai}subid`], day: ai, }); } } return i; }); // 去重 subject = _.uniqBy(subject, 'subid'); // 获得天列表 const dnum = moment(enddate).diff(moment(startdate), 'days') + 1; const dayList = []; for (let ind = 0; ind < dnum; ind++) { dayList.push(moment(startdate).add(ind, 'd').format('YYYY-MM-DD')); } // 将subject中的day换成日期 for (const sub of subject) { sub.day = dayList[sub.day * 1 - 1]; sub.time = times; } batch.lessons = subject; } } return termnum; } // 设置课表模板到班级上 setClassLesson(termnum) { // 为班级设置lessons,有的就不设置了 for (const t of termnum) { const { batchnum } = t; if (!batchnum || !_.isArray(batchnum)) { this.$message.error(`第${t.term}期的批次数据错误!`); continue; } for (const b of t.batchnum) { const { class: cla, lessons: ltemplate } = b; if (!cla) { console.warn(`${t.term}期${b.batch}批次没有班级列表`); continue; } if (!ltemplate) { console.warn(`${t.term}期${b.batch}批次没有课表`); continue; } for (const c of b.class) { const { lessons } = c; if (lessons) { if (lessons.length <= 0) c.lessons = ltemplate; } } } } return termnum; } // 自动生成班级私有方法 async autoclassNew(res) { // 删除所有计划下的班级 await this.clamodel.deleteMany({ planid: res.id }); // 循环出所有班级进行添加操作 for (const term of res.termnum) { for (const batch of term.batchnum) { const classs = await batch.class; for (const cla of classs) { const newdata = { name: cla.name, number: cla.number, batchid: batch.id, termid: term.id, planid: res.id, type: cla.type, headteacherid: cla.headteacherid, }; await this.clamodel.create(newdata); } } } } // 自动生成班级私有方法 async autoclass(res, trainplanold) { // 首先比较当前数据和原数据的值是否有不同 // 保存后所有期id const tremid_res = _.map(res.termnum, 'id'); // 保存前所有期id const tremid_old = _.map(trainplanold.termnum, 'id'); // 取得要删除的期id,进行班级中删除已删除期的班级 const deltrem = _.difference(tremid_old, tremid_res); // 循环删除已经删除期的所有班级 for (const elm of deltrem) { await this.clamodel.deleteMany({ termid: elm }); } // 取得所有新加期id const addtrem = _.difference(tremid_res, tremid_old); // 清空后循环取得所有期进行批次操作 const terms = res.termnum; for (const el of terms) { // 判断是否新加期 if (_.indexOf(addtrem, el.id) !== -1) { // 循环当前新加期的批次列表,根据批次id和班级数生成班级信息 const batchnums = el.batchnum; for (const batchnum of batchnums) { // 取得当前批次的班级数 const classnum = batchnum.class; for (const cla of classnum) { const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type, }; await this.clamodel.create(newdata); } } } else { // 不是新加期,更新期信息 // 保存后所有期id const batchid_res = _.map(el.batchnum, 'id'); // 保存前所有期id const batchid_old = _.map(trainplanold.termnum.id(el.id).batchnum, 'id'); // 取得要删除的期id,进行班级中删除已删除期的班级 const delbatchs = _.difference(batchid_old, batchid_res); // 循环删除已经删除期的所有班级 for (const delba of delbatchs) { await this.clamodel.deleteMany({ termid: el.id, batchid: delba }); } // 取得所有新加期id const addbatch = _.difference(batchid_res, batchid_old); const batchnums = el.batchnum; for (const batchnum of batchnums) { // 取得当前批次是否有删除 // 判断是否新加期 if (_.indexOf(addbatch, batchnum.id) !== -1) { // 取得当前批次的班级数 const classnum = batchnum.class; for (const cla of classnum) { const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type, }; await this.clamodel.create(newdata); } } else { if (batchnum.class === trainplanold.termnum.id(el.id).batchnum.id(batchnum.id).class) { // 编辑只会针对班级人数进行修改。 const _class = await this.clamodel.find({ termid: el.id, batchid: batchnum.id, }); if (_class.length !== 0) { for (const ee of _class) { ee.number = batchnum.number; await ee.save(); } } else { const classnum = batchnum.class; for (const cla of classnum) { const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type, }; await this.clamodel.create(newdata); } } } else { // 当班级数有更改时 // 删除所有班级 并重新生成班级 await this.clamodel.deleteMany({ termid: el.id, batchid: batchnum.id, }); const classnum = batchnum.class; for (const cla of classnum) { const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type, }; await this.clamodel.create(newdata); } } } } } } } // // 将分好的班级重新编排名字 // async autoclassname(res) { // // 取得所有期id // const tremid_res = _.map(res.termnum, 'id'); // for (const termid of tremid_res) { // const classs = await this.clamodel.find({ planid: res.id, termid }); // let i = 0; // for (const cla of classs) { // i = i + 1; // cla.name = i; // await cla.save(); // } // } // } async exportExcel({ trainplanIds }) { const nowDate = new Date().getTime(); const { repos_root_path: rp } = this.ctx.app.config.cdn; const { baseUrl: bu } = this.ctx.app.config; const path = `${rp}${sep}train${sep}${nowDate}.xlsx`; const respath = `${bu}/files/train/${nowDate}.xlsx`; const wb = { SheetNames: [], Sheets: {}, }; for (let i = 0; i < trainplanIds.length; i++) { // 批次期次都在这里面 const trainplan = await this.model.findOne({ _id: trainplanIds[i] }); // 这个计划下所有的学生 const studentList = await this.stumodel.find({ planid: trainplanIds[i] }); // 计划名称 const trainplandName = trainplan.title; // 在计划中找到这个学生在哪期以及哪期下的哪批次 for (const student of studentList) { student.isComming = utils.getIsNot(student.isComming); student.trainplandName = trainplandName; // 期次 const term = trainplan.termnum.filter((term) => { return term.id === student.termid; }); if (term.length > 0) { student.termName = term[0].term; } // 批次 if (term.length !== 0) { const batch = term[0].batchnum.filter((batch) => { return batch.id === student.batchid; }); if (batch.length > 0) { student.batchName = JSON.parse(JSON.stringify(batch[0])).name; } } student.is_fine = utils.getIsNot(student.is_fine); } const _headers = [ { key: 'trainplandName', title: '计划标题' }, { key: 'termName', title: '期次' }, { key: 'batchName', title: '批次' }, { key: 'school_name', title: '学校' }, { key: 'faculty', title: '院系' }, { key: 'major', title: '专业' }, { key: 'name', title: '姓名' }, { key: 'id_number', title: '身份证号' }, { key: 'phone', title: '手机号' }, { key: 'gender', title: '性别' }, { key: 'nation', title: '民族' }, { key: 'edua_level', title: '学历层次' }, { key: 'edua_system', title: '学制' }, { key: 'entry_year', title: '入学年份' }, { key: 'finish_year', title: '毕业年份' }, { key: 'school_job', title: '在校职务' }, { key: 'qq', title: 'QQ号' }, { key: 'email', title: '邮箱' }, // { key: 'openid', title: '微信openid' }, { key: 'family_place', title: '家庭位置' }, { key: 'family_is_hard', title: '是否困难' }, { key: 'have_grant', title: ' 是否获得过助学金' }, // { key: 'job', title: '职务' }, // { key: 'bedroom', title: '寝室号' }, { key: 'is_fine', title: '是否优秀' }, { key: 'isComming', title: '是否签到' }, { key: 'selfscore', title: '个人分' }, { key: 'score', title: '总分' }, ]; // 需要打出的列表 const _data = studentList; const headers = _headers .map(({ title }) => title) .map((v, i) => Object.assign({}, { v, position: String.fromCharCode(65 + i) + 1 })) .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {}); const data = _data .map((v, i) => _headers.map(({ key }, j) => Object.assign({}, { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) }))) .reduce((prev, next) => prev.concat(next)) .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {}); // 合并 headers 和 data const output = Object.assign({}, headers, data); // 获取所有单元格的位置 const outputPos = Object.keys(output); // 计算出范围 const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1]; // 构建 workbook 对象 wb.SheetNames.push('sheet' + i); wb.Sheets['sheet' + i] = Object.assign({}, output, { '!ref': ref }); } // 导出 Excel XLSX.writeFile(wb, path); return respath; } // 导出学校大表 async exportSchool({ trainplanId }) { const rows = [ ['学校名称', '期数'], [' ', '班级数'], [' ', '时间'], [' ', '备注'], ]; // 操作 const operaList = [{ startRow: 1, startCol: 1, endRow: 5, endCol: 1 }]; // 操作: // step1: 班级类型的动态列 // step2: 1行:期数需要合并列:需要合并的列数根据下面的批次决定 // step3: 2行:输出批次的班级类型及班级数 // step4: 3行:显示每批次的事件(开始-结束) // step5: 4行:每批次的备注,和5行同列合并 // 批次期次都在这里面 let trainplan = await this.model.findById(trainplanId); if (trainplan) trainplan = JSON.parse(JSON.stringify(trainplan)); const fn = trainplan.title; // 文件名 let classType = await this.ctmodel.find(); if (classType) classType = JSON.parse(JSON.stringify(classType)); let schtime = await this.schmodel.find({ planid: trainplanId }); if (schtime) schtime = JSON.parse(JSON.stringify(schtime)); // step1 // 整理班级类型的顺序 if (_.isArray(classType) && classType.length > 0) { classType = classType.map((i) => { i.sort = parseInt(i.code); return i; }); classType = _.orderBy(classType, ['sort'], ['desc']); } const row5 = ['']; for (const ct of classType) { const { name } = ct; row5.push(name); } row5.push('总人数', '实际分配总人数'); // 整理opera for (let i = 0; i < rows.length; i++) { // 结束列-2: -1=> 第二列,有个班级类型,与上面位于同列,需要减掉; -1=>row5初始有个'',占了学校的列,重复了 const opera = { startRow: i + 1, startCol: 2, endRow: i + 1, endCol: rows[i].length + row5.length - 2 }; operaList.push(opera); for (let k = 1; k < row5.length - 1; k++) { rows[i].push(''); } } rows.push(row5); // step2:处理计划 let { termnum, school } = trainplan; termnum = this.sortPlan(termnum); const batchs = this.getListForBatch(termnum); // 期数变的是行,不是列 const termRow = 1; // 因班级类型可能增加,所以此处是动态的 let col = rows[termRow - 1].length + 1; const classRow = 2; const timeRow = 3; const remarkRow = 4; for (const term of termnum) { const { term: termstr, batchnum } = term; // 处理期数据 rows[termRow - 1].push(termstr); const obj = { startRow: 1, startCol: col, endRow: 1, endCol: col + batchnum.length - 1 }; operaList.push(obj); // step3,4: for (const b of batchnum) { const { startdate, enddate } = b; const word = this.setClassColunmWord(b, classType); // 填充班级列数据 rows[classRow - 1].push(word); // 填充批次的时间 const timeword = `${moment(startdate).format('M.D')} - ${moment(enddate).format('M.D')}`; rows[timeRow - 1].push(timeword); // 数据,进行填充 rows[termRow - 1].push(''); // 随之更新列 col++; } // 因为期数需要占一个格子,所以得把在循环批次的时候加进去的-1 rows[termRow - 1].pop(); } // 整理注释 const { data, opera } = this.setRemark(batchs, schtime, rows[remarkRow - 1], remarkRow, 1); rows[remarkRow - 1] = data; operaList.push(...opera); // 整理数据 // ['学校名称','特殊班剩余人数','普通班剩余人数','总人数(预设)','实际分配总人数',...'对应批次的人数,没有就填充'] const schData = await this.setSchoolData(schtime, batchs, school, classType); rows.push(...schData); return await this.ctx.service.util.toExcel(rows, null, fn, operaList); } /** * 计划排序 * @param {Array} termnum 计划列表 */ sortPlan(termnum) { if (_.isArray(termnum) && termnum.length > 0) { // 数据排序 termnum = termnum.map((t) => { const { batchnum, term } = t; t.termnum = parseInt(term); t.batchnum = batchnum.map((b) => { const { batch, class: classes } = b; b.batchnum = _.isNumber(parseInt(batch)) ? parseInt(batch) : batch; b.class = classes.map((c) => { const { name } = c; c.classnum = _.isNumber(parseInt(name)) ? parseInt(name) : name; return c; }); b.class = _.orderBy(b.class, ['classnum'], ['asc']); return b; }); return t; }); termnum = _.orderBy(termnum, ['termnum'], ['asc']); } return termnum; } /** * 整理班级列的输出文字 * @param {Object} b batch,批次数据 * @param {Array} classType 班级类型列表 */ setClassColunmWord(b, classType) { const { class: classes } = b; const c = _.head(classes); let word = ''; if (c) { const { type } = c; const r = classType.find((f) => f.code === type); word = classes.length; if (r) word = `${word}(${r.name})`; } return word; } /** * 整理备注 * @param {Array} batchs 批次为元素的计划列表 * @param {Array} schtime 学校计划 * @param {Array} remarkRowData 备注行数据 * @param {Number} remarkRow 备注开始行 * @param {Number} mergeRow 备注要合并的行数 */ setRemark(batchs, schtime, remarkRowData, remarkRow, mergeRow) { let arr = []; for (const sp of schtime) { for (const arrange of sp.arrange) { if (_.get(arrange, 'remark')) arr.push(arrange); } } arr = _.uniqBy(arr, 'batchid'); // 整理完备注,按照顺序,依次去找,是否有备注,有就推进去备注,没有推空 const remarks = _.cloneDeep(remarkRowData); for (const b of batchs) { const r = arr.find((f) => ObjectId(f.batchid).equals(b._id)); if (r) remarks.push(r.remark); else remarks.push(''); } // 站位填充满 // 整理操作, 主要是行的合并 const opera = []; for (let i = remarkRowData.length; i < remarks.length; i++) { // i+1=>i为原数据的最后一个数据的位置,然而要处理的是后面的新添的数据 opera.push({ startRow: remarkRow, startCol: i + 1, endRow: remarkRow + mergeRow, endCol: i + 1 }); } return { data: remarks, opera }; } /** * 整理学校安排的数据 * @param {Array} schtime 学校安排的计划 * @param {Array} batchs 以批次为元素的计划列表 * @param {Array} schPlan 学校的预计划 * @param {Array} classType 班级类型 */ async setSchoolData(schtime, batchs, schPlan, classType) { let schoolList = await this.smodel.find(); const result = []; // 学校按code排序 if (schoolList && schoolList.length > 0) { schoolList = JSON.parse(JSON.stringify(schoolList)); schoolList = schoolList.map((i) => { i.schnum = this.toParseInt(i.code); return i; }); schoolList = _.orderBy(schoolList, ['schnum'], ['asc']); } // 实际安排的学校计划 const schArrange = []; for (const aobj of schtime) { const { schid, arrange } = aobj; if (!schid) continue; if (!_.isArray(arrange)) continue; for (const arrObj of arrange) { schArrange.push({ ...arrObj, schid }); } } // 计算各个班级类型剩余人数,总人数,实际分配总人数 for (const sch of schoolList) { const { name, code } = sch; // 该学校的数据 const arr = [name]; // 学校实际分配的计划, 先不管别的,先把计划的问题处理了 const reaFRes = schArrange.filter((f) => f.schid === code); // 过滤出来学校已经分配的数据后,需要知道是什么类型的 const reaObj = { total: 0, reaTotal: 0 }; // key:班级类型, value:为实际分配的人数 for (const rr of reaFRes) { const { batchid, number } = rr; const r = batchs.find((f) => f._id === batchid); if (r) { const { type } = r; reaObj[type] = (this.toParseInt(reaObj[type]) || 0) + this.toParseInt(number); // 计划人数累加 reaObj.reaTotal = _.add(this.toParseInt(reaObj.reaTotal), this.toParseInt(number)); // 实际人数累加 } } // 先查预计划有没有 const p = schPlan.find((f) => f.code === code); if (p) { // 获取到预设的学校计划 const { classnum } = p; // 班级类型对应的计划 for (const ct of classType) { const { code } = ct; const r = classnum.find((f) => f.code === code); // n为要求的人数 let n = 0; if (r) { // 找到对应类型的计划了,计算这个班级类型是否分配完毕 const { number } = r; if (number) n = this.toParseInt(number); } // 用code去reaObj中提取实际分配的人数,就可以算处的值 const rea = this.toParseInt(reaObj[code]) || 0; let word = _.subtract(n - rea); // n===0:表示原来计划里没有这个类型,word再为0,说明实际上也没有这个类型,就不需要显示了 if (n === 0 && word === 0) word = ''; reaObj.total = _.add(reaObj.total, n); // 计划人数累加 arr.push(word); } } else { // 如果没有这个学校的计划,就得看看reaObj中有没有分配,有分配还得处理;没分配就跳过下一个 const keys = Object.keys(reaObj); for (const ct of classType) { if (keys.length <= 0) { // 没分配,填充,下一个 arr.push(''); } else { // 没计划但是实际分配了,就需要体现出来了 const word = reaObj[ct.code]; if (word && word !== 0) { arr.push(`-${word}`); } else { arr.push(''); } } } } // 处理总人数和实际分配人数 arr.push(reaObj.total); arr.push(reaObj.reaTotal); // 循环学校列表,找到学校的所有安排,然后找到对应的索引,进行数据整理 for (const rea of reaFRes) { const index = batchs.findIndex((f) => f._id === rea.batchid); if (index < 0) continue; arr[index + 5] = rea.number; } result.push(arr); } // 合计行:只有学校列不计算,其他的列全部计算=>批次列(batchs.length)+班级类型列(classType.length)+总人数列(1)+实际分配人数列(1) const colTotal = batchs.length + classType.length + 1 + 1; const count = ['合计']; // 第一列不需要,是学校/合计列 for (let i = 1; i <= colTotal; i++) { const cmid = []; for (const arr of result) { let number = this.toParseInt(_.get(arr, i, 0)); if (_.isNaN(number)) number = 0; cmid.push(number); } const colCount = cmid.reduce((p, n) => p + n, 0); count.push(colCount); } result.push(count); return result; } toParseInt(number) { let num = parseInt(number); if (_.isNaN(num)) num = 0; return num; } /** * 获取批次列表(排完顺序了,直接拿出来) * @param {Array} termnum 计划列表 */ getListForBatch(termnum) { const arr = []; for (const t of termnum) { for (const b of t.batchnum) { const obj = { ...b }; const { class: classes } = b; if (_.isArray(classes)) { const head = _.head(classes); if (head) { const { type } = head; if (type) obj.type = type; } const all = classes.reduce((p, n) => p + (parseInt(n.number) || 0), 0); if (_.isNumber(all)) b.all = all; } arr.push(obj); } } return arr; } // 导出学校大表 async exportPlan({ trainplanId }) { const wscols = [ { wpx: 50 }, // 第一列宽度设置单位px ]; const monthList = []; // 月份合并单元格 const colzb = 0; let rowzb = 1; // 头部合并单元格 const coltb = 0; let rowtb = 0; // 人数合并单元格 const colrs = 0; let rowrs = 1; // 人数数量合并单元格 const colrssl = 0; let rowrssl = 3; // 班级数合并单元格 const colbjs = 0; let rowbjs = 1; // 班级数数量合并单元格 const colbjssl = 0; let rowbjssl = 3; // 数据 const data = []; let colRowBJSSL = {}; // 这里是头部颜色 const tatleCell = { v: '', s: { fill: { fgColor: { rgb: '191970' } } } }; // 坐标 const tatleCellstyle = 0; let tatleRowstyle = 0; const styleTatle = []; // 这里是月份颜色 const monthCell = [ { v: '一月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '二月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '三月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '四月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '五月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '六月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '七月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '八月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '九月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '十月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '十一月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, { v: '十二月', s: { fill: { fgColor: { rgb: 'B0E2FF' } } } }, ]; // 坐标 const monthCellstyle = 0; let monthRowstyle = 1; const styleMonth = []; // 这里是假期颜色 const festivalsCell = { v: '', s: { fill: { fgColor: { rgb: 'A2CD5A' } } }, }; // 坐标 const festivalsCellstyle = 0; let festivalsRowstyle = 0; const stylefestivals = []; // 计划表 const trainplan = await this.model.findOne({ _id: trainplanId }); const termnum = trainplan.termnum; let classNum = 0; // 获取最大的班级数,也就是月份的高 for (const term of termnum) { if (term.classnum > classNum) { classNum = parseInt(term.classnum); } } // 得到所有的节日日期的数组,然后循环时注意得到一个删除一个 const festivals = trainplan.festivals; const festivalList = this.getfestivalList(festivals); const termnumList = this.gettermnumList(termnum); // 得到所有班级的数组,然后循环时注意得到一个删除一个 // 循环12个月,得到12个月以及每个月的数据 for (let index = 1; index < 13; index++) { // 这里增加表格头部下标 const tatleCells = XLSX.utils.encode_cell({ c: tatleCellstyle, r: tatleRowstyle, }); tatleRowstyle = tatleRowstyle + classNum + 3; styleTatle.push(tatleCells); // 这里是月份颜色 const monthCells = XLSX.utils.encode_cell({ c: monthCellstyle, r: monthRowstyle, }); monthRowstyle = monthRowstyle + classNum + 3; styleMonth.push(monthCells); for (let j = 0; j < festivalList.length; j++) { const festival = festivalList[j]; // 如果月份相同时才会增加 const yue = parseInt(festival.substr(5, 2)); const lie = parseInt(festival.substr(8, 2)); if (index === yue) { // 这里是假期颜色这一列列7行都是这个颜色 for (let k = 0; k < classNum; k++) { const festivalsCells = XLSX.utils.encode_cell({ // 列 c: festivalsCellstyle + lie, r: festivalsRowstyle + k + 3, }); stylefestivals.push(festivalsCells); } } } festivalsRowstyle = festivalsRowstyle + classNum + 3; // 添加月份坐标 const colRow = { s: { c: colzb, r: rowzb }, // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行 e: { c: colzb, r: rowzb + classNum + 1 }, }; // rowzb为上一次终止,那么起始需+1,这里加3,代表头部+月份+星期,所以+3 rowzb = rowzb + classNum + 3; monthList.push(colRow); // 添加头部坐标 const colRowTB = { s: { c: coltb, r: rowtb }, // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行 e: { c: coltb + 33, r: rowtb }, }; // rowzb为上一次终止,那么起始需+1, rowtb = rowtb + classNum + 3; monthList.push(colRowTB); // 添加人数坐标 const colRowRS = { s: { c: colrs + 32, r: rowrs }, // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行 e: { c: colrs + 32, r: rowrs + 1 }, }; // rowzb为上一次终止,那么起始需+1, rowrs = rowrs + classNum + 3; monthList.push(colRowRS); // 添加人数数量坐标 const colRowRSSL = { s: { c: colrssl + 32, r: rowrssl }, // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行 e: { c: colrssl + 32, r: rowrssl + classNum - 1 }, }; // rowzb为上一次终止,那么起始需+1, rowrssl = rowrssl + classNum + 3; monthList.push(colRowRSSL); // 添加班级数坐标 const colRowBJS = { s: { c: colbjs + 33, r: rowbjs }, // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行 e: { c: colbjs + 33, r: rowbjs + 1 }, }; // rowzb为上一次终止,那么起始需+1, rowbjs = rowbjs + classNum + 3; monthList.push(colRowBJS); // 添加班级数数量坐标 colRowBJSSL = { s: { c: colbjssl + 33, r: rowbjssl }, // 保证留下7个空行,如果需要在上面加值,直接在下面加入即可第几空行加入就行 e: { c: colbjssl + 33, r: rowbjssl + classNum - 1 }, }; // rowzb为上一次终止,那么起始需+1, rowbjssl = rowbjssl + classNum + 3; monthList.push(colRowBJSSL); const resDate = this.makeCalendar(trainplan.year, index); data.push(['']); data.push([[this.getBigMonth(index) + '月']].concat(resDate.dlist).concat(['人数'].concat(['班级数']))); data.push([''].concat(resDate.tlist)); // 加列数组 for (let i = 0; i < classNum; i++) { data.push(''); } } // ...以此类推即可 /** 头部-行列信息*/ const ws = XLSX.utils.aoa_to_sheet(data); // 构建 workbook 对象 const nowDate = new Date().getTime(); const path = 'D:\\wwwroot\\service\\service-file\\upload\\train\\' + nowDate + '.xlsx'; const respath = 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx'; // 导出 const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, 'Sheet1'); ws['!cols'] = wscols; ws['!merges'] = monthList; // 头部赋值颜色需要计算出坐标 for (const tatlezb of styleTatle) { ws[tatlezb] = tatleCell; } // 月份赋值颜色需要计算出坐标 for (let index = 0; index < styleMonth.length; index++) { ws[styleMonth[index]] = monthCell[index]; } // 假期赋值颜色需要计算出坐标 for (const festivals of stylefestivals) { ws[festivals] = festivalsCell; } XLSXStyle.writeFile(wb, path); return respath; } // 获取批次日期列表 gettermnumList(termnums) { const termnumList = []; for (const termnum of termnums) { termnum.term; termnum.classnum; for (const batchnum of termnum.batchnum) { batchnum.batch; batchnum.class.length; batchnum.startdate; batchnum.enddate; } } return termnumList; } // 获取节假日集合 getfestivalList(festivals) { let dateList = []; for (let index = 0; index < festivals.length; index++) { dateList = [...dateList, ...utils.begindateEnddateSum(festivals[index].begindate, festivals[index].finishdate)]; } return dateList; } // 获取大月份传过来的值是以1月份开始的 getBigMonth(index) { const monthBig = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']; return monthBig[index - 1]; } // 获取这个月份的所有日期1~30号或者31或者28,或者29 makeCalendar(year, month = 1, month0) { month0 = month; if (month * 1 < 10) month = '0' + month; // 获取这个月份的最大值 const days = moment(year + '-' + month).daysInMonth(); const dlist = this.getDayList(year, month, days, month0); while (dlist.dlist.length < 31) { dlist.dlist.push(''); } return dlist; } // 获取这个月份的1-30号经过加工的 getDayList(year, month, days, month0) { const dlist = []; const tlist = []; const all = {}; for (let index = 0; index < days; index++) { dlist.push( month0 + '月' + moment(year + '-' + month) .add(index, 'days') .format('D') + '日' ); let dayy = parseInt(index + 1); if (dayy * 1 < 10) dayy = '0' + dayy; tlist.push(this.getWeekDay(year + '-' + month + '-' + dayy)); } all.dlist = dlist; all.tlist = tlist; return all; } // 获取星期几 getWeekDay(datestr) { const weekday = moment(datestr).weekday(); if (weekday || weekday === 0) { const arr = ['日', '一', '二', '三', '四', '五', '六']; return '星期' + arr[weekday]; } return ''; } // async updateclass({ trainplanid, classid, rightHeader }) { // assert(trainplanid && classid && rightHeader, '缺少参数项'); async updateclass({ trainplanid, termid, batchid, classid, rightHeader }) { assert(trainplanid && termid && batchid && classid && rightHeader, '缺少参数项'); // 根据全年计划表id查出对应的全年计划详细信息 const trainplan = await this.model.findById(trainplanid); if (!trainplan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在'); } const term = trainplan.termnum.id(termid); if (!term) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '期信息不存在'); } const batch = term.batchnum.id(batchid); if (!batch) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '批次信息不存在'); } const class_ = await batch.class.id(classid); if (class_) { class_.headteacherid = rightHeader; } const res = await trainplan.save(); if (res) { const cla_ = await this.clamodel.findOne({ termid, batchid, name: class_.name, }); if (cla_) { cla_.headteacherid = rightHeader; await cla_.save(); } } return res; } async updatereteacher({ trainplanid, termid, placereteacher }) { assert(trainplanid && termid && placereteacher, '缺少参数项'); // 根据全年计划表id查出对应的全年计划详细信息 const trainplan = await this.model.findById(trainplanid); if (!trainplan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在'); } const termnum = trainplan.termnum.find((f) => ObjectId(termid).equals(f._id)); if (termnum) { termnum.placereteacher = placereteacher; } await this.model.updateOne({ _id: trainplanid }, trainplan); } } module.exports = TrainplanService;