notice.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. this.lmodel = this.ctx.model.Lesson;
  18. this.clsmodel = this.ctx.model.Class;
  19. }
  20. async create(data) {
  21. const { planyearid, planid, termid, classid, noticeid, content, notified, type } = data;
  22. assert(planyearid, '年度计划id为必填项');
  23. assert(planid, '计划id为必填项');
  24. assert(noticeid, '通知人id为必填项');
  25. assert(content, '通知内容为必填项');
  26. const res = await this.model.create(data);
  27. if (res) {
  28. // 判断班级id是否为空
  29. // 不为空时被通知人:当前期,班下的学生,教师,班主任,并使用type判断
  30. const notified_ = await this.getnotified(termid, classid, type);
  31. res.notified = notified_;
  32. await res.save();
  33. for (const elm of notified_) {
  34. const user = await this.umodel.findOne({ uid: elm.notifiedid });
  35. if (!user) {
  36. continue;
  37. }
  38. if (user.openid) {
  39. const openid = user.openid;
  40. const remark = '感谢您的使用';
  41. const date = await this.ctx.service.util.updatedate();
  42. const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收';
  43. const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '&noticeid=' + res.id;
  44. this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl);
  45. }
  46. }
  47. }
  48. }
  49. // 取得要通知的人
  50. async getnotified(termid, classid, type) {
  51. const notified_ = [];
  52. if (classid) {
  53. if (type === '1') {
  54. const students = await this.stumodel.find({ classid });
  55. for (const stu of students) {
  56. const newdata = { notifiedid: stu.id, username: stu.name };
  57. notified_.push(newdata);
  58. }
  59. } else if (type === '2') {
  60. const lesson = await this.lmodel.findOne({ classid });
  61. for (const elm of lesson.lessons) {
  62. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  63. notified_.push(newdata);
  64. }
  65. } else if (type === '3') {
  66. const class_ = await this.clsmodel.findById(classid);
  67. if (class_) {
  68. const htea = await this.heamodel.findById(class_.headteacherid);
  69. if (htea) {
  70. const newdata = { notifiedid: htea.id, username: htea.name };
  71. notified_.push(newdata);
  72. }
  73. }
  74. } else if (type === '0') {
  75. const students = await this.stumodel.find({ classid });
  76. for (const stu of students) {
  77. const newdata = { notifiedid: stu.id, username: stu.name };
  78. notified_.push(newdata);
  79. }
  80. const lesson = await this.lmodel.findOne({ classid });
  81. for (const elm of lesson.lessons) {
  82. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  83. notified_.push(newdata);
  84. }
  85. const class_ = await this.clsmodel.findById(classid);
  86. if (class_) {
  87. const htea = await this.heamodel.findById(class_.headteacherid);
  88. if (htea) {
  89. const newdata = { notifiedid: htea.id, username: htea.name };
  90. notified_.push(newdata);
  91. }
  92. }
  93. }
  94. } else {
  95. // 没输入班级的时候
  96. if (type === '1') {
  97. const students = await this.stumodel.find({ termid });
  98. for (const stu of students) {
  99. const newdata = { notifiedid: stu.id, username: stu.name };
  100. notified_.push(newdata);
  101. }
  102. } else if (type === '2') {
  103. const lessons = await this.lmodel.find({ termid });
  104. for (const les of lessons) {
  105. for (const elm of les.lessons) {
  106. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  107. notified_.push(newdata);
  108. }
  109. }
  110. } else if (type === '3') {
  111. const class_ = await this.clsmodel.find({ termid });
  112. for (const cla of class_) {
  113. const htea = await this.heamodel.findById(cla.headteacherid);
  114. if (htea) {
  115. const newdata = { notifiedid: htea.id, username: htea.name };
  116. notified_.push(newdata);
  117. }
  118. }
  119. } else if (type === '0') {
  120. const students = await this.stumodel.find({ termid });
  121. for (const stu of students) {
  122. const newdata = { notifiedid: stu.id, username: stu.name };
  123. notified_.push(newdata);
  124. }
  125. const lessons = await this.lmodel.find({ termid });
  126. for (const les of lessons) {
  127. for (const elm of les.lessons) {
  128. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  129. notified_.push(newdata);
  130. }
  131. }
  132. const class_ = await this.clsmodel.find({ termid });
  133. for (const cla of class_) {
  134. const htea = await this.heamodel.findById(cla.headteacherid);
  135. if (htea) {
  136. const newdata = { notifiedid: htea.id, username: htea.name };
  137. notified_.push(newdata);
  138. }
  139. }
  140. }
  141. }
  142. return notified_;
  143. }
  144. async look(data) {
  145. const { noticeid, uid } = data;
  146. const notice = await this.model.findById(noticeid);
  147. if (notice) {
  148. for (const elm of notice.notified) {
  149. if (elm.notifiedid === uid) {
  150. elm.status = '1';
  151. elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  152. }
  153. }
  154. await notice.save();
  155. }
  156. }
  157. async query({ skip, limit, ...info }) {
  158. const total = await this.model.count(info);
  159. const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  160. const res = [];
  161. for (const _notice of notices) {
  162. const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
  163. const { noticeid, content, notified, meta } = notice;
  164. const elm = [];
  165. for (const notified of notice.notified) {
  166. const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
  167. const userinfo = await this.findUserInfo(_notified.notifiedid);
  168. elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
  169. }
  170. res.push({ noticeid, content, meta, notified: elm });
  171. }
  172. return { data: res, total };
  173. }
  174. async findUserInfo(userid) {
  175. let userinfo;
  176. userinfo = await this.heamodel.findById(userid);
  177. if (!userinfo) {
  178. userinfo = await this.schmodel.findById(userid);
  179. } if (!userinfo) {
  180. userinfo = await this.teamodel.findById(userid);
  181. if (userinfo) {
  182. delete userinfo.status;
  183. }
  184. } if (!userinfo) {
  185. userinfo = await this.stumodel.findById(userid);
  186. }
  187. return userinfo;
  188. }
  189. }
  190. module.exports = NoticeService;