notice.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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);
  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. // 获取所有人后发送信息
  52. const userList = await this.umodel.find({ uid: { $in: personList } });
  53. for (const user of userList) {
  54. // 1,判断有没有openid;2有openid的发送消息,添加记录
  55. if (!_.get(user, 'openid')) continue;
  56. const openid = _.get(user, 'openid');
  57. const remark = '感谢您的使用';
  58. const date = await this.ctx.service.util.updatedate();
  59. const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收';
  60. const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '&noticeid=' + res.id;
  61. this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl);
  62. const notified = _.get(res, 'notified', []);
  63. notified.push({ notifiedid: user._id, username: user.name });
  64. res.notified = notified;
  65. }
  66. res.save();
  67. // if (res) {
  68. // // 判断班级id是否为空
  69. // // 不为空时被通知人:当前期,班下的学生,教师,班主任,并使用type判断
  70. // const notified_ = await this.getnotified(termid, classid, type);
  71. // res.notified = notified_;
  72. // await res.save();
  73. // for (const elm of notified_) {
  74. // const user = await this.umodel.findOne({ uid: elm.notifiedid });
  75. // if (!user) {
  76. // continue;
  77. // }
  78. // if (user.openid) {
  79. // const openid = user.openid;
  80. // const remark = '感谢您的使用';
  81. // const date = await this.ctx.service.util.updatedate();
  82. // const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收';
  83. // const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '&noticeid=' + res.id;
  84. // this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl);
  85. // }
  86. // }
  87. // }
  88. }
  89. /**
  90. * 根据条件返回学生列表
  91. * @param {Object} condition 查询学生的条件
  92. */
  93. async getRangeStudent(condition) {
  94. const res = await this.stumodel.find(condition);
  95. return res;
  96. }
  97. /**
  98. * 根据条件返回教师列表
  99. * @param {Object} condition 查询教师的条件
  100. */
  101. async getRangeTeacher(condition) {
  102. // teamodel;lmodel
  103. const lessonList = await this.lmodel.find(condition);
  104. const teacherList = _.compact(_.uniq(lessonList.map(i => i.lessons).flat().map(i => i.teaid)));
  105. return teacherList;
  106. }
  107. /**
  108. * 根据条件返回班主任+礼仪教师列表
  109. * @param {Object} condition 查询班主任+礼仪教师条件
  110. */
  111. async getRangeClasses(condition) {
  112. const classesList = await this.clsmodel.find(condition);
  113. const resList = classesList.map(i => i.headteacherid).concat(classesList.map(i => i.lyteacherid));
  114. return resList;
  115. }
  116. // 取得要通知的人
  117. async getnotified(termid, classid, type) {
  118. const notified_ = [];
  119. if (classid) {
  120. if (type === '1') {
  121. const students = await this.stumodel.find({ classid });
  122. for (const stu of students) {
  123. const newdata = { notifiedid: stu.id, username: stu.name };
  124. notified_.push(newdata);
  125. }
  126. } else if (type === '2') {
  127. const lesson = await this.lmodel.findOne({ classid });
  128. for (const elm of lesson.lessons) {
  129. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  130. notified_.push(newdata);
  131. }
  132. } else if (type === '3') {
  133. const class_ = await this.clsmodel.findById(classid);
  134. if (class_) {
  135. const htea = await this.heamodel.findById(class_.headteacherid);
  136. if (htea) {
  137. const newdata = { notifiedid: htea.id, username: htea.name };
  138. notified_.push(newdata);
  139. }
  140. }
  141. } else if (type === '0') {
  142. const students = await this.stumodel.find({ classid });
  143. for (const stu of students) {
  144. const newdata = { notifiedid: stu.id, username: stu.name };
  145. notified_.push(newdata);
  146. }
  147. const lesson = await this.lmodel.findOne({ classid });
  148. for (const elm of lesson.lessons) {
  149. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  150. notified_.push(newdata);
  151. }
  152. const class_ = await this.clsmodel.findById(classid);
  153. if (class_) {
  154. const htea = await this.heamodel.findById(class_.headteacherid);
  155. if (htea) {
  156. const newdata = { notifiedid: htea.id, username: htea.name };
  157. notified_.push(newdata);
  158. }
  159. }
  160. }
  161. } else {
  162. // 没输入班级的时候
  163. if (type === '1') {
  164. const students = await this.stumodel.find({ termid });
  165. for (const stu of students) {
  166. const newdata = { notifiedid: stu.id, username: stu.name };
  167. notified_.push(newdata);
  168. }
  169. } else if (type === '2') {
  170. const lessons = await this.lmodel.find({ termid });
  171. for (const les of lessons) {
  172. for (const elm of les.lessons) {
  173. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  174. notified_.push(newdata);
  175. }
  176. }
  177. } else if (type === '3') {
  178. const class_ = await this.clsmodel.find({ termid });
  179. for (const cla of class_) {
  180. const htea = await this.heamodel.findById(cla.headteacherid);
  181. if (htea) {
  182. const newdata = { notifiedid: htea.id, username: htea.name };
  183. notified_.push(newdata);
  184. }
  185. }
  186. } else if (type === '0') {
  187. const students = await this.stumodel.find({ termid });
  188. for (const stu of students) {
  189. const newdata = { notifiedid: stu.id, username: stu.name };
  190. notified_.push(newdata);
  191. }
  192. const lessons = await this.lmodel.find({ termid });
  193. for (const les of lessons) {
  194. for (const elm of les.lessons) {
  195. const newdata = { notifiedid: elm.teaid, username: elm.teaname };
  196. notified_.push(newdata);
  197. }
  198. }
  199. const class_ = await this.clsmodel.find({ termid });
  200. for (const cla of class_) {
  201. const htea = await this.heamodel.findById(cla.headteacherid);
  202. if (htea) {
  203. const newdata = { notifiedid: htea.id, username: htea.name };
  204. notified_.push(newdata);
  205. }
  206. }
  207. }
  208. }
  209. return notified_;
  210. }
  211. async look(data) {
  212. const { noticeid, uid } = data;
  213. const notice = await this.model.findById(noticeid);
  214. if (notice) {
  215. for (const elm of notice.notified) {
  216. if (elm.notifiedid === uid) {
  217. elm.status = '1';
  218. elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  219. }
  220. }
  221. await notice.save();
  222. }
  223. }
  224. async query({ skip, limit, ...info }) {
  225. const total = await this.model.count(info);
  226. const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  227. const res = [];
  228. for (const _notice of notices) {
  229. const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
  230. const { noticeid, content, notified, meta } = notice;
  231. const elm = [];
  232. for (const notified of notice.notified) {
  233. const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
  234. const userinfo = await this.findUserInfo(_notified.notifiedid);
  235. elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
  236. }
  237. res.push({ noticeid, content, meta, notified: elm });
  238. }
  239. return { data: res, total };
  240. }
  241. async findUserInfo(userid) {
  242. let userinfo;
  243. userinfo = await this.heamodel.findById(userid);
  244. if (!userinfo) {
  245. userinfo = await this.schmodel.findById(userid);
  246. } if (!userinfo) {
  247. userinfo = await this.teamodel.findById(userid);
  248. if (userinfo) {
  249. delete userinfo.status;
  250. }
  251. } if (!userinfo) {
  252. userinfo = await this.stumodel.findById(userid);
  253. }
  254. return userinfo;
  255. }
  256. }
  257. module.exports = NoticeService;