'use strict'; const Service = require('egg').Service; class MsgQueryRecordService extends Service { async index({ type, startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime) }, { $group: ctx.helper.getTimeGroup(type, { total: { $sum: '$total' }, successTotal: { $sum: '$successTotal' }, failTotal: { $sum: '$failTotal' }, }) }, ]; return await ctx.model.Local.MsgQueryRecordModel.aggregateFix(agg); } // ====================清洗分割线================================================================== async statistics({ timeRangData, initData, isForceUpdate }) { const { ctx } = this; const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.MsgQueryRecordModel, { ...initData }); if (hasData && !isForceUpdate) { return; } initData.start_time = new Date(); const result = await this.group(timeRangData); let successTotal = 0;// 成功数 let failTotal = 0;// 失败数 let total = 0;// 总数 result.forEach(item => { switch (item._id) { case 0: failTotal = item.count; break; case 1: successTotal = item.count; break; default: break; } total += item.count; }); await ctx.service.statisticsService.save(ctx.model.Local.MsgQueryRecordModel, { ...initData, successTotal, failTotal, total }, isForceUpdate); } async group({ startTime, endTime }) { const { ctx } = this; const agg = [ { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') }, { $group: { _id: '$is_success', count: { $sum: 1 }, } }, ]; return await ctx.model.MsgQueryRecordModel.aggregateFix(agg); } } module.exports = MsgQueryRecordService;