noticeService.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const Service = require('../service/baseService');
  3. class NoticeService extends Service {
  4. tag() {
  5. return this.ctx.model.NoticeModel;
  6. }
  7. async listForApplets(data, pop) {
  8. const { model } = this.ctx;
  9. return await model.NoticeModel.find(data, pop).sort({ createTime: -1 });
  10. }
  11. async listForAppletsFirst(data, pop) {
  12. const { model } = this.ctx;
  13. return await model.NoticeModel.find(data, pop).sort({ createTime: -1 }).skip(0)
  14. .limit(1);
  15. }
  16. // PC端发给我的通知
  17. async pcListForPage(data, user) {
  18. const { model } = this.ctx;
  19. const page = data.page;
  20. const level = user.dept.level;
  21. const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize;
  22. delete data.page;
  23. delete data.rows;
  24. const where = {};
  25. if (data._id) {
  26. where._id = data._id;
  27. }
  28. if (!data.searchtitle) {
  29. delete data.searchtitle;
  30. } else {
  31. where.title = { $regex: data.searchtitle };
  32. delete data.searchtitle;
  33. }
  34. if (level == 1) {
  35. where.$or = [{ dept1: user.dept1._id }];
  36. where.userid = { $ne: user._id };
  37. } else if (level == 2) {
  38. where.$or = [{ deptId: user.dept1._id }, { deptId: user.dept2._id }];
  39. where.userid = { $ne: user._id };
  40. } else if (level >= 3) {
  41. where.$or = [{ deptId: user.dept1._id }, { deptId: user.dept2._id }, { deptId: user.dept3._id }];
  42. where.userid = { $ne: user._id };
  43. }
  44. const pop = [
  45. {
  46. path: 'userid',
  47. select: 'loginName userName',
  48. // select: 'loginName',
  49. },
  50. ];
  51. const total = await model.NoticeModel.find(where).countDocuments();
  52. const result = await model.NoticeModel.find(where).populate(pop).skip((page - 1) * rows)
  53. .limit(rows)
  54. .sort({ createTime: -1 });
  55. return {
  56. count: total,
  57. list: result,
  58. };
  59. }
  60. }
  61. module.exports = NoticeService;