lrf il y a 2 ans
Parent
commit
2686a7e607
2 fichiers modifiés avec 64 ajouts et 23 suppressions
  1. 1 0
      app/service/trade/orderDetail.js
  2. 63 23
      app/service/view/order.js

+ 1 - 0
app/service/trade/orderDetail.js

@@ -165,6 +165,7 @@ class OrderDetailService extends CrudService {
     const { populate } = this.getRefMods();
     let res = await this.model.findOne(filter).populate(populate).lean();
     res = JSON.parse(JSON.stringify(res));
+    if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
     // 找售后和评论
     const afterSale = await this.afterSaleModel.find({ order_detail: res._id });
     const rate = await this.goodsRateModel.find({ orderDetail: res._id });

+ 63 - 23
app/service/view/order.js

@@ -36,7 +36,7 @@ class OrderService extends CrudService {
         $project: {
           data: {
             $cond: {
-              if: { $gt: [{ $size: '$orderDetails' }, 0] },
+              if: { $gt: [{ $size: '$orderDetails' }, 0 ] },
               then: '$orderDetails',
               else: '$$CURRENT',
             },
@@ -52,7 +52,7 @@ class OrderService extends CrudService {
     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;
@@ -69,50 +69,90 @@ 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']);
-            if (!spec.price) spec.price = _.get(sg, 'sell_money');
-            const goods = _.pick(sg.goods, ['_id', 'name', 'file']);
-            spec.goods = goods;
-            specs.push(spec);
+            const { is_set = '1' } = sg;
+            if (is_set === '1') {
+              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' ]);
+              spec.goods = goods;
+              specs.push(spec);
+            } else {
+              const goods = _.get(sg, 'goods', []);
+              const obj = _.omit(sg, [ 'goods', 'meta' ]);
+              const newGoods = goods.map(i => {
+                const ngo = {
+                  goods: { name: _.get(i, 'goods.name'), file: _.get(i, 'goods.file') },
+                  spec: { name: _.get(i, 'spec.name'), file: _.get(i, 'spec.file') },
+                  set_num: _.get(i, 'set_num'),
+                };
+                return ngo;
+              });
+              obj.goods = newGoods;
+              specs.push(obj);
+            }
           }
         }
         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']);
-          if (!spec.price) spec.price = _.get(sg, 'sell_money');
-          const goods = _.pick(sg.goods, ['_id', 'name', 'file']);
-          spec.goods = goods;
+          const { is_set = '1' } = sg;
+          if (is_set === '1') {
+            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' ]);
+            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 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;
+            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);
+            specs.push(spec);
+          } else {
+            const goods = _.get(sg, 'goods', []);
+            const obj = _.omit(sg, [ 'goods', 'meta' ]);
+            const newGoods = goods.map(i => {
+              const ngo = {
+                goods: { name: _.get(i, 'goods.name'), file: _.get(i, 'goods.file') },
+                spec: { name: _.get(i, 'spec.name'), file: _.get(i, 'spec.file') },
+                set_num: _.get(i, 'set_num'),
+              };
+              const r = afterSale.find(f => _.get(f, 'goods._id') === _.get(i, 'spec._id'));
+              if (r) ngo.is_afterSale = true;
+              else ngo.is_afterSale = false;
+              const r2 = rate.find(f => _.get(f, 'goodsSpec') === _.get(i, 'spec._id'));
+              if (r2) {
+                ngo.is_rate = true;
+                ngo.rate = r2._id;
+              } else ngo.is_rate = false;
+              return ngo;
+            });
+            obj.goods = newGoods;
+            specs.push(obj);
+          }
         }
         obj.spec = specs;
         obj = { ...obj, ...others, is_order: false };