123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- '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(noticeid, '通知人id为必填项');
- assert(content, '通知内容为必填项');
- const res = await this.model.create(data);
- 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);
- }
- }
- }
- }
- // 取得要通知的人
- 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;
|