|
@@ -0,0 +1,83 @@
|
|
|
+'use strict';
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const moment = require('moment');
|
|
|
+
|
|
|
+// 统计
|
|
|
+class CountService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'count');
|
|
|
+ this.model = this.ctx.model.Count;
|
|
|
+ this.card = this.ctx.model.Card;
|
|
|
+ }
|
|
|
+
|
|
|
+ async index(query) {
|
|
|
+ const { id } = query;
|
|
|
+ if (!id) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未接收到用户信息');
|
|
|
+ const user = await this.card.findById(id);
|
|
|
+ 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 });
|
|
|
+ console.log(`group:${group}`);
|
|
|
+ return { yesterday, week, month, group };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计团队推销的卡
|
|
|
+ * @param {Object} data 查询条件
|
|
|
+ * @param {String} method 查询方法:count数量/find详情
|
|
|
+ */
|
|
|
+ async group({ r_mobile, ...info }, method = 'count') {
|
|
|
+ const group = await this.card[method]({ r_mobile, ...info });
|
|
|
+ return group;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计昨天推销的卡
|
|
|
+ * @param {Object} data 查询条件
|
|
|
+ * @param {String} method 查询方法:count数量/find详情
|
|
|
+ */
|
|
|
+ async yesterday({ r_mobile, ...info }, method = 'count') {
|
|
|
+ const 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') } });
|
|
|
+ console.log(`yesterday:${yesterday}`);
|
|
|
+ return yesterday;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计本周推销的卡
|
|
|
+ * @param {Object} data 查询条件
|
|
|
+ * @param {String} method 查询方法:count数量/find详情
|
|
|
+ */
|
|
|
+ async week({ r_mobile, ...info }, method = 'count') {
|
|
|
+ const wnum = moment().week();
|
|
|
+ const ws = moment().subtract(wnum, 'days').format('YYYY-MM-DD');
|
|
|
+ const we = moment().add(7 - wnum, 'days').format('YYYY-MM-DD');
|
|
|
+ const week = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ws, $lte: we } });
|
|
|
+ console.log(`week:${week}`);
|
|
|
+ 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');
|
|
|
+ const month = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ms, $lte: me } });
|
|
|
+ console.log(`month:${month}`);
|
|
|
+ return month;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = CountService;
|