123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- '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');
- const { ObjectId } = require('mongoose').Types;
- //
- class OrderService extends CrudService {
- constructor(ctx) {
- super(ctx, 'order');
- this.orderModel = this.ctx.model.Trade.Order;
- this.shopModel = this.ctx.model.Shop.Shop;
- this.goodsRateModel = this.ctx.model.Shop.GoodsRate;
- this.afterSaleModel = this.ctx.model.Trade.AfterSale;
- }
- async allOrder(query = {}) {
- const { skip, limit } = query;
- const pipeline = [];
- const step1 = [
- { $addFields: { order_id: { $toString: '$_id' } } },
- {
- $lookup: {
- from: 'orderDetail',
- localField: 'order_id',
- foreignField: 'order',
- as: 'orderDetails',
- },
- },
- ];
- pipeline.push(...step1);
- const step2 = [
- {
- $project: {
- data: {
- $cond: {
- if: { $gt: [{ $size: '$orderDetails' }, 0 ] },
- then: '$orderDetails',
- else: '$$CURRENT',
- },
- },
- },
- },
- ];
- pipeline.push(...step2);
- pipeline.push({ $unwind: '$data' });
- pipeline.push({ $replaceRoot: { newRoot: '$data' } });
- const qp = _.cloneDeep(pipeline);
- qp.push({ $sort: { 'meta.createdAt': -1 } });
- if (skip && limit) qp.push({ $skip: parseInt(skip) }, { $limit: parseInt(limit) });
- const data = await this.orderModel.aggregate(qp);
- const tr = await this.orderModel.aggregate([ ...pipeline, { $count: 'total' }]);
- const newArr = [];
- for (const i of data) {
- const { pay, goods } = i;
- let obj = {};
- if (pay) {
- // 这个是order表的数据,需要用order的计算方式
- const buy_num_total = goods.reduce(
- (p, n) =>
- this.ctx.plus(
- p,
- n.goods.reduce((np, ng) => this.ctx.plus(np, ng.buy_num), 0)
- ),
- 0
- );
- obj.buy_num_total = buy_num_total;
- obj.real_pay = _.get(i, 'pay.pay_money');
- const shopData = _.pick(_.head(goods), [ 'shop', 'shop_name' ]);
- obj.shop = { _id: shopData.shop, name: shopData.shop_name };
- const specs = [];
- for (const shop of goods) {
- const { goods: specGoods } = shop;
- for (const sg of specGoods) {
- const spec = _.pick(sg, [ '_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price' ]);
- const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
- spec.goods = goods;
- specs.push(spec);
- }
- }
- obj.spec = specs;
- const others = _.pick(i, [ '_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type' ]);
- obj = { ...obj, ...others, is_order: true };
- } else {
- // 是orderDetail表的数据,需要用orderDetail的计算方式
- const real_pay = this.ctx.service.util.orderDetail.computedRealPay(i);
- obj.real_pay = real_pay;
- obj.buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
- const others = _.pick(i, [ '_id', 'status', 'is_afterSale', 'is_rate', 'price', 'total_detail', 'type', 'transport', 'transport_type' ]);
- const shop = await this.shopModel.findById(i.shop, { name: 1 }).lean();
- obj.shop = shop;
- const afterSale = await this.afterSaleModel.find({ order_detail: i._id });
- const rate = await this.goodsRateModel.find({ orderDetail: i._id });
- const specs = [];
- for (const sg of goods) {
- const spec = _.pick(sg, [ '_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price' ]);
- const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
- spec.goods = goods;
- const r = afterSale.find(f => ObjectId(_.get(f, 'goods._id')).equals(sg._id));
- if (r) spec.is_afterSale = true;
- else spec.is_afterSale = false;
- const r2 = rate.find(f => ObjectId(_.get(f, 'goodsSpec')).equals(sg._id));
- if (r2) {
- spec.is_rate = true;
- spec.rate = r2._id;
- } else spec.is_rate = false;
- specs.push(spec);
- }
- obj.spec = specs;
- obj = { ...obj, ...others, is_order: false };
- }
- newArr.push(obj);
- }
- const total = _.get(_.head(tr), 'total', 0);
- return { data: newArr, total };
- }
- }
- module.exports = OrderService;
|