123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const moment = require('moment');
- //
- class StatisticsService extends CrudService {
- constructor(ctx) {
- super(ctx, 'statistics');
- this.billModel = this.ctx.model.Business.Bill;
- this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
- this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
- this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
- }
- // 学校统计 教练出勤率
- async schoolSignCoach({ school_id }) {
- // 查出这个学校下面的教练
- const rList = await this.rcsModel.find({ school_id }).populate('coach_id', 'name');
- const coachList = rList.map(i => i.coach_id);
- const lcList = await this.lessonCoachModel.find({ school_id }, { coach_id: 1, is_sign: 1 });
- const midList = _.groupBy(lcList, 'coach_id');
- 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;
- }
- }
- console.log(midList);
- }
- // 学校统计:学员按岁数区间
- async schoolStudentAge({ school_id }) {
- // 年龄组
- const ageList = [
- [ null, 6 ],
- [ 7, 9 ],
- [ 10, 12 ],
- [ 12, 18 ],
- [ 19, 30 ],
- [ 31, null ],
- ];
- const data = [];
- let list = await this.rssModel.find({ school_id }).populate('student_id', 'age');
- if (list.length > 0)list = JSON.parse(JSON.stringify(list));
- for (const a of ageList) {
- const start = _.head(a);
- const end = _.last(a);
- const obj = {};
- if (start && end) obj.name = `${start}-${end}岁`;
- else if (!start) obj.name = `${end}岁以下`;
- else if (!end) obj.name = `${start}岁以上`;
- const l = list.filter(f => {
- const age = _.get(f, 'student_id.age');
- if (start && end) return age >= start && age <= end;
- else if (!start) return age <= end;
- else if (!end) return age >= start;
- return false;
- });
- obj.value = l.length;
- data.push(obj);
- }
- return data;
- }
- /**
- * 羽校总收入
- * 统计账单的 收入(类型为-1/-2) - 退款(至余额,2) 且 is_pay 为 1
- * m:当前这个月; 3m: 往前推3个月; 6m:往前推6个月; 1y:当前年
- * @param {Object} query 查询条件
- * @property {String} school_id 学校id
- * @property {String} time 时间 m:月;3m:3月;6m:6个月; 1y:1年
- */
- async schoolTotalIn({ school_id, time }) {
- assert(school_id, '缺少羽校信息');
- assert(time, '缺少时间范围');
- let query = { is_pay: '1', $or: [{ type: '-1' }, { type: '-2' }, { type: '2' }], school_id };
- const { year, month, lastDate } = this.getPartsOfNow();
- const timeQuery = (start, end) => ({ $and: [{ time: { $gte: start } }, { time: { $lte: end } }] });
- let xList;
- const yList = [];
- if (time === 'm') {
- // 这个月,按天查
- const start = `${year}-${month}-01`;
- const end = `${year}-${month}-${lastDate}`;
- query = { ...query, ...timeQuery(start, end) };
- xList = this.getEachDay(start, end);
- } else if ([ '3m', '6m' ].includes(time)) {
- const ms = _.head(time.split('')); // 月份数量 3/6/...
- xList = this.getMonthList(ms);
- const start = _.last(xList);
- const { year, month, lastDate } = this.getPartsOfNow(_.head(xList));
- const end = `${year}-${month}-${lastDate}`;
- query = { ...query, ...timeQuery(start, end) };
- } else if (time === '1y') {
- const { year } = this.getPartsOfNow();
- const start = `${year}-01-01`;
- const end = `${year}-12-31`;
- xList = this.getMonthList(12, end);
- query = { ...query, ...timeQuery(start, end) };
- }
- let billList = await this.billModel.find(query);
- if (billList.length > 0) billList = JSON.parse(JSON.stringify(billList));
- for (const i of billList) {
- const { time } = i;
- const date = this.getDate(time);
- i.date = date;
- }
- if (time === 'm') {
- for (const x of xList) {
- const date = x;
- const list = billList.filter(f => f.date === date);
- const inList = list.filter(f => f.type !== '2');
- const outList = list.filter(f => f.type === '2');
- const inTotal = inList.reduce((p, n) => p + (n.money || 0), 0);
- const outTotal = outList.reduce((p, n) => p + (n.money || 0), 0);
- const total = inTotal - outTotal;
- yList.push(total);
- }
- xList = xList.map(i => {
- const { date } = this.getPartsOfNow(i);
- return `${date}日`;
- });
- } else if ([ '3m', '6m', '1y' ].includes(time)) {
- for (const x of xList) {
- const start = x;
- const { year, month, lastDate } = this.getPartsOfNow(x);
- const end = `${year}-${month}-${lastDate}`;
- const list = billList.filter(f => moment(f.date).isBetween(start, end, null, '[]'));
- const inList = list.filter(f => f.type !== '2');
- const outList = list.filter(f => f.type === '2');
- const inTotal = inList.reduce((p, n) => p + (n.money || 0), 0);
- const outTotal = outList.reduce((p, n) => p + (n.money || 0), 0);
- const total = inTotal - outTotal;
- yList.unshift(total);
- }
- xList = xList.map(i => {
- const { month } = this.getPartsOfNow(i);
- return `${parseInt(month)}月`;
- });
- xList = _.reverse(xList);
- }
- return { x: xList, y: yList };
- }
- // 获取现在时间的各个部分 年月日时分秒 和 当月最后一天是几号
- getPartsOfNow(time = new Date()) {
- const year = moment(time).year();
- let month = moment(time).month() + 1;
- if (month < 10) month = `0${month}`;
- const date = moment(time).date();
- const hour = moment(time).hour();
- const minute = moment(time).minute();
- const second = moment(time).second();
- const lastDate = moment(`${year}-${month}-01`).add(1, 'months').subtract(1, 'days')
- .date();
- return { year, month, date, hour, minute, second, lastDate };
- }
- // 获取两个时间点内的每天
- getEachDay(start, end) {
- const arr = [];
- let i = 0;
- const addDay = (s, i) => moment(s).add(i, 'days').format('YYYY-MM-DD');
- while (!moment(addDay(start, i)).isAfter(end, 'day')) {
- arr.push(addDay(start, i));
- i++;
- }
- return arr;
- }
- // 根据数据获取日期
- getDate(time) {
- const year = moment(time).year();
- let month = moment(time).month() + 1;
- if (month < 10) month = `0${month}`;
- const date = moment(time).date();
- return `${year}-${month}-${date}`;
- }
- // 获取月份第一天的列表, 往前推 pushNum 个月
- getMonthList(pushNum, date) {
- if (!_.isNumber(pushNum)) pushNum = parseInt(pushNum);
- const arr = [];
- const { year, month } = this.getPartsOfNow(date);
- const sDate = `${year}-${month}-01`;
- for (let i = 0; i < pushNum; i++) {
- const { year, month } = this.getPartsOfNow(moment(sDate).subtract(i, 'months'));
- arr.push(`${year}-${month}-01`);
- }
- return arr;
- }
- }
- module.exports = StatisticsService;
|