lrf 2 years ago
parent
commit
b27cd0a40a
2 changed files with 27 additions and 21 deletions
  1. 1 0
      app/controller/config/.statistics.js.js
  2. 26 21
      app/service/statistics.js

+ 1 - 0
app/controller/config/.statistics.js.js

@@ -18,6 +18,7 @@ module.exports = {
     parameters: {
       query: {
         school_id: 'school_id',
+        coach_id: 'coach_id',
       },
     },
   },

+ 26 - 21
app/service/statistics.js

@@ -16,34 +16,39 @@ class StatisticsService extends CrudService {
     this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
   }
   // 学校统计 教练出勤率
-  async schoolSignCoach({ school_id }) {
+  async schoolSignCoach({ school_id, coach_id }) {
     // 查出这个学校下面的教练
-    const rList = await this.rcsModel.find({ school_id }).populate('coach_id', 'name');
-    const coachList = rList.map(i => i.coach_id);
-    console.log(coachList);
-    const lcList = await this.lessonCoachModel.find({ school_id }, { coach_id: 1, is_sign: 1 });
-    const midList = _.groupBy(lcList, 'coach_id');
+    const query = { school_id, coach_id };
+    const { year } = this.getPartsOfNow();
+    const yearEnd = `${year}-12-01`;
+    query.$and = [{ 'meta.createdAt': { $gte: `${year}-01-01 00:00:00` } }, { 'meta.createdAt': { $lte: `${year}-12-31 23:59:59` } }];
+    let monthList = this.getMonthList(12, yearEnd);
+    monthList = monthList.map(i => {
+      const start = moment(i).startOf('month').format('YYYY-MM-DD HH:mm:ss');
+      const end = moment(i).endOf('month').format('YYYY-MM-DD HH:mm:ss');
+      return [ start, end ];
+    });
+    let lcList = await this.lessonCoachModel.find(query, { coach_id: 1, is_sign: 1, meta: 1 });
+    if (lcList.length > 0) lcList = JSON.parse(JSON.stringify((lcList)));
     const arr = [];
-    for (const coach_id in midList) {
-      const l = midList[coach_id];
-      midList[coach_id] = _.groupBy(l, 'is_sign');
-      for (const is_sign in midList[coach_id]) {
-        midList[coach_id][is_sign] = midList[coach_id][is_sign].length;
-      }
-      const coach = coachList.find(f => ObjectId(f._id).equals(coach_id));
-      const obj = { coach: coach.name };
-      const csList = midList[coach_id];
+    for (const months of monthList) {
+      const s = _.head(months);
+      const l = _.last(months);
+      const m = moment(s).month() + 1;
+      const obj = { m };
+      const list = lcList.filter(f => moment(_.get(f, 'meta.createdAt')).isBetween(s, l, null, '[]'));
       let total = 0;
       let is_signed = 0;
-      for (const is_sign in csList) {
-        const d = csList[is_sign];
-        if (is_sign === '1') is_signed = d;
-        total += d;
+      for (const lc of list) {
+        if (lc.is_sign === '1') is_signed++;
+        total++;
       }
-      console.log(is_signed, total);
-      obj.value = `${_.floor(_.divide(is_signed, total) * 100)}%`;
+      obj.total = total;
+      obj.is_sign = is_signed;
+      obj.value = `${_.floor(_.divide(is_signed, total) * 100) || 0}%`;
       arr.push(obj);
     }
+
     return arr;
   }