lrf 2 年之前
父节点
当前提交
4cd8881b94

+ 3 - 1
app/controller/shop/config/.goodsJoinAct.js

@@ -25,6 +25,8 @@ module.exports = {
         shop: 'shop',
         goods: 'goods',
         status: 'status',
+        goods_name: 'goods_name',
+        shop_name: 'shop_name',
       },
     },
     service: 'query',
@@ -32,7 +34,7 @@ module.exports = {
       query: ['skip', 'limit'],
       sort: ['meta.createdAt'],
       desc: true,
-      count: true,
+      count: false,
     },
   },
 };

+ 68 - 1
app/service/shop/goodsJoinAct.js

@@ -4,12 +4,79 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
 
-// 
+//
 class GoodsJoinActService extends CrudService {
   constructor(ctx) {
     super(ctx, 'goodsjoinact');
     this.model = this.ctx.model.Shop.GoodsJoinAct;
   }
+  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);
+    const { shop, status, platformAct, goods, goods_name, shop_name } = condition;
+    const fMatch = { };
+    if (shop)fMatch.shop = shop;
+    if (platformAct) fMatch.platformAct = platformAct;
+    if (goods)fMatch.goods = goods;
+    if (status) fMatch.status = status;
+    const pipeline = [];
+    if (Object.keys(fMatch).length > 0) {
+      pipeline.push({ $match: fMatch });
+    }
+    // 表关联
+    pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' }, goods_id: { $toObjectId: '$goods' } } });
+    pipeline.push({
+      $lookup: {
+        from: 'shop',
+        localField: 'shop_id',
+        foreignField: '_id',
+        pipeline: [{ $project: { name: 1 } }],
+        as: 'shopInfo',
+      },
+    });
+    pipeline.push({
+      $lookup: {
+        from: 'goods',
+        localField: 'goods_id',
+        foreignField: '_id',
+        pipeline: [{ $project: { name: 1, file: 1 } }],
+        as: 'goodsInfo',
+      },
+    });
+    pipeline.push({ $unwind: '$shopInfo' });
+    pipeline.push({ $unwind: '$goodsInfo' });
+    // 过滤
+    if (goods_name) {
+      pipeline.push({ $match: { 'goodsInfo.name': new RegExp(goods_name) } });
+    }
+    if (shop_name) {
+      pipeline.push({ $match: { 'shopInfo.name': new RegExp(shop_name) } });
+    }
+    pipeline.push({ $sort: { 'meta.createdAt': -1 } });
+    pipeline.push({
+      $project: {
+        statsu: 1,
+        platformAct: 1,
+        shop: '$shopInfo',
+        goods: '$goodsInfo',
+      },
+    });
+    const qPipeline = _.cloneDeep(pipeline);
+    if (parseInt(skip)) qPipeline.push({ $skip: parseInt(skip) });
+    if (parseInt(limit)) qPipeline.push({ $limit: parseInt(limit) });
+    const list = await this.model.aggregate(qPipeline);
+    const tPipeline = _.cloneDeep(pipeline);
+    tPipeline.push({ $count: 'total' });
+    const total = await this.model.aggregate(tPipeline);
+    return { data: list, total: _.get(_.head(total), 'total', 0) };
+  }
 }
 
 module.exports = GoodsJoinActService;

+ 0 - 1
app/service/view/goods.js

@@ -106,7 +106,6 @@ class GoodsService extends CrudService {
     sort.sort = 1;
     sort = { ...sort, sort: 1, create_time: -1 };
     pipline.push({ $sort: sort });
-    console.log(pipline);
     // 分页处理
     const qPipline = _.cloneDeep(pipline);
     if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });

+ 1 - 1
app/z_router/shop/goodsJoinAct.js

@@ -7,7 +7,7 @@ const rkey = 'goodsJoinAct';
 const ckey = 'shop.goodsJoinAct';
 const keyZh = '平台商品活动关联';
 const routes = [
-  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}`, middleware: [ 'dealQuery' ], controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
   { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },