'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const { ObjectId } = require('mongoose').Types; const moment = require('moment'); const _ = require('lodash'); // 统计 class CountService extends CrudService { constructor(ctx) { super(ctx, 'count'); this.model = this.ctx.model.Count; this.card = this.ctx.model.Card; this.set = this.ctx.model.Set; // group,yesterday,week,month查询是否需要详细信息 this.needDetail = false; } async index(query) { const { mobile } = query; if (!mobile) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未接收到用户信息'); const user = await this.card.findOne({ mobile }); const { mobile: r_mobile } = user; // 昨日新增 // day-1 00:00:00 - day-1 23:59:59 const yesterday = await this.yesterday({ r_mobile }); // 本周新增 const week = await this.week({ r_mobile }); // 当月新增 const month = await this.month({ r_mobile }); // 团队统计 const group = await this.group({ r_mobile }); return { yesterday, week, month, group }; } /** * 统计团队推销的卡 * @param {Object} data 查询条件 * @param {String} method 查询方法:count数量/find详情 */ async group({ r_mobile, ...info }, method = 'count') { let group = await this.card[method]({ r_mobile, ...info }); if (method !== 'count') { group = _.groupBy(group, 'set'); if (!this.needDetail) { const result = {}; // 如果需要详情,则把下面代码注释 const keys = Object.keys(group); const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) }); for (const key of keys) { const res = setList.find(f => ObjectId(f._id).equals(key)); if (res) { result[`${res.title}`] = group[key].length; } } return result; } } return group; } /** * 统计昨天推销的卡 * @param {Object} data 查询条件 * @param {String} method 查询方法:count数量/find详情 */ async yesterday({ r_mobile, ...info }, method = 'count') { let yesterday = await this.card[method]({ r_mobile, ...info, create_time: { $gte: moment().subtract(1, 'days').format('YYYY-MM-DD'), $lte: moment().format('YYYY-MM-DD') } }); if (method !== 'count') { const result = {}; yesterday = _.groupBy(yesterday, 'set'); if (!this.needDetail) { // 如果需要详情,则把下面代码注释 const keys = Object.keys(yesterday); const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) }); for (const key of keys) { const res = setList.find(f => ObjectId(f._id).equals(key)); if (res) { result[`${res.title}`] = yesterday[key].length; } } } return result; } return yesterday; } /** * 统计本周推销的卡 * @param {Object} data 查询条件 * @param {String} method 查询方法:count数量/find详情 */ async week({ r_mobile, ...info }, method = 'count') { const wnum = moment().weekday(); const ws = moment().subtract(wnum - 1, 'days').format('YYYY-MM-DD'); const we = moment().add(7 - wnum, 'days').format('YYYY-MM-DD'); let week = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ws, $lte: we } }); if (method !== 'count') { const result = {}; week = _.groupBy(week, 'set'); if (!this.needDetail) { // 如果需要详情,则把下面代码注释 const keys = Object.keys(week); const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) }); for (const key of keys) { const res = setList.find(f => ObjectId(f._id).equals(key)); if (res) { result[`${res.title}`] = week[key].length; } } } return result; } return week; } /** * 统计本周推销的卡 * @param {Object} data 查询条件 * @param {String} method 查询方法:count数量/find详情 */ async month({ r_mobile, ...info }, method = 'count') { const prefix = moment().format('YYYY-MM-'); const ms = `${prefix}01`; const me = moment(ms).add(1, 'months').format('YYYY-MM-DD'); let month = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ms, $lte: me } }); if (method !== 'count') { const result = {}; month = _.groupBy(month, 'set'); if (!this.needDetail) { // 如果需要详情,则把下面代码注释 const keys = Object.keys(month); const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) }); for (const key of keys) { const res = setList.find(f => ObjectId(f._id).equals(key)); if (res) { result[`${res.title}`] = month[key].length; } } } return result; } return month; } } module.exports = CountService;