count.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const moment = require('moment');
  6. const _ = require('lodash');
  7. // 统计
  8. class CountService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'count');
  11. this.model = this.ctx.model.Count;
  12. this.card = this.ctx.model.Card;
  13. this.set = this.ctx.model.Set;
  14. // group,yesterday,week,month查询是否需要详细信息
  15. this.needDetail = false;
  16. }
  17. async index(query) {
  18. const { mobile } = query;
  19. if (!mobile) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未接收到用户信息');
  20. const user = await this.card.findOne({ mobile });
  21. const { mobile: r_mobile } = user;
  22. // 昨日新增
  23. // day-1 00:00:00 - day-1 23:59:59
  24. const yesterday = await this.yesterday({ r_mobile });
  25. // 本周新增
  26. const week = await this.week({ r_mobile });
  27. // 当月新增
  28. const month = await this.month({ r_mobile });
  29. // 团队统计
  30. const group = await this.group({ r_mobile });
  31. console.log(`group:${group}`);
  32. return { yesterday, week, month, group };
  33. }
  34. /**
  35. * 统计团队推销的卡
  36. * @param {Object} data 查询条件
  37. * @param {String} method 查询方法:count数量/find详情
  38. */
  39. async group({ r_mobile, ...info }, method = 'count') {
  40. let group = await this.card[method]({ r_mobile, ...info });
  41. if (method !== 'count') {
  42. group = _.groupBy(group, 'set');
  43. if (!this.needDetail) {
  44. const result = {};
  45. // 如果需要详情,则把下面代码注释
  46. const keys = Object.keys(group);
  47. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  48. for (const key of keys) {
  49. const res = setList.find(f => ObjectId(f._id).equals(key));
  50. if (res) {
  51. result[`${res.title}`] = group[key].length;
  52. }
  53. }
  54. return result;
  55. }
  56. }
  57. return group;
  58. }
  59. /**
  60. * 统计昨天推销的卡
  61. * @param {Object} data 查询条件
  62. * @param {String} method 查询方法:count数量/find详情
  63. */
  64. async yesterday({ r_mobile, ...info }, method = 'count') {
  65. let yesterday = await this.card[method]({ r_mobile, ...info, create_time: { $gte: moment().subtract(1, 'days').format('YYYY-MM-DD'), $lte: moment().format('YYYY-MM-DD') } });
  66. if (method !== 'count') {
  67. const result = {};
  68. yesterday = _.groupBy(yesterday, 'set');
  69. if (!this.needDetail) {
  70. // 如果需要详情,则把下面代码注释
  71. const keys = Object.keys(yesterday);
  72. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  73. for (const key of keys) {
  74. const res = setList.find(f => ObjectId(f._id).equals(key));
  75. if (res) {
  76. result[`${res.title}`] = yesterday[key].length;
  77. }
  78. }
  79. }
  80. return result;
  81. }
  82. return yesterday;
  83. }
  84. /**
  85. * 统计本周推销的卡
  86. * @param {Object} data 查询条件
  87. * @param {String} method 查询方法:count数量/find详情
  88. */
  89. async week({ r_mobile, ...info }, method = 'count') {
  90. const wnum = moment().week();
  91. const ws = moment().subtract(wnum, 'days').format('YYYY-MM-DD');
  92. const we = moment().add(7 - wnum, 'days').format('YYYY-MM-DD');
  93. let week = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ws, $lte: we } });
  94. if (method !== 'count') {
  95. const result = {};
  96. week = _.groupBy(week, 'set');
  97. if (!this.needDetail) {
  98. // 如果需要详情,则把下面代码注释
  99. const keys = Object.keys(week);
  100. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  101. for (const key of keys) {
  102. const res = setList.find(f => ObjectId(f._id).equals(key));
  103. if (res) {
  104. result[`${res.title}`] = week[key].length;
  105. }
  106. }
  107. }
  108. return result;
  109. }
  110. return week;
  111. }
  112. /**
  113. * 统计本周推销的卡
  114. * @param {Object} data 查询条件
  115. * @param {String} method 查询方法:count数量/find详情
  116. */
  117. async month({ r_mobile, ...info }, method = 'count') {
  118. const prefix = moment().format('YYYY-MM-');
  119. const ms = `${prefix}01`;
  120. const me = moment(ms).add(1, 'months').format('YYYY-MM-DD');
  121. let month = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ms, $lte: me } });
  122. if (method !== 'count') {
  123. const result = {};
  124. month = _.groupBy(month, 'set');
  125. if (!this.needDetail) {
  126. // 如果需要详情,则把下面代码注释
  127. const keys = Object.keys(month);
  128. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  129. for (const key of keys) {
  130. const res = setList.find(f => ObjectId(f._id).equals(key));
  131. if (res) {
  132. result[`${res.title}`] = month[key].length;
  133. }
  134. }
  135. }
  136. return result;
  137. }
  138. return month;
  139. }
  140. }
  141. module.exports = CountService;