|
@@ -16,6 +16,8 @@ class OrderService extends CrudService {
|
|
|
|
|
|
async allOrder(query = {}) {
|
|
|
const { skip, limit } = query;
|
|
|
+ const customer = _.get(this.ctx, 'user._id');
|
|
|
+ if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN);
|
|
|
const pipeline = [];
|
|
|
const step1 = [
|
|
|
{ $addFields: { order_id: { $toString: '$_id' } } },
|
|
@@ -34,7 +36,7 @@ class OrderService extends CrudService {
|
|
|
$project: {
|
|
|
data: {
|
|
|
$cond: {
|
|
|
- if: { $gt: [{ $size: '$orderDetails' }, 0 ] },
|
|
|
+ if: { $gt: [{ $size: '$orderDetails' }, 0] },
|
|
|
then: '$orderDetails',
|
|
|
else: '$$CURRENT',
|
|
|
},
|
|
@@ -45,11 +47,12 @@ class OrderService extends CrudService {
|
|
|
pipeline.push(...step2);
|
|
|
pipeline.push({ $unwind: '$data' });
|
|
|
pipeline.push({ $replaceRoot: { newRoot: '$data' } });
|
|
|
+ pipeline.push({ $match: { customer } });
|
|
|
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 tr = await this.orderModel.aggregate([...pipeline, { $count: 'total' }]);
|
|
|
const newArr = [];
|
|
|
for (const i of data) {
|
|
|
const { pay, goods } = i;
|
|
@@ -66,44 +69,44 @@ class OrderService extends CrudService {
|
|
|
);
|
|
|
obj.buy_num_total = buy_num_total;
|
|
|
obj.real_pay = _.get(i, 'pay.pay_money');
|
|
|
- const shopData = _.pick(_.head(goods), [ 'shop', 'shop_name' ]);
|
|
|
+ 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 spec = _.pick(sg, ['_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price']);
|
|
|
if (!spec.price) spec.price = _.get(sg, 'sell_money');
|
|
|
- const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
|
|
|
+ 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' ]);
|
|
|
+ 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 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 spec = _.pick(sg, ['_id', 'name', 'file', 'act', 'buy_num', 'gift', 'price', 'freight', 'sp_price']);
|
|
|
if (!spec.price) spec.price = _.get(sg, 'sell_money');
|
|
|
- const goods = _.pick(sg.goods, [ '_id', 'name', 'file' ]);
|
|
|
+ const goods = _.pick(sg.goods, ['_id', 'name', 'file']);
|
|
|
spec.goods = goods;
|
|
|
|
|
|
- const r = afterSale.find(f => ObjectId(_.get(f, 'goods._id')).equals(sg._id));
|
|
|
+ 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));
|
|
|
+ const r2 = rate.find((f) => ObjectId(_.get(f, 'goodsSpec')).equals(sg._id));
|
|
|
if (r2) {
|
|
|
spec.is_rate = true;
|
|
|
spec.rate = r2._id;
|