notice_push.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, spid = '') {
  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. // const wheres = { pid };
  56. // if (uid !== 0) {
  57. // wheres.uid = uid;
  58. // }
  59. // let notice = await this.db.select(this.tableName, {
  60. // where: wheres,
  61. // orders: [['create_time', 'desc']],
  62. // limit: 30,
  63. // offset: 0
  64. // });
  65. const uidSql = uid ? ' AND a.`uid` = ' + uid : '';
  66. const spidSql = spid ? ' AND (a.`spid` = "' + spid + '" OR t.`spid` = "' + spid + '")': '';
  67. const sql = 'SELECT a.* FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`pid` = ?' + uidSql + spidSql + ' ORDER BY a.`create_time` DESC LIMIT 0,30';
  68. const params = [this.tableName, this.ctx.service.tender.tableName, pid];
  69. let notice = await this.db.query(sql, params);
  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. * 查询所有推送记录,不重复数据
  79. * @param {Integer} pid - 项目id
  80. * @param {Integer} uid - 查询人id
  81. */
  82. async getNoticeByDataCollect(pid, tenderIds = []) {
  83. const wheres = { pid };
  84. if (tenderIds.length > 0) {
  85. wheres.tid = tenderIds;
  86. }
  87. let notice = await this.db.select(this.tableName, {
  88. where: wheres,
  89. orders: [['create_time', 'desc']],
  90. limit: 40,
  91. offset: 0
  92. });
  93. notice = this._.uniqBy(notice, 'content');
  94. notice = notice.map(v => {
  95. const extra = JSON.parse(v.content);
  96. delete v.content;
  97. return { ...v, ...extra }
  98. });
  99. return notice
  100. }
  101. }
  102. return NoticePush
  103. }