count.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. return { yesterday, week, month, group };
  32. }
  33. /**
  34. * 统计团队推销的卡
  35. * @param {Object} data 查询条件
  36. * @param {String} method 查询方法:count数量/find详情
  37. */
  38. async group({ r_mobile, ...info }, method = 'count') {
  39. let group = await this.card[method]({ r_mobile, ...info });
  40. if (method !== 'count') {
  41. group = _.groupBy(group, 'set');
  42. if (!this.needDetail) {
  43. const result = {};
  44. // 如果需要详情,则把下面代码注释
  45. const keys = Object.keys(group);
  46. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  47. for (const key of keys) {
  48. const res = setList.find(f => ObjectId(f._id).equals(key));
  49. if (res) {
  50. result[`${res.title}`] = group[key].length;
  51. }
  52. }
  53. return result;
  54. }
  55. }
  56. return group;
  57. }
  58. /**
  59. * 统计昨天推销的卡
  60. * @param {Object} data 查询条件
  61. * @param {String} method 查询方法:count数量/find详情
  62. */
  63. async yesterday({ r_mobile, ...info }, method = 'count') {
  64. 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') } });
  65. if (method !== 'count') {
  66. const result = {};
  67. yesterday = _.groupBy(yesterday, 'set');
  68. if (!this.needDetail) {
  69. // 如果需要详情,则把下面代码注释
  70. const keys = Object.keys(yesterday);
  71. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  72. for (const key of keys) {
  73. const res = setList.find(f => ObjectId(f._id).equals(key));
  74. if (res) {
  75. result[`${res.title}`] = yesterday[key].length;
  76. }
  77. }
  78. }
  79. return result;
  80. }
  81. return yesterday;
  82. }
  83. /**
  84. * 统计本周推销的卡
  85. * @param {Object} data 查询条件
  86. * @param {String} method 查询方法:count数量/find详情
  87. */
  88. async week({ r_mobile, ...info }, method = 'count') {
  89. const wnum = moment().weekday();
  90. const ws = moment().subtract(wnum - 1, 'days').format('YYYY-MM-DD');
  91. const we = moment().add(7 - wnum, 'days').format('YYYY-MM-DD');
  92. let week = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ws, $lte: we } });
  93. if (method !== 'count') {
  94. const result = {};
  95. week = _.groupBy(week, 'set');
  96. if (!this.needDetail) {
  97. // 如果需要详情,则把下面代码注释
  98. const keys = Object.keys(week);
  99. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  100. for (const key of keys) {
  101. const res = setList.find(f => ObjectId(f._id).equals(key));
  102. if (res) {
  103. result[`${res.title}`] = week[key].length;
  104. }
  105. }
  106. }
  107. return result;
  108. }
  109. return week;
  110. }
  111. /**
  112. * 统计本周推销的卡
  113. * @param {Object} data 查询条件
  114. * @param {String} method 查询方法:count数量/find详情
  115. */
  116. async month({ r_mobile, ...info }, method = 'count') {
  117. const prefix = moment().format('YYYY-MM-');
  118. const ms = `${prefix}01`;
  119. const me = moment(ms).add(1, 'months').format('YYYY-MM-DD');
  120. let month = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ms, $lte: me } });
  121. if (method !== 'count') {
  122. const result = {};
  123. month = _.groupBy(month, 'set');
  124. if (!this.needDetail) {
  125. // 如果需要详情,则把下面代码注释
  126. const keys = Object.keys(month);
  127. const setList = await this.set.find({ _id: keys.map(k => ObjectId(k)) });
  128. for (const key of keys) {
  129. const res = setList.find(f => ObjectId(f._id).equals(key));
  130. if (res) {
  131. result[`${res.title}`] = month[key].length;
  132. }
  133. }
  134. }
  135. return result;
  136. }
  137. return month;
  138. }
  139. }
  140. module.exports = CountService;