statistics.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class StatisticsService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'statistics');
  11. this.billModel = this.ctx.model.Business.Bill;
  12. this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
  13. this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
  14. this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
  15. }
  16. // 学校统计 教练出勤率
  17. async schoolSignCoach({ school_id }) {
  18. // 查出这个学校下面的教练
  19. const rList = await this.rcsModel.find({ school_id }).populate('coach_id', 'name');
  20. const coachList = rList.map(i => i.coach_id);
  21. const lcList = await this.lessonCoachModel.find({ school_id }, { coach_id: 1, is_sign: 1 });
  22. }
  23. // 学校统计:学员按岁数区间
  24. async schoolStudentAge({ school_id }) {
  25. // 年龄组
  26. const ageList = [
  27. [ null, 6 ],
  28. [ 7, 9 ],
  29. [ 10, 12 ],
  30. [ 12, 18 ],
  31. [ 19, 30 ],
  32. [ 31, null ],
  33. ];
  34. const data = [];
  35. let list = await this.rssModel.find({ school_id }).populate('student_id', 'age');
  36. if (list.length > 0)list = JSON.parse(JSON.stringify(list));
  37. for (const a of ageList) {
  38. const start = _.head(a);
  39. const end = _.last(a);
  40. const obj = {};
  41. if (start && end) obj.name = `${start}-${end}岁`;
  42. else if (!start) obj.name = `${end}岁以下`;
  43. else if (!end) obj.name = `${start}岁以上`;
  44. const l = list.filter(f => {
  45. const age = _.get(f, 'student_id.age');
  46. if (start && end) return age >= start && age <= end;
  47. else if (!start) return age <= end;
  48. else if (!end) return age >= start;
  49. return false;
  50. });
  51. obj.value = l.length;
  52. data.push(obj);
  53. }
  54. return data;
  55. }
  56. /**
  57. * 羽校总收入
  58. * 统计账单的 收入(类型为-1/-2) - 退款(至余额,2) 且 is_pay 为 1
  59. * m:当前这个月; 3m: 往前推3个月; 6m:往前推6个月; 1y:当前年
  60. * @param {Object} query 查询条件
  61. * @property {String} school_id 学校id
  62. * @property {String} time 时间 m:月;3m:3月;6m:6个月; 1y:1年
  63. */
  64. async schoolTotalIn({ school_id, time }) {
  65. assert(school_id, '缺少羽校信息');
  66. assert(time, '缺少时间范围');
  67. let query = { is_pay: '1', $or: [{ type: '-1' }, { type: '-2' }, { type: '2' }], school_id };
  68. const { year, month, lastDate } = this.getPartsOfNow();
  69. const timeQuery = (start, end) => ({ $and: [{ time: { $gte: start } }, { time: { $lte: end } }] });
  70. let xList;
  71. const yList = [];
  72. if (time === 'm') {
  73. // 这个月,按天查
  74. const start = `${year}-${month}-01`;
  75. const end = `${year}-${month}-${lastDate}`;
  76. query = { ...query, ...timeQuery(start, end) };
  77. xList = this.getEachDay(start, end);
  78. } else if ([ '3m', '6m' ].includes(time)) {
  79. const ms = _.head(time.split('')); // 月份数量 3/6/...
  80. xList = this.getMonthList(ms);
  81. const start = _.last(xList);
  82. const { year, month, lastDate } = this.getPartsOfNow(_.head(xList));
  83. const end = `${year}-${month}-${lastDate}`;
  84. query = { ...query, ...timeQuery(start, end) };
  85. } else if (time === '1y') {
  86. const { year } = this.getPartsOfNow();
  87. const start = `${year}-01-01`;
  88. const end = `${year}-12-31`;
  89. xList = this.getMonthList(12, end);
  90. query = { ...query, ...timeQuery(start, end) };
  91. }
  92. let billList = await this.billModel.find(query);
  93. if (billList.length > 0) billList = JSON.parse(JSON.stringify(billList));
  94. for (const i of billList) {
  95. const { time } = i;
  96. const date = this.getDate(time);
  97. i.date = date;
  98. }
  99. if (time === 'm') {
  100. for (const x of xList) {
  101. const date = x;
  102. const list = billList.filter(f => f.date === date);
  103. const inList = list.filter(f => f.type !== '2');
  104. const outList = list.filter(f => f.type === '2');
  105. const inTotal = inList.reduce((p, n) => p + (n.money || 0), 0);
  106. const outTotal = outList.reduce((p, n) => p + (n.money || 0), 0);
  107. const total = inTotal - outTotal;
  108. yList.push(total);
  109. }
  110. xList = xList.map(i => {
  111. const { date } = this.getPartsOfNow(i);
  112. return date;
  113. });
  114. } else if ([ '3m', '6m', '1y' ].includes(time)) {
  115. for (const x of xList) {
  116. const start = x;
  117. const { year, month, lastDate } = this.getPartsOfNow(x);
  118. const end = `${year}-${month}-${lastDate}`;
  119. const list = billList.filter(f => moment(f.date).isBetween(start, end, null, '[]'));
  120. const inList = list.filter(f => f.type !== '2');
  121. const outList = list.filter(f => f.type === '2');
  122. const inTotal = inList.reduce((p, n) => p + (n.money || 0), 0);
  123. const outTotal = outList.reduce((p, n) => p + (n.money || 0), 0);
  124. const total = inTotal - outTotal;
  125. yList.unshift(total);
  126. }
  127. xList = xList.map(i => {
  128. const { month } = this.getPartsOfNow(i);
  129. return parseInt(month);
  130. });
  131. xList = _.reverse(xList);
  132. }
  133. return { x: xList, y: yList };
  134. }
  135. // 获取现在时间的各个部分 年月日时分秒 和 当月最后一天是几号
  136. getPartsOfNow(time = new Date()) {
  137. const year = moment(time).year();
  138. let month = moment(time).month() + 1;
  139. if (month < 10) month = `0${month}`;
  140. const date = moment(time).date();
  141. const hour = moment(time).hour();
  142. const minute = moment(time).minute();
  143. const second = moment(time).second();
  144. const lastDate = moment(`${year}-${month}-01`).add(1, 'months').subtract(1, 'days')
  145. .date();
  146. return { year, month, date, hour, minute, second, lastDate };
  147. }
  148. // 获取两个时间点内的每天
  149. getEachDay(start, end) {
  150. const arr = [];
  151. let i = 0;
  152. const addDay = (s, i) => moment(s).add(i, 'days').format('YYYY-MM-DD');
  153. while (!moment(addDay(start, i)).isAfter(end, 'day')) {
  154. arr.push(addDay(start, i));
  155. i++;
  156. }
  157. return arr;
  158. }
  159. // 根据数据获取日期
  160. getDate(time) {
  161. const year = moment(time).year();
  162. let month = moment(time).month() + 1;
  163. if (month < 10) month = `0${month}`;
  164. const date = moment(time).date();
  165. return `${year}-${month}-${date}`;
  166. }
  167. // 获取月份第一天的列表, 往前推 pushNum 个月
  168. getMonthList(pushNum, date) {
  169. if (!_.isNumber(pushNum)) pushNum = parseInt(pushNum);
  170. const arr = [];
  171. const { year, month } = this.getPartsOfNow(date);
  172. const sDate = `${year}-${month}-01`;
  173. for (let i = 0; i < pushNum; i++) {
  174. const { year, month } = this.getPartsOfNow(moment(sDate).subtract(i, 'months'));
  175. arr.push(`${year}-${month}-01`);
  176. }
  177. return arr;
  178. }
  179. }
  180. module.exports = StatisticsService;