123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- // 对账
- class BillService extends CrudService {
- constructor(ctx) {
- super(ctx, 'bill');
- this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
- this.afterSaleModel = this.ctx.model.Trade.AfterSale;
- }
- /**
- * 生成店铺对账单
- ** 根据店铺id,开始时间和结束时间,形成对账单
- * @param {Object} query 查询条件
- */
- async getBill(query) {
- const { shop, start, end } = query;
- assert(start, '缺少开始时间');
- assert(end, '缺少结束时间');
- const pipline = [];
- // TODO: 商店过滤
- // 时间过滤
- const q = { $match: { $and: [{ pay_time: { $gte: start } }, { pay_time: { $lte: end } }] } };
- pipline.push(q);
- // #region 整理数据
- // 整理最外层的orderDetail,没用的裁掉
- const $project = {
- type: 1,
- total_detail: 1,
- no: 1,
- goods: {
- _id: 1,
- sell_money: 1,
- buy_num: 1,
- freight: 1,
- group_config: { money: 1 },
- name: 1,
- goods: { name: 1 },
- },
- };
- pipline.push({ $project });
- // 按规格平铺开
- pipline.push({ $unwind: '$goods' });
- // #endregion
- const orderList = await this.orderDetailModel.aggregate(pipline);
- return orderList;
- }
- }
- module.exports = BillService;
|