'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const sd = require('silly-datetime'); class NoticeService extends CrudService { constructor(ctx) { super(ctx, 'notice'); this.model = this.ctx.model.Notice; this.umodel = this.ctx.model.User; this.stumodel = this.ctx.model.Student; this.schmodel = this.ctx.model.School; this.heamodel = this.ctx.model.Headteacher; this.teamodel = this.ctx.model.Teacher; this.lmodel = this.ctx.model.Lesson; this.clsmodel = this.ctx.model.Class; } async create(data) { const { planyearid, planid, termid, classid, noticeid, content, notified, type } = data; assert(planyearid, '年度计划id为必填项'); assert(planid, '计划id为必填项'); assert(termid, '期id为必填项'); assert(noticeid, '通知人id为必填项'); assert(content, '通知内容为必填项'); // type:0=>所有人;1=>学生;2=>教师;3=>班主任 TODO:缺少个人发信息 const res = await this.model.create(data); let range = 'term'; let personList = []; if (classid) range = 'class'; if (range === 'term') { if (type === '0') { const sList = await this.getRangeStudent({ termid }); const tList = await this.getRangeTeacher({ termid }); const hList = await this.getRangeClasses({ termid }); personList = [ ...sList, ...tList, ...hList ]; } else if (type === '1') personList = await this.getRangeStudent({ termid }); else if (type === '2')personList = await this.getRangeTeacher({ termid }); else if (type === '3')personList = await this.getRangeClasses({ termid }); } else if (range === 'class') { if (type === '0') { const sList = await this.getRangeStudent({ classid }); const tList = await this.getRangeTeacher({ classid }); const hList = await this.getRangeClasses({ id: classid }); personList = [ ...sList, ...tList, ...hList ]; } else if (type === '1') personList = await this.getRangeStudent({ classid }); else if (type === '2') personList = await this.getRangeTeacher({ classid }); else if (type === '3') personList = await this.getRangeClasses({ id: classid }); } // 获取所有人后发送信息 const userList = await this.umodel.find({ uid: { $in: personList } }); for (const user of userList) { // 1,判断有没有openid;2有openid的发送消息,添加记录 if (!_.get(user, 'openid')) continue; const openid = _.get(user, 'openid'); const remark = '感谢您的使用'; const date = await this.ctx.service.util.updatedate(); const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收'; const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '¬iceid=' + res.id; this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl); const notified = _.get(res, 'notified', []); notified.push({ notifiedid: user._id, username: user.name }); res.notified = notified; } res.save(); // if (res) { // // 判断班级id是否为空 // // 不为空时被通知人:当前期,班下的学生,教师,班主任,并使用type判断 // const notified_ = await this.getnotified(termid, classid, type); // res.notified = notified_; // await res.save(); // for (const elm of notified_) { // const user = await this.umodel.findOne({ uid: elm.notifiedid }); // if (!user) { // continue; // } // if (user.openid) { // const openid = user.openid; // const remark = '感谢您的使用'; // const date = await this.ctx.service.util.updatedate(); // const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收'; // const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '¬iceid=' + res.id; // this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl); // } // } // } } /** * 根据条件返回学生列表 * @param {Object} condition 查询学生的条件 */ async getRangeStudent(condition) { const res = await this.stumodel.find(condition); return res; } /** * 根据条件返回教师列表 * @param {Object} condition 查询教师的条件 */ async getRangeTeacher(condition) { // teamodel;lmodel const lessonList = await this.lmodel.find(condition); const teacherList = _.compact(_.uniq(lessonList.map(i => i.lessons).flat().map(i => i.teaid))); return teacherList; } /** * 根据条件返回班主任+礼仪教师列表 * @param {Object} condition 查询班主任+礼仪教师条件 */ async getRangeClasses(condition) { const classesList = await this.clsmodel.find(condition); const resList = classesList.map(i => i.headteacherid).concat(classesList.map(i => i.lyteacherid)); return resList; } // 取得要通知的人 async getnotified(termid, classid, type) { const notified_ = []; if (classid) { if (type === '1') { const students = await this.stumodel.find({ classid }); for (const stu of students) { const newdata = { notifiedid: stu.id, username: stu.name }; notified_.push(newdata); } } else if (type === '2') { const lesson = await this.lmodel.findOne({ classid }); for (const elm of lesson.lessons) { const newdata = { notifiedid: elm.teaid, username: elm.teaname }; notified_.push(newdata); } } else if (type === '3') { const class_ = await this.clsmodel.findById(classid); if (class_) { const htea = await this.heamodel.findById(class_.headteacherid); if (htea) { const newdata = { notifiedid: htea.id, username: htea.name }; notified_.push(newdata); } } } else if (type === '0') { const students = await this.stumodel.find({ classid }); for (const stu of students) { const newdata = { notifiedid: stu.id, username: stu.name }; notified_.push(newdata); } const lesson = await this.lmodel.findOne({ classid }); for (const elm of lesson.lessons) { const newdata = { notifiedid: elm.teaid, username: elm.teaname }; notified_.push(newdata); } const class_ = await this.clsmodel.findById(classid); if (class_) { const htea = await this.heamodel.findById(class_.headteacherid); if (htea) { const newdata = { notifiedid: htea.id, username: htea.name }; notified_.push(newdata); } } } } else { // 没输入班级的时候 if (type === '1') { const students = await this.stumodel.find({ termid }); for (const stu of students) { const newdata = { notifiedid: stu.id, username: stu.name }; notified_.push(newdata); } } else if (type === '2') { const lessons = await this.lmodel.find({ termid }); for (const les of lessons) { for (const elm of les.lessons) { const newdata = { notifiedid: elm.teaid, username: elm.teaname }; notified_.push(newdata); } } } else if (type === '3') { const class_ = await this.clsmodel.find({ termid }); for (const cla of class_) { const htea = await this.heamodel.findById(cla.headteacherid); if (htea) { const newdata = { notifiedid: htea.id, username: htea.name }; notified_.push(newdata); } } } else if (type === '0') { const students = await this.stumodel.find({ termid }); for (const stu of students) { const newdata = { notifiedid: stu.id, username: stu.name }; notified_.push(newdata); } const lessons = await this.lmodel.find({ termid }); for (const les of lessons) { for (const elm of les.lessons) { const newdata = { notifiedid: elm.teaid, username: elm.teaname }; notified_.push(newdata); } } const class_ = await this.clsmodel.find({ termid }); for (const cla of class_) { const htea = await this.heamodel.findById(cla.headteacherid); if (htea) { const newdata = { notifiedid: htea.id, username: htea.name }; notified_.push(newdata); } } } } return notified_; } async look(data) { const { noticeid, uid } = data; const notice = await this.model.findById(noticeid); if (notice) { for (const elm of notice.notified) { if (elm.notifiedid === uid) { elm.status = '1'; elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'); } } await notice.save(); } } async query({ skip, limit, ...info }) { const total = await this.model.count(info); const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit)); const res = []; for (const _notice of notices) { const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice))); const { noticeid, content, notified, meta } = notice; const elm = []; for (const notified of notice.notified) { const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified))); const userinfo = await this.findUserInfo(_notified.notifiedid); elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified }); } res.push({ noticeid, content, meta, notified: elm }); } return { data: res, total }; } async findUserInfo(userid) { let userinfo; userinfo = await this.heamodel.findById(userid); if (!userinfo) { userinfo = await this.schmodel.findById(userid); } if (!userinfo) { userinfo = await this.teamodel.findById(userid); if (userinfo) { delete userinfo.status; } } if (!userinfo) { userinfo = await this.stumodel.findById(userid); } return userinfo; } } module.exports = NoticeService;