lrf 2 년 전
부모
커밋
dbe648b06c
5개의 변경된 파일59개의 추가작업 그리고 26개의 파일을 삭제
  1. 18 1
      app/controller/view/config/.goods.js
  2. 1 1
      app/model/trade/afterSale.js
  3. 1 1
      app/model/trade/order.js
  4. 1 1
      app/model/trade/orderDetail.js
  5. 38 22
      app/service/view/goods.js

+ 18 - 1
app/controller/view/config/.goods.js

@@ -2,5 +2,22 @@ module.exports = {
   goodsDetail: {
     requestBody: ['!id'],
   },
-  indexGoodsList: {},
+  indexGoodsList: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        name: '%name%',
+        view_num: 'view_num',
+        sell_num: 'sell_num',
+        sell_money: 'sell_money',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    options: {
+      query: ['skip', 'limit'],
+    },
+  },
 };

+ 1 - 1
app/model/trade/afterSale.js

@@ -8,7 +8,7 @@ const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-pl
 const afterSale = {
   order: { type: String, required: false, zh: '总订单', ref: 'Trade.Order' }, //
   order_detail: { type: String, required: false, zh: '订单详情', ref: 'Trade.OrderDetail' }, //
-  customer: { type: String, required: false, zh: '顾客', ref: 'User.Customer' }, //
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
   shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, //
   goods: { type: Array, required: false, zh: '需要售后的商品快照清单' }, //
   type: { type: String, required: false, zh: '售后类型' }, // 字典:afterSale_type

+ 1 - 1
app/model/trade/order.js

@@ -6,7 +6,7 @@ const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-pl
 
 // 订单
 const order = {
-  customer: { type: String, required: false, zh: '顾客', ref: 'User.Customer' }, //
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
   address: { type: String, required: false, zh: '邮寄地址', ref: 'User.address' }, //
   total_detail: { type: Array, required: false, zh: '总金额明细' }, // 运费,优惠,活动,商品原费用的和
   buy_time: { type: String, required: false, zh: '下单时间' }, //

+ 1 - 1
app/model/trade/orderDetail.js

@@ -8,7 +8,7 @@ const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-pl
 const orderDetail = {
   order: { type: String, required: false, zh: '总订单', ref: 'Trade.order' }, //
   shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, //
-  customer: { type: String, required: false, zh: '顾客', ref: 'User.Customer' }, //
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
   address: { type: String, required: false, zh: '邮寄地址', ref: 'User.address' }, //
   goods: { type: Array, required: false, zh: '商品快照清单' }, // 下单时,商品的属性设置
   freight: { type: String, required: false, zh: '运费' }, //

+ 38 - 22
app/service/view/goods.js

@@ -30,35 +30,51 @@ class GoodsService extends CrudService {
     // goods: 商品信息; specs:商品规格信息; shop:店铺信息
     const returnData = { goods, specs, shop };
 
-
     // 添加浏览次数
     await this.goodsModel.updateOne({ _id: id }, { view_num: (goods.view_num || 0) + 1 });
     return returnData;
   }
 
-
-  async indexGoodsList({ skip = 0, limit = 20 } = {}) {
-    const list = await this.goodsModel.aggregate([
-      {
-        $lookup: {
-          from: 'goodsSpec',
-          localField: 'id',
-          foreignField: 'shop',
-          as: 'specs',
-        },
+  async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
+    condition = this.dealFilter(condition);
+    const pipline = [];
+    const { view_num, sell_num, sell_money, name } = condition;
+    const sort = {};
+    if (view_num) sort.view_num = view_num;
+    if (sell_num) sort.sell_num = sell_num;
+    if (sell_money) sort.sell_money = sell_money;
+    if (name) pipline.push({ $match: { name } });
+    // 表关联
+    pipline.push({
+      $lookup: {
+        from: 'goodsSpec',
+        localField: 'id',
+        foreignField: 'shop',
+        as: 'specs',
       },
-      {
-        $project: { name: 1, file: 1, specs: 1 },
+    });
+    // 按照规格平铺数据
+    pipline.push({ $unwind: '$specs' });
+    // 格式化平铺后的数据
+    pipline.push({ $project: { name: 1, view_num: 1, sell_num: 1, file: 1, sell_money: { $toDouble: '$specs.sell_money' } } });
+    pipline.push({
+      $group: {
+        _id: '$_id',
+        name: { $first: '$name' },
+        view_num: { $first: '$view_num' },
+        sell_num: { $first: '$sell_num' },
+        sell_money: { $min: '$sell_money' },
+        // file: { $first: '$file' },
       },
-      { $skip: parseInt(skip) },
-      { $limit: parseInt(limit) },
-    ]);
-    for (const i of list) {
-      const spec = _.minBy(i.specs, 'sell_money');
-      i.sell_money = _.get(spec, 'sell_money');
-      if (i.sell_money) i.sell_money = i.sell_money.toString() * 1;
-      delete i.specs;
-    }
+    });
+    // 排序处理
+    if (view_num) pipline.push({ $sort: { view_num } });
+    if (sell_num) pipline.push({ $sort: { sell_num } });
+    if (sell_money) pipline.push({ $sort: { sell_money } });
+    // 分页处理
+    if (parseInt(skip)) pipline.push({ $skip: parseInt(skip) });
+    if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
+    const list = await this.goodsModel.aggregate(pipline);
     return list;
   }
 }