lrf vor 2 Jahren
Ursprung
Commit
d98af8c049

+ 1 - 4
app/controller/util.js

@@ -12,20 +12,17 @@ class UtilController extends Controller {
     this.bodyObject = this.ctx.request.body;
     this.service = this.ctx.service.util;
     this.tradeService = this.ctx.service.util.trade;
-
   }
   async util() {
-    console.log('line 18 in function:');
+    await this.ctx.service.view.order.allOrder();
     this.ctx.ok();
   }
 
-
   async crk() {
     const key = await this.ctx.service.util.rk.crk();
     this.ctx.ok({ data: key });
   }
 
-
   /**
    * 查询是否可以购买
    */

+ 17 - 0
app/controller/view/order.js

@@ -0,0 +1,17 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+//
+class OrderController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.view.order;
+  }
+
+  async allOrder() {
+    const data = await this.service.allOrder(this.ctx.query);
+    this.ctx.ok(data);
+  }
+}
+module.exports = CrudController(OrderController, {});

+ 76 - 0
app/service/view/order.js

@@ -0,0 +1,76 @@
+'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');
+
+//
+class OrderService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'order');
+    this.orderModel = this.ctx.model.Trade.Order;
+  }
+
+  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' }]);
+    for (const i of data) {
+      const { pay, goods } = i;
+      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
+        );
+        i.buy_num_total = buy_num_total;
+        i.real_pay = _.get(i, 'pay.pay_money');
+      } 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);
+      }
+    }
+    const total = _.get(_.head(tr), 'total', 0);
+    return { data, total };
+  }
+}
+
+module.exports = OrderService;

+ 1 - 0
app/z_router/view/index.js

@@ -5,4 +5,5 @@ module.exports = app => {
   require('./goods')(app); // 商品视图
   require('./shop')(app); // 店铺视图
   require('./user')(app); // 用户视图
+  require('./order')(app); // 订单视图
 };

+ 13 - 0
app/z_router/view/order.js

@@ -0,0 +1,13 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'viewOrder';
+const ckey = 'view.order';
+const keyZh = '订单相关视图';
+const routes = [{ method: 'get', path: `${rkey}`, controller: `${ckey}.allOrder`, name: `${ckey}allOrder`, zh: '全部订单' }];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};