statistics.js 7.1 KB

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