lrf 2 anos atrás
pai
commit
097a71c1c1
1 arquivos alterados com 52 adições e 6 exclusões
  1. 52 6
      app/service/view/order.js

+ 52 - 6
app/service/view/order.js

@@ -3,12 +3,15 @@ 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 = {}) {
@@ -47,8 +50,10 @@ class OrderService extends CrudService {
     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(
@@ -59,17 +64,58 @@ class OrderService extends CrudService {
             ),
           0
         );
-        i.buy_num_total = buy_num_total;
-        i.real_pay = _.get(i, 'pay.pay_money');
+        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' ]);
+            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);
-        i.real_pay = real_pay;
-        i.buy_num_total = goods.reduce((p, n) => this.ctx.plus(p, n.buy_num), 0);
+        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' ]);
+          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, total };
+    return { data: newArr, total };
   }
 }