notice.js 7.6 KB

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