bill.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // 对账
  7. class BillService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'bill');
  10. this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
  11. this.afterSaleModel = this.ctx.model.Trade.AfterSale;
  12. }
  13. /**
  14. * 生成店铺对账单
  15. ** 根据店铺id,开始时间和结束时间,形成对账单
  16. * @param {Object} query 查询条件
  17. */
  18. async getBill(query) {
  19. const { shop, start, end } = query;
  20. assert(start, '缺少开始时间');
  21. assert(end, '缺少结束时间');
  22. const pipline = [];
  23. // TODO: 商店过滤
  24. // 时间过滤
  25. const q = { $match: { $and: [{ pay_time: { $gte: start } }, { pay_time: { $lte: end } }] } };
  26. pipline.push(q);
  27. // #region 整理数据
  28. // 整理最外层的orderDetail,没用的裁掉
  29. const $project = {
  30. type: 1,
  31. total_detail: 1,
  32. no: 1,
  33. goods: {
  34. _id: 1,
  35. sell_money: 1,
  36. buy_num: 1,
  37. freight: 1,
  38. group_config: { money: 1 },
  39. name: 1,
  40. goods: { name: 1 },
  41. },
  42. };
  43. pipline.push({ $project });
  44. // 按规格平铺开
  45. pipline.push({ $unwind: '$goods' });
  46. // #endregion
  47. const orderList = await this.orderDetailModel.aggregate(pipline);
  48. return orderList;
  49. }
  50. }
  51. module.exports = BillService;