msgCenterRecordService2.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class MsgCenterRecordService2 extends Service {
  4. async channel({ type, startTime, endTime }) {
  5. const { ctx } = this;
  6. const agg = [
  7. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  8. { $group: ctx.helper.getTimeGroup(type, { colorTotal: { $sum: '$colorTotal' },
  9. grayTotal: { $sum: '$grayTotal' },
  10. }) },
  11. ];
  12. return await ctx.model.Local.MsgCenterRecordModel.aggregateFix(agg);
  13. }
  14. async read({ type, startTime, endTime, msgType }) {
  15. const { ctx } = this;
  16. let cond = [];
  17. if (msgType) {
  18. cond = [{ $match: { 'msgType._id': msgType } }];
  19. }
  20. const agg = [
  21. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  22. { $unwind: '$msgType' },
  23. ...cond,
  24. { $group: ctx.helper.getTimeGroup(type, { readCount: { $sum: '$msgType.readCount' },
  25. total: { $sum: '$msgType.count' },
  26. }) },
  27. ];
  28. return await ctx.model.Local.MsgCenterRecordModel.aggregateFix(agg);
  29. }
  30. async over({ type, startTime, endTime }) {
  31. const { ctx } = this;
  32. const agg = [
  33. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  34. { $group: ctx.helper.getTimeGroup(type, { readTotal: { $sum: '$readTotal' },
  35. unReadTotal: { $sum: '$unReadTotal' },
  36. }) },
  37. ];
  38. return await ctx.model.Local.MsgCenterRecordModel.aggregateFix(agg);
  39. }
  40. // ====================清洗分割线=========================================================================================
  41. async statistics({ timeRangData, initData, isForceUpdate }) {
  42. const { ctx } = this;
  43. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.MsgCenterRecordModel,
  44. { ...initData });
  45. if (hasData && !isForceUpdate) {
  46. return;
  47. }
  48. initData.start_time = new Date();
  49. const cResult = await this.channelGroup(timeRangData);
  50. ctx.logger.info('任务进行channelGroup');
  51. const rResult = await this.readGroup(timeRangData);
  52. ctx.logger.info('任务进行readGroup');
  53. let colorTotal = 0;// 彩色数
  54. let grayTotal = 0;// 灰色数
  55. let readTotal = 0;// 已读数
  56. let unReadTotal = 0;// 未读数
  57. let total = 0;// 总数
  58. cResult.forEach(item => {
  59. switch (item._id) {
  60. case 1:
  61. grayTotal = item.count;
  62. break;
  63. case 2:
  64. colorTotal = item.count;
  65. break;
  66. default:
  67. break;
  68. }
  69. total += item.count;
  70. });
  71. rResult.forEach(item => {
  72. switch (item._id) {
  73. case 0:
  74. unReadTotal = item.count;
  75. break;
  76. case 1:
  77. readTotal = item.count;
  78. break;
  79. default:
  80. break;
  81. }
  82. });
  83. const msgType = await this.typeGroup(timeRangData);
  84. ctx.logger.info('任务进行typeGroup');
  85. await ctx.service.statisticsService.save(ctx.model.Local.MsgCenterRecordModel,
  86. { ...initData, colorTotal, grayTotal, readTotal, unReadTotal, total, msgType }, isForceUpdate);
  87. }
  88. async typeGroup({ startTime, endTime }) {
  89. const { ctx } = this;
  90. const agg = [
  91. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  92. { $group: {
  93. _id: '$message_type',
  94. count: { $sum: 1 },
  95. readCount: { $sum: {
  96. $cond: [{ $eq: [ '$reading_state', 1 ] }, 1, 0 ],
  97. } },
  98. } },
  99. { $lookup: { from: 'msg_type', localField: '_id', foreignField: '_id', as: 'msg' } },
  100. { $unwind: { path: '$msg', preserveNullAndEmptyArrays: true } },
  101. { $addFields: {
  102. msgId: { $cond: {
  103. if: { $eq: [ '$msg.parent_id', 0 ] },
  104. then: '$msg._id', else: '$msg.parent_id',
  105. } } },
  106. },
  107. { $group: {
  108. _id: '$msgId',
  109. count: { $sum: '$count' },
  110. readCount: { $sum: '$readCount' },
  111. } },
  112. ];
  113. return await ctx.model.MsgCenterRecordModel.aggregateFix(agg);
  114. }
  115. async channelGroup({ startTime, endTime }) {
  116. const { ctx } = this;
  117. const agg = [
  118. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  119. { $group: {
  120. _id: '$message_channel',
  121. count: { $sum: 1 },
  122. } },
  123. ];
  124. return await ctx.model.MsgCenterRecordModel.aggregateFix(agg);
  125. }
  126. async readGroup({ startTime, endTime }) {
  127. const { ctx } = this;
  128. const agg = [
  129. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  130. { $group: {
  131. _id: '$reading_state',
  132. count: { $sum: 1 },
  133. } },
  134. ];
  135. return await ctx.model.MsgCenterRecordModel.aggregateFix(agg);
  136. }
  137. }
  138. module.exports = MsgCenterRecordService2;