|
@@ -1,6 +1,7 @@
|
|
|
'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');
|
|
|
|
|
@@ -10,14 +11,15 @@ class CountService extends CrudService {
|
|
|
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 { id } = query;
|
|
|
- if (!id) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未接收到用户信息');
|
|
|
- const user = await this.card.findById(id);
|
|
|
+ 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
|
|
@@ -42,11 +44,18 @@ class CountService extends CrudService {
|
|
|
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) {
|
|
|
- group[key] = group[key].length;
|
|
|
+ const res = setList.find(f => ObjectId(f._id).equals(key));
|
|
|
+ if (res) {
|
|
|
+ result[`${res.title}`] = group[key].length;
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|
|
|
return group;
|
|
@@ -61,14 +70,21 @@ class CountService extends CrudService {
|
|
|
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) {
|
|
|
- yesterday[key] = yesterday[key].length;
|
|
|
+ const res = setList.find(f => ObjectId(f._id).equals(key));
|
|
|
+ if (res) {
|
|
|
+ result[`${res.title}`] = yesterday[key].length;
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
return yesterday;
|
|
|
}
|
|
@@ -84,14 +100,21 @@ class CountService extends CrudService {
|
|
|
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) {
|
|
|
- week[key] = week[key].length;
|
|
|
+ const res = setList.find(f => ObjectId(f._id).equals(key));
|
|
|
+ if (res) {
|
|
|
+ result[`${res.title}`] = week[key].length;
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
return week;
|
|
|
}
|
|
@@ -107,14 +130,20 @@ class CountService extends CrudService {
|
|
|
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) {
|
|
|
- month[key] = month[key].length;
|
|
|
+ const res = setList.find(f => ObjectId(f._id).equals(key));
|
|
|
+ if (res) {
|
|
|
+ result[`${res.title}`] = month[key].length;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
return month;
|
|
|
}
|