'use strict'; const Service = require('egg').Service; class MsgCenterRecordService2 extends Service { async channel({ type, startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime) }, { $group: ctx.helper.getTimeGroup(type, { colorTotal: { $sum: '$colorTotal' }, grayTotal: { $sum: '$grayTotal' }, }) }, ]; return await ctx.model.Local.MsgCenterRecordModel.aggregateFix(agg); } async read({ type, startTime, endTime, msgType }) { const { ctx } = this; let cond = []; if (msgType) { cond = [{ $match: { 'msgType._id': msgType } }]; } const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime) }, { $unwind: '$msgType' }, ...cond, { $group: ctx.helper.getTimeGroup(type, { readCount: { $sum: '$msgType.readCount' }, total: { $sum: '$msgType.count' }, }) }, ]; return await ctx.model.Local.MsgCenterRecordModel.aggregateFix(agg); } async over({ type, startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime) }, { $group: ctx.helper.getTimeGroup(type, { readTotal: { $sum: '$readTotal' }, unReadTotal: { $sum: '$unReadTotal' }, }) }, ]; return await ctx.model.Local.MsgCenterRecordModel.aggregateFix(agg); } // ====================清洗分割线========================================================================================= async statistics({ timeRangData, initData, isForceUpdate }) { const { ctx } = this; const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.MsgCenterRecordModel, { ...initData }); if (hasData && !isForceUpdate) { return; } initData.start_time = new Date(); const cResult = await this.channelGroup(timeRangData); ctx.logger.info('任务进行channelGroup'); const rResult = await this.readGroup(timeRangData); ctx.logger.info('任务进行readGroup'); let colorTotal = 0;// 彩色数 let grayTotal = 0;// 灰色数 let readTotal = 0;// 已读数 let unReadTotal = 0;// 未读数 let total = 0;// 总数 cResult.forEach(item => { switch (item._id) { case 1: grayTotal = item.count; break; case 2: colorTotal = item.count; break; default: break; } total += item.count; }); rResult.forEach(item => { switch (item._id) { case 0: unReadTotal = item.count; break; case 1: readTotal = item.count; break; default: break; } }); const msgType = await this.typeGroup(timeRangData); ctx.logger.info('任务进行typeGroup'); await ctx.service.statisticsService.save(ctx.model.Local.MsgCenterRecordModel, { ...initData, colorTotal, grayTotal, readTotal, unReadTotal, total, msgType }, isForceUpdate); } async typeGroup({ startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') }, { $group: { _id: '$message_type', count: { $sum: 1 }, readCount: { $sum: { $cond: [{ $eq: [ '$reading_state', 1 ] }, 1, 0 ], } }, } }, { $lookup: { from: 'msg_type', localField: '_id', foreignField: '_id', as: 'msg' } }, { $unwind: { path: '$msg', preserveNullAndEmptyArrays: true } }, { $addFields: { msgId: { $cond: { if: { $eq: [ '$msg.parent_id', 0 ] }, then: '$msg._id', else: '$msg.parent_id', } } }, }, { $group: { _id: '$msgId', count: { $sum: '$count' }, readCount: { $sum: '$readCount' }, } }, ]; return await ctx.model.MsgCenterRecordModel.aggregateFix(agg); } async channelGroup({ startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') }, { $group: { _id: '$message_channel', count: { $sum: 1 }, } }, ]; return await ctx.model.MsgCenterRecordModel.aggregateFix(agg); } async readGroup({ startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') }, { $group: { _id: '$reading_state', count: { $sum: 1 }, } }, ]; return await ctx.model.MsgCenterRecordModel.aggregateFix(agg); } } module.exports = MsgCenterRecordService2;