'use strict'; const assert = require('assert'); const { after } = require('lodash'); const _ = require('lodash'); const moment = require('moment'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class ApplyService extends CrudService { constructor(ctx) { super(ctx, 'apply'); this.model = this.ctx.model.Apply; this.tmodel = this.ctx.model.Teacher; this.submodel = this.ctx.model.Subject; this.trainmodel = this.ctx.model.Trainplan; this.umodel = this.ctx.model.User; this.nmodel = this.ctx.model.Notice; this.dayList = [ '日', '一', '二', '三', '四', '五', '六' ]; } // 查询 async queryteacher(query) { const { termid, subid, date } = query; const data = await this.model .find({ termid, subid, date }) .sort({ msscore: -1 }); const teachers = []; for (const _data of data) { const teacherid = _data.teacherid; const teacher = await this.tmodel.findById(teacherid); teachers.push(teacher); } return teachers; } /** * 教师计划初步课表安排,可反复使用 * @param {Object} body planid:计划id,ids:期数id列表;classtype:班级类型 */ async arrangeteacher({ planid, ids, classtype }) { assert(planid, '缺少计划信息'); const trainplan = await this.trainmodel.findById(planid); if (!trainplan) { throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在'); } // trainplan = JSON.parse(JSON.stringify(trainplan)); // 查找所有教师列表 let teacherList = await this.tmodel.find({ xsscore: { $exists: true } }); teacherList = JSON.parse(JSON.stringify(teacherList)); // 查找所有教师上报列表(今年) const year = moment().format('YYYY'); const start = `${year}-01-01`; const end = `${year}-12-31`; let teaplanList = await this.model.find({ date: { $gte: start, $lte: end } }); teaplanList = JSON.parse(JSON.stringify(teaplanList)); // 课程 let subjectList = await this.submodel.find(); subjectList = JSON.parse(JSON.stringify(subjectList)); const termList = _.cloneDeep(trainplan); let { termnum } = termList; if (!termnum) return; termnum = JSON.parse(JSON.stringify(termnum)); // 整理出课表 const arr = this.setLessonList(termnum); // 安排后的课表 const afterList = []; // 教师排课计数对象 key:teacherid;value:次数 let teacherCount = {}; // 循环拍平的课表 for (const l of arr) { const { termid, subid, day: date, status, type, classid } = l; // 检验是否在需要安排的期内 if (!ids.includes(termid)) { // 不在要求安排的期内,教师计数后就直接放回去 teacherCount = this.countTeacher(l, teacherCount); afterList.push(l); continue; } // 检查是否符合要求的班级类型 if (classtype || `${classtype}` === '0') { // 不符合,教师计数,放回去,下一个 if (`${classtype}` !== `${type}`) { teacherCount = this.countTeacher(l, teacherCount); afterList.push(l); continue; } } // 检查是否确定 if (status && `${status}` === '1') { // 确定了,教师计数,放回去 teacherCount = this.countTeacher(l, teacherCount); afterList.push(l); continue; } // 重置教师:为什么在这?如果不需要教师,那也得重置;需要教师,下面重排,也重置,所以在这! l.teaid = null; l.teaname = null; // 查看这科要不要教师 const subject = subjectList.find(f => ObjectId(subid).equals(f._id)); if (subject.need_teacher !== '0') { afterList.push(l); continue; } // 到这了,就说明差不多能排了 // 整份上报教师列表 let applyList = _.cloneDeep(teaplanList); // 申请该天,该科目的教师,并查出教师的名字,分数; applyList = applyList.filter(f => f.date === date && f.subid === subid); applyList = applyList.map(i => { let obj = { ...JSON.parse(JSON.stringify(i)) }; const r = teacherList.find(f => i.teacherid === f._id); if (r) { const { name: teaname, xsscore: score } = r; const nscore = _.isNaN(parseFloat(score)) ? 0 : parseFloat(score); i.teaname = teaname; i.score = nscore; obj = { ...obj, teaname, score: nscore }; obj = _.omit(obj, [ 'meta' ]); } return obj; }); // 过滤出没有分数的,不排 applyList = applyList.filter(f => f.score && f.score !== 0); // 21-04-26添加:找到教师已经排列的次数,也加上 for (const apply of applyList) apply.count = _.get(teacherCount, apply.teacherid, 0); // 按已经安排的次数-升序;成绩-降序 排序 applyList = _.orderBy(applyList, [ 'count', 'score' ], [ 'asc', 'desc' ]); // 教师准备完了,之后就该排了 let reserve;// 最优备选解 // 条件:1(强制),同一天,一个教师只能有一次,因为一上一天课 for (const atea of applyList) { // 条件1处理: // tr为该教师在该天(现在安排的这天)有课:数据为2种:Object/undefined:object表示有课了,反之没有课 const tr = afterList.find(f => f.teaid === atea.teacherid && f.day === atea.date); if (tr) continue; // TODO:条件2(非强制)教师尽量不教同一个班. // 不强制的原因是:因为只按这个条件走,真的可能会出现空缺,但是还是有教师报了. // 处理:将该教师放入备选,继续向下找,如果找到了,说明那个教师就是最优解;如果没有找到,说明,备选教师仍是最优解 // 找该教师在该班是否有课(遵循先排不动原则) const already_teach = afterList.find(f => f.teaid === atea.teacherid && f.classid === classid); if (already_teach) { // 说明这个教师已经教过这个班了,再看下最优解有没有 if (reserve) { // 已经有最优解了,直接下个教师 continue; } else { // 还没有最优解,这个就是最优解 reserve = atea; } } else { // 这个教师没有教过这个班,直接上 l.teaid = atea.teacherid; l.teaname = atea.teaname; break; } // 直接选取这个教师即可,因为已经是上报的教师中次数最少的了 } // 上面处理完了,该检查有没有教师了:2种情况,有教师;没有教师,有备选最优解; if (!l.teaid && reserve) { // 没有教师,但是有备选最优解,那就用备选最优解 l.teaid = reserve.teacherid; l.teaname = reserve.teaname; } // 教师计数 teacherCount = this.countTeacher(l, teacherCount); afterList.push(l); } const newTermnum = this.returnTermnum(afterList, termnum); // 下面代码是检查本次计划教师都排了多少次 // const keys = Object.keys(teacherCount); // for (const key of keys) { // const r = teacherList.find(f => f._id === key); // if (r) console.log(`${r.name}:${teacherCount[key]}`); // } // 保存至计划 trainplan.termnum = newTermnum; await trainplan.save(); } // 确认计划安排 async arrangeConfirm({ planid, ids, classtype }) { const trainplan = await this.trainmodel.findById(planid); if (!trainplan) { throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在'); } const plan = _.cloneDeep(trainplan); let { termnum } = plan; if (!termnum) return; termnum = JSON.parse(JSON.stringify(termnum)); // 过滤出确认的期,TODO:没有做通知 // termnum = termnum.filter(f => ); // 找到每个教师的位置,然后把状态(status)改成1=>已确认 for (const t of termnum) { if (!ids.includes(t._id)) continue; const { term } = t; if (!(t.batchnum && _.isArray(t.batchnum))) continue; for (const b of t.batchnum) { const { batch } = b; if (!(b.class && _.isArray(b.class))) continue; for (const c of b.class) { // 检查是否要求班级类型 if (classtype || `${classtype}` === '0') { // 获取这个班级的班级类型 const { type } = c; // 判断班级类型与要求的符不符合,不符合就跳过不改 if (`${type}` !== `${classtype}`) continue; } if (!(c.lessons && _.isArray(c.lessons))) continue; for (const l of c.lessons) { l.status = '1'; } } } } trainplan.termnum = termnum; await trainplan.save(); } /** * 给教师计数 * @param {Object} data 拍平的数据 * @param {Object} countObj 教师计数对象 */ countTeacher(data, countObj) { const { teaid, teaname } = data; if (teaid) { countObj[`${teaid}`] = _.add(_.get(countObj, `${teaid}`, 0), 1); } return countObj; } /** * 拍平了的课表=>termnum * @param {Array} list 拍平了的课表,详情参考页面的初步课表的数据 * @param {Array} termnum 原termnum */ returnTermnum(list, termnum) { let newTermnum = []; for (const l of list) { const { termid, batchid, classid, ...info } = l; const updata = _.pick(info, [ 'day', 'subid', 'subname', 'teaid', 'teaname', 'time', 'status', ]); newTermnum = termnum.map(t => { // 找到期 if (termid === t._id) { t.batchnum = t.batchnum.map(b => { if (batchid === b._id) { // 找到批次 b.class = b.class.map(c => { if (classid === c._id) { if (c.lessons) { // 说明有课程安排,找有没有重复的,没有就推进去,有就更改,subid查 const r = c.lessons.find(f => f.subid === updata.subid); if (r) { const rindex = c.lessons.findIndex( f => f.subid === updata.subid ); c.lessons[rindex] = updata; } else { c.lessons.push(updata); } } else { // 说明没有课程安排,放进去一条保存 c.lessons = [ updata ]; } } return c; }); } return b; }); } return t; }); } return newTermnum; } /** * 将课表拍平了,从多维=>一维 * @param {Array} termnum 计划的termnum */ setLessonList(termnum) { let arr = []; for (const t of termnum) { const { batchnum, term, _id: termid } = t; // 班级和课程一一匹 for (const b of batchnum) { const { class: classes, lessons, _id: batchid } = b; const claslesList = this.setList( term * 1, termid, batchid, classes, lessons ); arr.push(...claslesList); } } arr = _.orderBy(arr, [ 'term', 'day' ], [ 'asc', 'asc' ]); return arr; } /** * 将课表模板和班级整理成一维数组 * @param {String} term 期数 * @param {String} termid 期id * @param {String} batchid 批id * @param {Array} classes 班级列表 * @param {Array} lessonTemplate 课表模板 */ setList(term, termid, batchid, classes, lessonTemplate) { const arr = []; // 班级和课程匹配 for (const cla of classes) { let { lessons } = cla; if (!lessons) lessons = lessonTemplate; for (const i of lessons) { let nobj = {}; nobj.term = term; nobj.termid = termid; nobj.batchid = batchid; nobj.type = cla.type; const obj = _.omit(cla, [ 'lessons' ]); nobj.classid = _.clone(cla._id); nobj = _.assign(nobj, obj); nobj = _.assign(nobj, i); arr.push(nobj); } } return arr; } /** * 发送消息 * @param {Object} param planid:年度计划id,ids,发送的期列表;classtype:发送班级类型 undefined 都发,有的话就找指定班级类型发 */ async arrangeSendMsg({ planid, ids, classtype }) { const trainplan = await this.trainmodel.findById(planid); if (!trainplan) { throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在'); } // 大批次id,年度计划id const plan = _.cloneDeep(trainplan); let { termnum, planyearid } = plan; if (!termnum) return; termnum = JSON.parse(JSON.stringify(termnum)); // 整理出课表 let arr = this.setLessonList(termnum); // 过滤出需要发送的教师 arr = arr.filter(f => ids.find(id => f.termid === id) && f.teaid); // && f.status !== '1' // 整理出要发送的教师列表 let teaids = arr.map(i => i.teaid); teaids = _.uniq(teaids); // 找到教师信息 let teaList = await this.tmodel.find({ _id: teaids }); // 找到教师用户信息 let teauserList = await this.umodel.find({ uid: teaids }); if (teaList) teaList = JSON.parse(JSON.stringify(teaList)); if (teauserList) teauserList = JSON.parse(JSON.stringify(teauserList)); // 发送,此处是根据安排,给教师发.还有一种方案是根据教师,整理安排一起发送 // 查询是否发送过这期的通知 // 排序 arr = _.orderBy(arr, [ 'day' ], [ 'asc' ]); for (const l of arr) { // 教师id,期数,班级名,上课的日期,课程名 const { teaid, term, name, day, subname, termid, classid, type, status, } = l; // 已确认的教师不发信息 if (status === '1') continue; // 判断发送的班级类型 if (!(classtype && classtype === type)) continue; const tea = teaList.find(f => f._id === teaid); const teauser = teauserList.find(f => f.uid === teaid); // 文案 let msg = `${_.get(tea, 'name', '')}老师您好: 吉林省高等学校毕业生就业指导中心-双困生培训系统提醒您: ${term}期-${name.includes('班') ? name : `${name}班`} ${day}(星期${this.dayList[moment(day).days()]}) 有您的课程安排:${subname}`; msg = `${msg}\n 如果您无法进行授课,请及时联系中心负责人`; const { openid } = teauser; let tourl; let to_send = false; if (openid) { let notice = await this.nmodel.findOne({ planid, termid, classid, type: '6', }); // 找下是否发过信息 if (notice) { // 发过信息,找有没有这个教师 const { notified } = notice; if (_.isArray(notified)) { const has_notice = notified.find(f => f.notifiedid === teaid); if (has_notice) { const { status } = has_notice; if (status !== '1') to_send = true; } else { const obj = { notifiedid: teaid, username: _.get(tea, 'name', ''), content: msg, }; notice.notified.push(obj); await notice.save(); to_send = true; } } } else { const notified = [ { notifiedid: teaid, username: _.get(tea, 'name', ''), content: msg, }, ]; const noticeObj = { planyearid, planid, termid, classid, noticeid: 'system', type: '6', content: `${term}期-${ name.includes('班') ? name : `${name}班` }教师计划初步信息确认`, notified, }; await this.nmodel.create(noticeObj); notice = await this.nmodel.findOne({ planid, termid, classid, type: '6', }); to_send = true; } tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + teaid + '¬iceid=' + notice._id; } if (to_send) { // 邮箱与微信都发送 const { email } = tea; if (email) { this.toSendEmail(email, msg, tea.name); } if (openid) { this.toSendWxMsg(openid, msg, tea.name, tourl); } } } } /** * 计划-教师初步课表发送邮件 * @param {String} email 邮件 * @param {String} content 内容 * @param {String} teaname 教师姓名 */ async toSendEmail(email, content, teaname) { if (!email) { console.error(`计划教师发送通知:${teaname}没有email`); return; } const subject = '吉林省高等学校毕业生就业指导中心通知(系统邮件,请勿回复)'; // this.ctx.service.util.sendMail(email, subject, content); } /** * 计划-教师初步课表发送微信推送 * @param {String} openid 微信公众号的openid * @param {String} content 内容 * @param {String} teaname 教师姓名 * @param {String} tourl 确认地址 */ async toSendWxMsg(openid, content, teaname, tourl) { if (!openid) { console.error(`计划教师发送微信推送:${teaname}没有openid`); return; } // TODO or notTODO 发送微信推送记录 await this.ctx.service.weixin.sendTemplateDesign( this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', '您有新的安排', content, '感谢您的使用', tourl ); } async repealConfirm({ planid, ids }) { // 将指定计划,期数的教师状态解除确认 let trainPlan = await this.ctx.model.Trainplan.findById(planid); trainPlan = JSON.parse(JSON.stringify(trainPlan)); let terms = {}; for (const term of trainPlan.termnum) { const in_ids = ids.find(f => ObjectId(f).equals(term._id)); if (in_ids) { for (const batch of term.batchnum) { for (const c of batch.class) { for (const l of c.lessons) { l.status = '0'; } } } terms = term; } } // const i = trainPlan.termnum.findIndex(f => ObjectId('5f5aed5e69b4221aedaa5005').equals(f._id)); // trainPlan.termnum[i] = terms; delete trainPlan.meta; const r = await this.ctx.model.Trainplan.update( { _id: ObjectId('5f5adb337ceb003386c9b0d4') }, { ...trainPlan } ); } } module.exports = ApplyService;