notice.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const sd = require('silly-datetime');
  8. class NoticeService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'notice');
  11. this.model = this.ctx.model.Notice;
  12. this.umodel = this.ctx.model.User;
  13. this.stumodel = this.ctx.model.Student;
  14. this.schmodel = this.ctx.model.School;
  15. this.heamodel = this.ctx.model.Headteacher;
  16. this.teamodel = this.ctx.model.Teacher;
  17. }
  18. async create(data) {
  19. const { planyearid, planid, termid, classid, noticeid, content, notified } = data;
  20. assert(planyearid, '年度计划id为必填项');
  21. assert(planid, '计划id为必填项');
  22. assert(noticeid, '通知人id为必填项');
  23. assert(content, '通知内容为必填项');
  24. const res = await this.model.create(data);
  25. if (res) {
  26. for (const elm of res.notified) {
  27. const user = await this.umodel.findOne({ uid: elm.notifiedid });
  28. if (!user) {
  29. continue;
  30. }
  31. if (user.openid) {
  32. const openid = user.openid;
  33. const remark = '感谢您的使用';
  34. const date = await this.ctx.service.util.updatedate();
  35. const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收';
  36. const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '&noticeid=' + res.id;
  37. this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl);
  38. }
  39. }
  40. }
  41. }
  42. async look(data) {
  43. const { noticeid, uid } = data;
  44. const notice = await this.model.findById(noticeid);
  45. if (notice) {
  46. for (const elm of notice.notified) {
  47. if (elm.notifiedid === uid) {
  48. elm.status = '1';
  49. elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  50. }
  51. }
  52. await notice.save();
  53. }
  54. }
  55. async query({ skip, limit, ...info }) {
  56. const total = await this.model.count(info);
  57. const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  58. const res = [];
  59. for (const _notice of notices) {
  60. const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
  61. const { noticeid, content, notified, meta } = notice;
  62. const elm = [];
  63. for (const notified of notice.notified) {
  64. const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
  65. const userinfo = await this.findUserInfo(_notified.notifiedid);
  66. elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
  67. }
  68. res.push({ noticeid, content, meta, notified: elm });
  69. }
  70. return { data: res, total };
  71. }
  72. async findUserInfo(userid) {
  73. let userinfo;
  74. userinfo = await this.heamodel.findById(userid);
  75. if (!userinfo) {
  76. userinfo = await this.schmodel.findById(userid);
  77. } if (!userinfo) {
  78. userinfo = await this.teamodel.findById(userid);
  79. if (userinfo) {
  80. delete userinfo.status;
  81. }
  82. } if (!userinfo) {
  83. userinfo = await this.stumodel.findById(userid);
  84. }
  85. return userinfo;
  86. }
  87. }
  88. module.exports = NoticeService;