notice_push.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict'
  2. /**
  3. * 推送表模型
  4. *
  5. * @author LanJianRong
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. module.exports = app => {
  11. class NoticePush extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx)
  20. this.tableName = 'notice'
  21. }
  22. /**
  23. * 将未读记录设置成已读
  24. * @param {Number} id 推送记录id
  25. */
  26. async set(id) {
  27. await this.update({ is_read: 1 }, { id })
  28. }
  29. /**
  30. * 查询所有推送记录(取每个审批类型的前20条再拼接)
  31. * @param {Integer} pid - 项目id
  32. * @param {Integer} uid - 查询人id
  33. */
  34. async getNotice(pid, uid = 0) {
  35. const noticeList = [];
  36. for (const type in auditConst.pushType) {
  37. const wheres = { pid, type: auditConst.pushType[type] };
  38. if (uid !== 0) {
  39. wheres.uid = uid;
  40. }
  41. let notice = await this.db.select(this.tableName, {
  42. where: wheres,
  43. orders: [['create_time', 'desc']],
  44. limit: 20,
  45. offset: 0
  46. });
  47. notice = notice.map(v => {
  48. const extra = JSON.parse(v.content);
  49. delete v.content;
  50. return { ...v, ...extra };
  51. });
  52. noticeList.push(...notice);
  53. }
  54. return this._.orderBy(noticeList, ['create_time'], ['desc']);
  55. }
  56. /**
  57. * 查询所有推送记录,不重复数据
  58. * @param {Integer} pid - 项目id
  59. * @param {Integer} uid - 查询人id
  60. */
  61. async getNoticeByDataCollect(pid, tenderIds = []) {
  62. const wheres = { pid };
  63. let notice = await this.db.select(this.tableName, {
  64. where: wheres,
  65. orders: [['create_time', 'desc']],
  66. limit: 40,
  67. offset: 0
  68. });
  69. notice = this._.uniqBy(notice, 'content');
  70. notice = notice.map(v => {
  71. const extra = JSON.parse(v.content);
  72. delete v.content;
  73. return { ...v, ...extra }
  74. });
  75. return notice
  76. }
  77. }
  78. return NoticePush
  79. }