count.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const moment = require('moment');
  5. // 统计
  6. class CountService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx, 'count');
  9. this.model = this.ctx.model.Count;
  10. this.card = this.ctx.model.Card;
  11. }
  12. async index(query) {
  13. const { id } = query;
  14. if (!id) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未接收到用户信息');
  15. const user = await this.card.findById(id);
  16. const { mobile: r_mobile } = user;
  17. // 昨日新增
  18. // day-1 00:00:00 - day-1 23:59:59
  19. const yesterday = await this.yesterday({ r_mobile });
  20. // 本周新增
  21. const week = await this.week({ r_mobile });
  22. // 当月新增
  23. const month = await this.month({ r_mobile });
  24. // 团队统计
  25. const group = await this.group({ r_mobile });
  26. console.log(`group:${group}`);
  27. return { yesterday, week, month, group };
  28. }
  29. /**
  30. * 统计团队推销的卡
  31. * @param {Object} data 查询条件
  32. * @param {String} method 查询方法:count数量/find详情
  33. */
  34. async group({ r_mobile, ...info }, method = 'count') {
  35. const group = await this.card[method]({ r_mobile, ...info });
  36. return group;
  37. }
  38. /**
  39. * 统计昨天推销的卡
  40. * @param {Object} data 查询条件
  41. * @param {String} method 查询方法:count数量/find详情
  42. */
  43. async yesterday({ r_mobile, ...info }, method = 'count') {
  44. const 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') } });
  45. console.log(`yesterday:${yesterday}`);
  46. return yesterday;
  47. }
  48. /**
  49. * 统计本周推销的卡
  50. * @param {Object} data 查询条件
  51. * @param {String} method 查询方法:count数量/find详情
  52. */
  53. async week({ r_mobile, ...info }, method = 'count') {
  54. const wnum = moment().week();
  55. const ws = moment().subtract(wnum, 'days').format('YYYY-MM-DD');
  56. const we = moment().add(7 - wnum, 'days').format('YYYY-MM-DD');
  57. const week = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ws, $lte: we } });
  58. console.log(`week:${week}`);
  59. return week;
  60. }
  61. /**
  62. * 统计本周推销的卡
  63. * @param {Object} data 查询条件
  64. * @param {String} method 查询方法:count数量/find详情
  65. */
  66. async month({ r_mobile, ...info }, method = 'count') {
  67. const prefix = moment().format('YYYY-MM-');
  68. const ms = `${prefix}01`;
  69. const me = moment(ms).add(1, 'months').format('YYYY-MM-DD');
  70. const month = await this.card[method]({ r_mobile, ...info, create_time: { $gte: ms, $lte: me } });
  71. console.log(`month:${month}`);
  72. return month;
  73. }
  74. }
  75. module.exports = CountService;