lrf 2 years ago
parent
commit
4c1855deea
3 changed files with 68 additions and 2 deletions
  1. 8 0
      app/controller/config/.statistics.js.js
  2. 59 2
      app/service/statistics.js
  3. 1 0
      app/z_router/statistics.js

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

@@ -22,4 +22,12 @@ module.exports = {
       },
     },
   },
+  schoolCoachIn: {
+    parameters: {
+      query: {
+        school_id: 'school_id',
+        coach_id: 'coach_id',
+      },
+    },
+  },
 };

+ 59 - 2
app/service/statistics.js

@@ -14,9 +14,65 @@ class StatisticsService extends CrudService {
     this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
     this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
     this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
+    this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
+    this.lessonModel = this.ctx.model.Business.Lesson;
   }
+  // 学校统计,教练收入
+  async schoolCoachIn({ school_id, coach_id }) {
+    assert(school_id, '缺少学校信息');
+    assert(coach_id, '缺少教练信息');
+    // 教练收入分为私教课和公开课,公开课是根据给教练设置的钱计算,
+    const query = { school_id, coach_id };
+    const { year } = this.getPartsOfNow();
+    const yearEnd = `${year}-12-01`;
+    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 ];
+    });
+    query.$and = [{ 'meta.createdAt': { $gte: `${year}-01-01 00:00:00` } }, { 'meta.createdAt': { $lte: `${year}-12-31 23:59:59` } }];
+    const lcList = await this.lessonCoachModel.find(query).populate('lesson_id');
+    const lessonList = lcList.map(i => i.lesson_id).map(i => _.pick(i, [ '_id', 'type', 'title', 'meta' ]));
+    const arr = [];
+    for (const months of monthList) {
+      const s = _.head(months);
+      const l = _.last(months);
+      const m = moment(s).month() + 1;
+      const obj = { m, total: 0 };
+      const list = lessonList.filter(f => moment(_.get(f, 'meta.createdAt')).isBetween(s, l, null, '[]'));
+      // console.log(m);
+      const privateLesson = list.filter(f => f.type === '1');
+      const publicLesson = list.filter(f => f.type === '0');
+      // 先算公开课
+      for (const pl of publicLesson) {
+        // 找到该教练的价格,这节公开课的人头数 算出教练应得金额
+        const { _id: lesson_id } = pl;
+        const lessonCoach = lcList.find(f => ObjectId(f.lesson_id._id).equals(lesson_id));
+        if (!lessonCoach) continue;
+        const studentNumber = await this.lessonStudentModel.count({ lesson_id, is_pay: '1' });
+        const { money = 0 } = lessonCoach;
+        const total = money * studentNumber;
+        obj.total += total;
+      }
+      for (const pl of privateLesson) {
+        // 私教课,lessonStudent中这个课学生交的钱
+        const { _id: lesson_id } = pl;
+        const studentList = await this.lessonStudentModel.find({ lesson_id, is_pay: '1' });
+        const total = studentList.reduce((p, n) => p + (n.money || 0), 0);
+        obj.total += total;
+      }
+      arr.push(obj);
+    }
+    return arr;
+
+  }
+
+
   // 学校统计 教练出勤率
   async schoolSignCoach({ school_id, coach_id }) {
+    assert(school_id, '缺少学校信息');
+    assert(coach_id, '缺少教练信息');
     // 查出这个学校下面的教练
     const query = { school_id, coach_id };
     const { year } = this.getPartsOfNow();
@@ -55,6 +111,7 @@ class StatisticsService extends CrudService {
 
   // 学校统计:学员按岁数区间
   async schoolStudentAge({ school_id }) {
+    assert(school_id, '缺少学校信息');
     // 年龄组
     const ageList = [
       [ null, 6 ],
@@ -125,7 +182,7 @@ class StatisticsService extends CrudService {
       query = { ...query, ...timeQuery(start, end) };
     }
 
-    let billList = await this.billModel.find(query);
+    let billList = await this.billModel.find(query, { pay_for: 1, from_id: 1, type: 1, money: 1, time: 1, payer_id: 1 });
     if (billList.length > 0) billList = JSON.parse(JSON.stringify(billList));
     for (const i of billList) {
       const { time } = i;
@@ -166,7 +223,7 @@ class StatisticsService extends CrudService {
       });
       xList = _.reverse(xList);
     }
-
+    console.log('line 189 in function:');
     return { x: xList, y: yList };
   }
   // 获取现在时间的各个部分 年月日时分秒 和 当月最后一天是几号

+ 1 - 0
app/z_router/statistics.js

@@ -7,6 +7,7 @@ const rkey = 'statistics';
 const ckey = 'statistics';
 const keyZh = '统计';
 const routes = [
+  { method: 'get', path: `${rkey}/schoolCoachIn`, controller: `${ckey}.schoolCoachIn`, name: `${ckey}schoolCoachIn`, zh: `${keyZh}-羽校,教练收入` },
   { method: 'get', path: `${rkey}/schoolSignCoach`, controller: `${ckey}.schoolSignCoach`, name: `${ckey}schoolSignCoach`, zh: `${keyZh}-羽校,教练出勤率` },
   { method: 'get', path: `${rkey}/schoolStudentAge`, controller: `${ckey}.schoolStudentAge`, name: `${ckey}schoolStudentAge`, zh: `${keyZh}-羽校,学员区间` },
   { method: 'get', path: `${rkey}/schoolTotalIn`, controller: `${ckey}.schoolTotalIn`, name: `${ckey}schoolTotalIn`, zh: `${keyZh}-羽校总收入` },