lrf 2 years ago
parent
commit
db3980807b
1 changed files with 49 additions and 9 deletions
  1. 49 9
      app/service/statistics.js

+ 49 - 9
app/service/statistics.js

@@ -286,8 +286,29 @@ class StatisticsService extends CrudService {
     return { mm, m3m, m6m, my };
   }
 
+  async studentSign({ lesson_id, student_id, range, skip, limit }) {
+    const pipeline = [];
+    let $match = { lesson_id, student_id };
+    if (_.isArray(range)) {
+      const start = _.head(range);
+      const end = _.last(range);
+      const timeQuery = (start, end) => ({ $and: [{ time_start: { $gte: start } }, { time_start: { $lte: end } }] });
+      const tq = timeQuery(start, end);
+      $match = { ...$match, ...tq };
+    }
+    pipeline.push({ $match });
+
+    //
+
+    pipeline.push({ $sort: { time_start: 1 } });
+    // 返回数据:学生,上课时间,课程,是否签单,是否缴费
+    const res = await this.lessonModel.aggregate(pipeline);
+    return res;
+  }
+
   async schoolInByLesson({ school_id, time = 'm', skip = 0, limit }) {
     const tq = this.resetQuery(time, 'time_start');
+    console.log(JSON.stringify(tq, null, 1));
     const query = { ...tq, school_id };
     const pipeline = [{ $match: query }];
     const baseCol = { time_start: 1, title: 1 };
@@ -308,9 +329,7 @@ class StatisticsService extends CrudService {
         from: 'lessonStudent',
         localField: 'lesson_id',
         foreignField: 'lesson_id',
-        pipeline: [
-          { $group: { _id: '$lesson_id', num: { $sum: 1 } } },
-        ],
+        pipeline: [{ $group: { _id: '$lesson_id', num: { $sum: 1 } } }],
         as: 'zStudent',
       },
     });
@@ -333,10 +352,31 @@ class StatisticsService extends CrudService {
     baseCol.lStudent = 1;
     pipeline.push({ $set: { total: { $multiply: [ '$money', { $add: [ '$zStudent', '$lStudent' ] }] } } });
     // #endregion
-    if (parseInt(skip)) pipeline.push({ $skip: parseInt(skip) });
-    if (parseInt(limit))pipeline.push({ $limit: parseInt(limit) });
-    const res = await this.lessonModel.aggregate(pipeline);
-    return res;
+    const qp = _.cloneDeep(pipeline);
+    if (parseInt(skip)) qp.push({ $skip: parseInt(skip) });
+    if (parseInt(limit)) qp.push({ $limit: parseInt(limit) });
+    const data = await this.lessonModel.aggregate(qp);
+    const tp = _.cloneDeep(pipeline);
+    tp.push(this.totalPip());
+    const tr = await this.lessonModel.aggregate(tp);
+    const total = this.getTotal(tr);
+    return { data, total };
+  }
+
+  // 聚合总数管道
+  totalPip() {
+    return {
+      $count: 'total',
+    };
+  }
+  /**
+   * 取出聚合查询的总数
+   * @param {Array} data 聚合查询总数的结果($count)
+   * @param {String} key 总数的字段
+   * @return {Number} 返回总数
+   */
+  getTotal(data, key = 'total') {
+    return _.get(_.head(data), key, 0);
   }
 
   resetQuery(time, key = 'time') {
@@ -350,8 +390,8 @@ class StatisticsService extends CrudService {
     } else if ([ '3m', '6m' ].includes(time)) {
       const ms = _.head(time.split('')); // 月份数量 3/6/...
       const start = moment().subtract(ms, 'months').format('YYYY-MM-01');
-      const { lastDate } = this.getPartsOfNow();
-      const end = lastDate;
+      const { year, month, lastDate } = this.getPartsOfNow();
+      const end = `${year}-${month}-${lastDate}`;
       query = { ...query, ...timeQuery(start, end) };
     } else if (time === '1y') {
       const { year } = this.getPartsOfNow();