lrf402788946 4 years ago
parent
commit
012a67ab70
4 changed files with 141 additions and 0 deletions
  1. 43 0
      app/controller/count.js
  2. 1 0
      app/router.js
  3. 14 0
      app/router/count.js
  4. 83 0
      app/service/count.js

+ 43 - 0
app/controller/count.js

@@ -0,0 +1,43 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 统计
+class CountController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.count;
+  }
+
+  /**
+   * 首页四部分统计
+   */
+  async index() {
+    const res = await this.service.index(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+
+  /**
+   * 下面的四个是首页四部分详情,带条件,至少传入 r_mobile
+   */
+  async yesterday() {
+    const res = await this.service.yesterday(this.ctx.query, 'find');
+    this.ctx.ok({ data: res });
+  }
+
+  async week() {
+    const res = await this.service.week(this.ctx.query, 'find');
+    this.ctx.ok({ data: res });
+  }
+
+  async month() {
+    const res = await this.service.month(this.ctx.query, 'find');
+    this.ctx.ok({ data: res });
+  }
+
+  async group() {
+    const res = await this.service.group(this.ctx.query, 'find');
+    this.ctx.ok({ data: res });
+  }
+}
+module.exports = CrudController(CountController, {});

+ 1 - 0
app/router.js

@@ -12,4 +12,5 @@ module.exports = app => {
   require('./router/card')(app); // 办卡
   require('./router/cash')(app); // 提现
   require('./router/record')(app); // 记录
+  require('./router/count')(app); // 统计
 };

+ 14 - 0
app/router/count.js

@@ -0,0 +1,14 @@
+'use strict';
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const prefix = '/api/htyd';
+  const index = 'count';
+  const { router, controller } = app;
+  router.get(index, `${prefix}/${index}/index`, controller[index].index);
+  router.get(index, `${prefix}/${index}/yesterday`, controller[index].yesterday);
+  router.get(index, `${prefix}/${index}/week`, controller[index].week);
+  router.get(index, `${prefix}/${index}/month`, controller[index].month);
+  router.get(index, `${prefix}/${index}/group`, controller[index].group);
+};

+ 83 - 0
app/service/count.js

@@ -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;