123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 'use strict';
- const Service = require('egg').Service;
- class TRbacUserService extends Service {
- async index(cond, condStr = 'total') {
- const { ctx } = this;
- const maxStrArray = [ 'total', 'appTotal', 'iviTotal' ];
- let agg = [];
- if (maxStrArray.indexOf(condStr) !== -1) {
- agg = ctx.helper.getCommonAggMax({ ...cond, value: condStr });
- } else {
- agg = ctx.helper.getCommonAggSum({ ...cond, value: condStr });
- }
- return await ctx.model.Local.TRbacUserModel.aggregateFix(agg);
- }
- async sexAndAge(condStr = 'sexAndAgeTotal') {
- const { ctx } = this;
- const oneCond = { create_date: 1 };
- oneCond[condStr] = 1;
- const result = await ctx.model.Local.TRbacUserModel.findOne({}, oneCond)
- .sort({ create_date: -1 });
- if (result) {
- return { time: ctx.helper.formatTime1(result.create_date), list: result[condStr] };
- }
- return { time: '', list: [] };
- }
- async statistics({ timeRangData, initData, isForceUpdate }) {
- const { ctx } = this;
- const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.TRbacUserModel,
- { ...initData });
- if (hasData && !isForceUpdate) {
- return;
- }
- initData.start_time = new Date();
- const total = await this.total({ create_date: { $lt: timeRangData.endTime } });
- ctx.logger.info('任务进行total');
- const newTotal = await this.total({ create_date:
- { $gte: timeRangData.startTime, $lt: timeRangData.endTime } });
- ctx.logger.info('任务进行newTotal');
- const sexAndAgeTotal = await this.sex2Age({ create_date: { $lt: timeRangData.endTime } });
- ctx.logger.info('任务进行sexAndAgeTotal');
- let appTotal = 0;
- if (timeRangData.startTime >= ctx.helper.timeMonthSeven) {
- appTotal = await this.total({
- create_date: { $lt: timeRangData.endTime }, user_orgin: 'APP',
- });
- ctx.logger.info('任务进行appTotal');
- }
- const appSexAndAgeTotal = await this.sex2Age({
- create_date: { $lt: timeRangData.endTime }, user_orgin: 'APP' });
- ctx.logger.info('任务进行appSexAndAgeTotal');
- const iviTotal = await this.total({
- create_date: { $lt: timeRangData.endTime }, user_orgin: 'IVI' });
- ctx.logger.info('任务进行iviTotal');
- const iviSexAndAgeTotal = await this.sex2Age({
- create_date: { $lt: timeRangData.endTime }, user_orgin: 'IVI' });
- ctx.logger.info('任务进行iviSexAndAgeTotal');
- let newAppTotal = 0;
- if (timeRangData.startTime >= ctx.helper.timeMonthSeven) {
- newAppTotal = await this.total({
- create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'APP',
- });
- ctx.logger.info('任务进行newAppTotal');
- }
- const newAppSexAndAgeTotal = await this.sex2Age({
- create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'APP' });
- ctx.logger.info('任务进行newAppSexAndAgeTotal');
- const newIviTotal = await this.total({
- create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'IVI' });
- ctx.logger.info('任务进行newIviTotal');
- const newIviSexAndAgeTotal = await this.sex2Age({
- create_date: { $gte: timeRangData.startTime, $lt: timeRangData.endTime }, user_orgin: 'IVI' });
- ctx.logger.info('任务进行newIviSexAndAgeTotal');
- let activeAppTotal = 0;
- if (timeRangData.startTime >= ctx.helper.timeMonthSeven) {
- activeAppTotal = await this.activeAppTotal(timeRangData);
- ctx.logger.info('任务进行activeAppTotal');
- }
- const activeAppMonth = await this.activeAppTotal2({ startTime: ctx.helper.getMonthTop(timeRangData.startTime),
- endTime: timeRangData.endTime }, 3);
- ctx.logger.info('任务进行activeAppMonth');
- const activeAppYear = await this.activeAppTotal2({ startTime: ctx.helper.nowYear(timeRangData.startTime),
- endTime: timeRangData.endTime }, 30);
- ctx.logger.info('任务进行activeAppYear');
- const activeAppSexAndAgeTotal = await this.activeAppSex2Age(timeRangData);
- ctx.logger.info('任务进行activeAppSexAndAgeTotal');
- const activeIviTotal = await this.activeIviTotal(timeRangData);
- ctx.logger.info('任务进行activeIviTotal');
- const activeIviMonth = await this.activeIviTotal2({ startTime: ctx.helper.getMonthTop(timeRangData.startTime),
- endTime: timeRangData.endTime }, 3);
- ctx.logger.info('任务进行activeIviMonth');
- const activeIviYear = await this.activeIviTotal2({ startTime: ctx.helper.nowYear(timeRangData.startTime),
- endTime: timeRangData.endTime }, 30);
- ctx.logger.info('任务进行activeIviYear');
- const activeIviSexAndAgeTotal = await this.activeIviSex2Age(timeRangData);
- ctx.logger.info('任务进行activeIviSexAndAgeTotal');
- // 目前的所有性别年龄统计 和这下面的3个 算法-算车主都是算最终态
- const saledTotal = await this.total({ role_id: ctx.helper.saledRoleId });
- ctx.logger.info('任务进行saledTotal');
- const saledAppTotal = await this.total({ user_orgin: 'APP', role_id: ctx.helper.saledRoleId });
- ctx.logger.info('任务进行saledAppTotal');
- const saledIviTotal = await this.total({ user_orgin: 'IVI', role_id: ctx.helper.saledRoleId });
- ctx.logger.info('任务进行saledIviTotal');
- await ctx.service.statisticsService.save(ctx.model.Local.TRbacUserModel,
- { ...initData, total, newTotal, sexAndAgeTotal,
- appTotal, appSexAndAgeTotal,
- newAppTotal, newAppSexAndAgeTotal,
- activeAppTotal, activeAppMonth, activeAppYear, activeAppSexAndAgeTotal,
- iviTotal, iviSexAndAgeTotal,
- newIviTotal, newIviSexAndAgeTotal,
- activeIviTotal, activeIviMonth, activeIviYear, activeIviSexAndAgeTotal,
- saledTotal, saledAppTotal, saledIviTotal,
- }, isForceUpdate);
- }
- async total(cond) {
- const { ctx } = this;
- return await ctx.model.TRbacUserModel.find(cond).countDocuments();
- }
- async sex2Age(cond) {
- const { ctx } = this;
- const agg = [
- { $match: { ...cond, role_id: ctx.helper.saledRoleId } },
- ...ctx.helper.getAggAndSexMongo(),
- ];
- return await ctx.model.TRbacUserModel.aggregateFix(agg);
- }
- async activeAppTotal({ startTime, endTime }) {
- const { ctx } = this;
- const onlineAgg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
- { $group: {
- _id: '$user_Id',
- } },
- ];
- const result1 = await ctx.model.AppOnlineUserModel.aggregateFix(onlineAgg);
- // const unionArray = result1.map(item => item._id);
- return result1.length;
- // return await ctx.model.TRbacUserModel.find({ user_id: { $in: unionArray }, user_orgin: 'APP' }).countDocuments();
- }
- async activeAppTotal2({ startTime, endTime }, count = 1) {
- const { ctx } = this;
- const onlineAgg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
- { $group: {
- _id: { user_id: '$user_Id', login_date: { $dateToString:
- { date: { $toDate: '$login_time' }, format: '%Y-%m-%d',
- timezone: 'Asia/Shanghai', onNull: 0 } } },
- } },
- { $group: {
- _id: '$_id.user_id',
- login_count: { $sum: 1 },
- } },
- { $match: { login_count: { $gte: count } } },
- ];
- const result1 = await ctx.model.AppOnlineUserModel.aggregateFix(onlineAgg);
- // const unionArray = result1.map(item => item._id);
- return result1.length;
- // return await ctx.model.TRbacUserModel.find({ user_id: { $in: unionArray }, user_orgin: 'APP' }).countDocuments();
- }
- async activeAppSex2Age({ startTime, endTime }) {
- const { ctx } = this;
- const onlineAgg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
- { $group: {
- _id: '$user_Id',
- } },
- ];
- const result1 = await ctx.model.AppOnlineUserModel.aggregateFix(onlineAgg);
- const unionArray = result1.map(item => item._id);
- const agg = [
- { $match: { user_id: { $in: unionArray }, user_orgin: 'APP' } },
- ...ctx.helper.getAggAndSexMongo(),
- ];
- return await ctx.model.TRbacUserModel.aggregateFix(agg);
- }
- async activeIviTotal({ startTime, endTime }) {
- const { ctx } = this;
- const agg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
- { $group: {
- _id: '$user_id' },
- },
- ];
- const result = await ctx.model.IviOnlineUserModel.aggregateFix(agg);
- return await ctx.model.TRbacUserModel.find({ user_id: { $in: result.map(item => item._id) },
- user_orgin: 'IVI' }).countDocuments();
- }
- async activeIviTotal2({ startTime, endTime }, count = 1) {
- const { ctx } = this;
- const agg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
- { $group: {
- _id: { user_id: '$user_id', login_date: { $dateToString:
- { date: { $toDate: '$login_time' }, format: '%Y-%m-%d',
- timezone: 'Asia/Shanghai', onNull: 0 } } },
- } },
- { $group: {
- _id: '$_id.user_id',
- login_count: { $sum: 1 },
- } },
- { $match: { login_count: { $gte: count } } },
- ];
- const result = await ctx.model.IviOnlineUserModel.aggregateFix(agg);
- return await ctx.model.TRbacUserModel.find({ user_id: { $in: result.map(item => item._id) },
- user_orgin: 'IVI' }).countDocuments();
- }
- async activeIviSex2Age({ startTime, endTime }) {
- const { ctx } = this;
- const onlineAgg = [
- { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
- { $group: {
- _id: '$user_id',
- } },
- ];
- const result = await ctx.model.IviOnlineUserModel.aggregateFix(onlineAgg);
- const unionArray = result.map(item => item._id);
- const agg = [
- { $match: { user_id: { $in: unionArray }, user_orgin: 'IVI' } },
- ...ctx.helper.getAggAndSexMongo(),
- ];
- return await ctx.model.TRbacUserModel.aggregateFix(agg);
- }
- }
- module.exports = TRbacUserService;
|