|
@@ -13,6 +13,8 @@ class OrderDetailService extends CrudService {
|
|
|
this.orderModel = this.ctx.model.Trade.Order;
|
|
|
this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
|
|
|
this.userCouponModel = this.ctx.model.User.UserCoupon;
|
|
|
+ this.goodsRateModel = this.ctx.model.Shop.GoodsRate;
|
|
|
+ this.afterSaleModel = this.ctx.model.Trade.AfterSale;
|
|
|
this.tran = new Transaction();
|
|
|
}
|
|
|
async searchOrderTransport({ id }) {
|
|
@@ -129,24 +131,103 @@ class OrderDetailService extends CrudService {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
- async afterQuery(filter, data) {
|
|
|
- data = JSON.parse(JSON.stringify(data));
|
|
|
- for (const i of data) {
|
|
|
+
|
|
|
+ async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
|
|
|
+ // 处理排序
|
|
|
+ if (sort && _.isString(sort)) {
|
|
|
+ sort = { [sort]: desc ? -1 : 1 };
|
|
|
+ } else if (sort && _.isArray(sort)) {
|
|
|
+ sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
+ }
|
|
|
+ let condition = _.cloneDeep(filter);
|
|
|
+ condition = await this.beforeQuery(condition);
|
|
|
+ condition = this.dealFilter(condition);
|
|
|
+ // 过滤出ref字段
|
|
|
+ const pipline = [{ $sort: { 'meta.createdAt': -1 } }];
|
|
|
+ pipline.push({ $match: condition });
|
|
|
+ // 整理字段
|
|
|
+ // 店铺需要的字段
|
|
|
+ pipline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
|
|
|
+ pipline.push({
|
|
|
+ $lookup: {
|
|
|
+ from: 'shop',
|
|
|
+ localField: 'shop_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ as: 'shopInfo',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ pipline.push({ $addFields: { customer_id: { $toObjectId: '$customer' } } });
|
|
|
+ pipline.push({
|
|
|
+ $lookup: {
|
|
|
+ from: 'user',
|
|
|
+ localField: 'customer_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ as: 'customerInfo',
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ pipline.push({ $unwind: '$shopInfo' });
|
|
|
+ pipline.push({ $unwind: '$customerInfo' });
|
|
|
+ const lastProject = {
|
|
|
+ $project: {
|
|
|
+ _id: 1,
|
|
|
+ type: 1,
|
|
|
+ order: 1,
|
|
|
+ buy_time: 1,
|
|
|
+ pay_time: 1,
|
|
|
+ status: 1,
|
|
|
+ no: 1,
|
|
|
+ group: 1,
|
|
|
+ goods: {
|
|
|
+ _id: 1,
|
|
|
+ sell_money: 1,
|
|
|
+ freight: 1,
|
|
|
+ name: 1,
|
|
|
+ buy_num: 1,
|
|
|
+ group_config: 1,
|
|
|
+ goods: { name: 1, file: 1 },
|
|
|
+ file: 1,
|
|
|
+ },
|
|
|
+ shop: {
|
|
|
+ _id: '$shopInfo._id',
|
|
|
+ name: '$shopInfo.name',
|
|
|
+ },
|
|
|
+ customer: {
|
|
|
+ _id: '$customerInfo._id',
|
|
|
+ name: '$customerInfo.name',
|
|
|
+ },
|
|
|
+ },
|
|
|
+ };
|
|
|
+ pipline.push(lastProject);
|
|
|
+ const qPipline = _.cloneDeep(pipline);
|
|
|
+ if (parseInt(skip) >= 0) qPipline.push({ $skip: parseInt(skip) });
|
|
|
+ if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
|
|
|
+ const rs = await this.model.aggregate(qPipline);
|
|
|
+ const list = [];
|
|
|
+ for (const i of rs) {
|
|
|
const { goods } = i;
|
|
|
- const buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
|
|
|
- i.buy_num_total = buy_num_total;
|
|
|
- let discount = 0;
|
|
|
- const dd = _.get(i, 'total_detail.discount_detail', {});
|
|
|
- for (const uc_id in dd) {
|
|
|
- const obj = _.get(dd, uc_id, {});
|
|
|
- const values = Object.values(obj);
|
|
|
- if (!values.every(e => _.isNumber(e))) continue;
|
|
|
- const dm = values.reduce((p, n) => this.ctx.plus(p, n), 0);
|
|
|
- discount = this.ctx.plus(discount, dm);
|
|
|
+ const obj = _.cloneDeep(i);
|
|
|
+ const real_pay = this.ctx.service.util.orderDetail.computedRealPay(obj);
|
|
|
+ obj.real_pay = real_pay;
|
|
|
+ obj.buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
|
|
|
+ for (const og of obj.goods) {
|
|
|
+ const { file = [] } = og;
|
|
|
+ const gfile = _.get(og, 'goods.file', []);
|
|
|
+ const nf = [ ...file, ...gfile ];
|
|
|
+ const url = _.get(_.head(nf), 'url');
|
|
|
+ og.url = url;
|
|
|
+ delete og.file;
|
|
|
+ delete og.goods.file;
|
|
|
}
|
|
|
- i.real_pay = this.ctx.service.util.orderDetail.computedRealPay(i);
|
|
|
+ // 评价与售后
|
|
|
+ const rate = await this.goodsRateModel.findOne({ orderDetail: obj._id }, { _id: 1 });
|
|
|
+ obj.rate = _.get(rate, '_id');
|
|
|
+ const asum = await this.afterSaleModel.count({ orderDetail: obj._id });
|
|
|
+ obj.is_afterSale = asum > 0;
|
|
|
+ list.push(obj);
|
|
|
}
|
|
|
- return data;
|
|
|
+
|
|
|
+ return list;
|
|
|
}
|
|
|
}
|
|
|
|