123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- 'use strict';
- const Service = require('egg').Service;
- class IviBehaviorRecordService 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' },
- musicTotal: { $sum: '$musicTotal' }, fwTotal: { $sum: '$fwTotal' },
- newsTotal: { $sum: '$newsTotal' }, videoTotal: { $sum: '$videoTotal' } }) },
- ];
- return await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(agg);
- }
- async ext() {
- const { ctx } = this;
- const musicMonthAgg = [
- { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: '$musicTotal' } } },
- { $sort: { count: -1 } },
- ];
- const musicYearAgg = [
- { $group: { _id: { year: '$year' }, count: { $sum: '$musicTotal' } } },
- { $sort: { count: -1 } },
- ];
- const musicMResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(musicMonthAgg);
- const musicYResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(musicYearAgg);
- let musicMaxMonth = {};
- let musicMinMonth = {};
- let musicMaxYear = {};
- let musicMinYear = {};
- if (musicMResult && musicMResult.length > 0) {
- musicMaxMonth = musicMResult[0];
- musicMinMonth = musicMResult[musicMResult.length - 1];
- }
- if (musicYResult && musicYResult.length > 0) {
- musicMaxYear = musicYResult[0];
- musicMinYear = musicYResult[musicYResult.length - 1];
- }
- const fwMonthAgg = [
- { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: '$fwTotal' } } },
- { $sort: { count: -1 } },
- ];
- const fwYearAgg = [
- { $group: { _id: { year: '$year' }, count: { $sum: '$fwTotal' } } },
- { $sort: { count: -1 } },
- ];
- const fwMResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(fwMonthAgg);
- const fwYResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(fwYearAgg);
- let fwMaxMonth = {};
- let fwMinMonth = {};
- let fwMaxYear = {};
- let fwMinYear = {};
- if (fwMResult && fwMResult.length > 0) {
- fwMaxMonth = fwMResult[0];
- fwMinMonth = fwMResult[fwMResult.length - 1];
- }
- if (fwYResult && fwYResult.length > 0) {
- fwMaxYear = fwYResult[0];
- fwMinYear = fwYResult[fwMResult.length - 1];
- }
- const videoMonthAgg = [
- { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: '$videoTotal' } } },
- { $sort: { count: -1 } },
- ];
- const videoYearAgg = [
- { $group: { _id: { year: '$year' }, count: { $sum: '$videoTotal' } } },
- { $sort: { count: -1 } },
- ];
- const videoMResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(videoMonthAgg);
- const videoYResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(videoYearAgg);
- let videoMaxMonth = {};
- let videoMinMonth = {};
- let videoMaxYear = {};
- let videoMinYear = {};
- if (videoMResult && videoMResult.length > 0) {
- videoMaxMonth = videoMResult[0];
- videoMinMonth = videoMResult[videoMResult.length - 1];
- }
- if (videoYResult && videoYResult.length > 0) {
- videoMaxYear = videoYResult[0];
- videoMinYear = videoMResult[videoMResult.length - 1];
- }
- return {
- musicMaxMonth, musicMinMonth, musicMaxYear, musicMinYear,
- fwMaxMonth, fwMinMonth, fwMaxYear, fwMinYear,
- videoMaxMonth, videoMinMonth, videoMaxYear, videoMinYear,
- };
- }
- // ====================清洗分割线==================================================================
- async statistics({ timeRangData, initData, isForceUpdate }) {
- const { ctx } = this;
- const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.IviBehaciorRecordModel,
- { ...initData });
- if (hasData && !isForceUpdate) {
- return;
- }
- initData.start_time = new Date();
- const obj = await this.total(timeRangData);
- const vins = await this.geneTemp(timeRangData);
- let musicTotal = 0;
- let fwTotal = 0;
- let newsTotal = 0;
- let videoTotal = 0;
- const iviBehavior = await this.group(timeRangData, vins);
- iviBehavior.forEach(item => {
- switch (item._id) {
- case 20020001:
- musicTotal = item.count;
- break;
- case 20010001:
- fwTotal = item.count;
- break;
- case 20050001:
- newsTotal = item.count;
- break;
- case 20030001:
- videoTotal = item.count;
- break;
- default:
- break;
- }
- });
- await ctx.service.statisticsService.save(ctx.model.Local.IviBehaciorRecordModel,
- { ...initData, musicTotal, fwTotal, newsTotal, videoTotal, ...obj }, isForceUpdate);
- }
- async geneTemp({ startTime, endTime }) {
- const { ctx } = this;
- await ctx.model.TempModel.deleteMany();
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
- { $out: 'temp' },
- ];
- await ctx.model.IviBehaviorRecordModel.aggregate(agg);
- const aggVin = [
- { $group: { _id: '$vin' } },
- ];
- const result = await ctx.model.TempModel.aggregate(aggVin);
- return result.map(item => item._id);
- }
- async total({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
- { $group: { _id: null,
- total: { $sum: { $toLong: '$mileage_cnt' } } } },
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateNGroup(agg);
- }
- async group({ startTime, endTime }, vins) {
- const { ctx } = this;
- const agg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time'), vin: { $in: vins } } },
- { $unwind: '$mileage_list' },
- { $lookup:
- {
- from: 'temp',
- let: { vin: '$vin', stime: '$mileage_list.start_time', etime: '$mileage_list.end_time' },
- pipeline: [{ $match: { $expr: { $and:
- [
- { $gte: [ '$create_time', '$$stime' ] },
- { $lt: [ '$create_time', '$$etime' ] },
- { $eq: [ '$vin', '$$vin' ] },
- ] } } }], as: 'ivi',
- },
- },
- { $unwind: '$ivi' },
- { $group: {
- _id: '$mileage_list.start_time',
- behavior_id: { $addToSet: '$ivi.behavior_id' },
- } },
- { $unwind: '$behavior_id' },
- { $group: {
- _id: '$behavior_id',
- count: { $sum: 1 },
- } },
- ];
- return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
- }
- }
- module.exports = IviBehaviorRecordService;
|