notice.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(termid, '期id为必填项');
  25. assert(noticeid, '通知人id为必填项');
  26. assert(content, '通知内容为必填项');
  27. // type:0=>所有人;1=>学生;2=>教师;3=>班主任 TODO:缺少个人发信息
  28. const res = await this.model.create({ ...data, type: '0' });
  29. let range = 'term';
  30. let personList = [];
  31. if (classid) range = 'class';
  32. if (range === 'term') {
  33. if (type === '0') {
  34. const sList = await this.getRangeStudent({ termid });
  35. const tList = await this.getRangeTeacher({ termid });
  36. const hList = await this.getRangeClasses({ termid });
  37. personList = [ ...sList, ...tList, ...hList ];
  38. } else if (type === '1') personList = await this.getRangeStudent({ termid });
  39. else if (type === '2')personList = await this.getRangeTeacher({ termid });
  40. else if (type === '3')personList = await this.getRangeClasses({ termid });
  41. } else if (range === 'class') {
  42. if (type === '0') {
  43. const sList = await this.getRangeStudent({ classid });
  44. const tList = await this.getRangeTeacher({ classid });
  45. const hList = await this.getRangeClasses({ _id: classid });
  46. personList = [ ...sList, ...tList, ...hList ];
  47. } else if (type === '1') personList = await this.getRangeStudent({ classid });
  48. else if (type === '2') personList = await this.getRangeTeacher({ classid });
  49. else if (type === '3') personList = await this.getRangeClasses({ id: classid });
  50. }
  51. personList = personList.map(i => i._id);
  52. // 获取所有人后发送信息
  53. const userList = await this.umodel.find({ uid: { $in: personList } });
  54. console.log(userList);
  55. for (const user of userList) {
  56. // 1,判断有没有openid;2有openid的发送消息,添加记录
  57. if (!_.get(user, 'openid')) continue;
  58. const openid = _.get(user, 'openid');
  59. const title = '您有一个新的通知';
  60. const detail = content;
  61. const tourl = this.ctx.app.config.baseUrl + '/trainnotice/?userid=' + user.uid + '&noticeid=' + res.id;
  62. await this.toSendWithUrl(title, openid, detail, tourl);
  63. const notified = _.get(res, 'notified', []);
  64. notified.push({ notifiedid: user.uid, username: user.name });
  65. res.notified = notified;
  66. }
  67. res.save();
  68. }
  69. /**
  70. * 根据条件返回学生列表
  71. * @param {Object} condition 查询学生的条件
  72. */
  73. async getRangeStudent(condition) {
  74. const res = await this.stumodel.find({ ...condition, openid: { $exists: true } });
  75. console.log(res);
  76. return res;
  77. }
  78. /**
  79. * 根据条件返回教师列表
  80. * @param {Object} condition 查询教师的条件
  81. */
  82. async getRangeTeacher(condition) {
  83. // teamodel;lmodel
  84. const lessonList = await this.lmodel.find(condition);
  85. const teacherList = _.compact(_.uniq(lessonList.map(i => i.lessons).flat().map(i => i.teaid)));
  86. const teaInfoList = await this.teamodel.find({ _id: { $in: teacherList } });
  87. return teaInfoList;
  88. }
  89. /**
  90. * 根据条件返回班主任+礼仪教师列表
  91. * @param {Object} condition 查询班主任+礼仪教师条件
  92. */
  93. async getRangeClasses({ _id, termid }) {
  94. let resList = [];
  95. if (_.id) {
  96. const cla = await this.clsmodel.findOne(_id);
  97. if (cla) resList.push(cla.headteacherid, cla.lyteacherid);
  98. } else {
  99. const classesList = await this.clsmodel.find(termid);
  100. if (classesList) {
  101. for (const cla of classesList) {
  102. resList.push(cla.headteacherid, cla.lyteacherid);
  103. }
  104. }
  105. }
  106. resList = _.compact(_.uniq(resList));
  107. const headteaList = await this.heamodel.find({ _id: { $in: resList } });
  108. resList = resList.filter(f => !headteaList.find(hf => ObjectId(f).equals(hf._id)));
  109. const teaList = await this.teamodel.find({ _id: { $in: resList } });
  110. resList = [ ...headteaList, ...teaList ];
  111. return resList;
  112. }
  113. async look(data) {
  114. const { noticeid, userid } = data;
  115. const notice = await this.model.findById(noticeid);
  116. if (notice) {
  117. for (const elm of notice.notified) {
  118. if (elm.notifiedid === userid) {
  119. elm.status = '1';
  120. elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  121. }
  122. }
  123. await notice.save();
  124. }
  125. }
  126. // async query({ skip, limit, ...info }) {
  127. // const total = await this.model.count(info);
  128. // const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  129. // const res = [];
  130. // for (const _notice of notices) {
  131. // const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
  132. // const { noticeid, content, notified, meta, _id } = notice;
  133. // const elm = [];
  134. // for (const notified of notice.notified) {
  135. // const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
  136. // const userinfo = await this.findUserInfo(_notified.notifiedid);
  137. // elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
  138. // }
  139. // res.push({ noticeid, content, meta, notified: elm, _id });
  140. // }
  141. // return { data: res, total };
  142. // }
  143. async findUserInfo(userid) {
  144. let userinfo;
  145. userinfo = await this.heamodel.findById(userid);
  146. if (!userinfo) {
  147. userinfo = await this.schmodel.findById(userid);
  148. } if (!userinfo) {
  149. userinfo = await this.teamodel.findById(userid);
  150. if (userinfo) {
  151. delete userinfo.status;
  152. }
  153. } if (!userinfo) {
  154. userinfo = await this.stumodel.findById(userid);
  155. }
  156. return userinfo;
  157. }
  158. async resend({ id }) {
  159. const res = await this.model.findById(id);
  160. if (res) {
  161. const { content, notified } = res;
  162. const useridList = notified.filter(f => f.status === '0').map(i => i.notifiedid);
  163. const userList = await this.umodel.find({ uid: { $in: useridList } });
  164. for (const user of userList) {
  165. if (!_.get(user, 'openid')) continue;
  166. const openid = _.get(user, 'openid');
  167. const detail = content;
  168. const tourl = this.ctx.app.config.baseUrl + '/trainnotice/?userid=' + user.uid + '&noticeid=' + res.id;
  169. await this.toSendWithUrl('您有信息需要确认', openid, detail, tourl);
  170. }
  171. } else {
  172. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  173. }
  174. }
  175. /**
  176. *发送自定义信息
  177. * @param {String} title 标题
  178. * @param {String} openid user表的openid
  179. * @param {String} detail 通知内容
  180. * @param {String} tourl 确认通知地址
  181. */
  182. async toSendWithUrl(title, openid, detail, tourl) {
  183. const remark = '感谢您的使用';
  184. const date = await this.ctx.service.util.updatedate();
  185. this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, `${title},请点击信息,确认您已收到信息!`, detail, date, remark, tourl);
  186. }
  187. }
  188. module.exports = NoticeService;