lrf402788946 4 年 前
コミット
a486897808
1 ファイル変更47 行追加7 行削除
  1. 47 7
      app/service/count.js

+ 47 - 7
app/service/count.js

@@ -2,6 +2,7 @@
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const moment = require('moment');
+const _ = require('lodash');
 
 // 统计
 class CountService extends CrudService {
@@ -9,6 +10,8 @@ class CountService extends CrudService {
     super(ctx, 'count');
     this.model = this.ctx.model.Count;
     this.card = this.ctx.model.Card;
+    // group,yesterday,week,month查询是否需要详细信息
+    this.needDetail = false;
   }
 
   async index(query) {
@@ -35,7 +38,17 @@ class CountService extends CrudService {
    * @param {String} method 查询方法:count数量/find详情
    */
   async group({ r_mobile, ...info }, method = 'count') {
-    const group = await this.card[method]({ r_mobile, ...info });
+    let group = await this.card[method]({ r_mobile, ...info });
+    if (method !== 'count') {
+      group = _.groupBy(group, 'set');
+      if (!this.needDetail) {
+        // 如果需要详情,则把下面代码注释
+        const keys = Object.keys(group);
+        for (const key of keys) {
+          group[key] = group[key].length;
+        }
+      }
+    }
     return group;
   }
 
@@ -46,8 +59,17 @@ class CountService extends CrudService {
    * @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}`);
+    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') {
+      yesterday = _.groupBy(yesterday, 'set');
+      if (!this.needDetail) {
+        // 如果需要详情,则把下面代码注释
+        const keys = Object.keys(yesterday);
+        for (const key of keys) {
+          yesterday[key] = yesterday[key].length;
+        }
+      }
+    }
     return yesterday;
   }
 
@@ -60,8 +82,17 @@ class CountService extends CrudService {
     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}`);
+    let week = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ws, $lte: we } });
+    if (method !== 'count') {
+      week = _.groupBy(week, 'set');
+      if (!this.needDetail) {
+        // 如果需要详情,则把下面代码注释
+        const keys = Object.keys(week);
+        for (const key of keys) {
+          week[key] = week[key].length;
+        }
+      }
+    }
     return week;
   }
 
@@ -74,8 +105,17 @@ class CountService extends CrudService {
     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}`);
+    let month = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ms, $lte: me } });
+    if (method !== 'count') {
+      month = _.groupBy(month, 'set');
+      if (!this.needDetail) {
+        // 如果需要详情,则把下面代码注释
+        const keys = Object.keys(month);
+        for (const key of keys) {
+          month[key] = month[key].length;
+        }
+      }
+    }
     return month;
   }
 }