lrf 2 år sedan
förälder
incheckning
f01161341a
2 ändrade filer med 23 tillägg och 81 borttagningar
  1. 5 1
      app/controller/shop/config/.goodsJoinAct.js
  2. 18 80
      app/service/shop/goodsJoinAct.js

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

@@ -24,7 +24,11 @@ module.exports = {
         platform_act: 'platform_act',
         platform_act: 'platform_act',
         platform_act_type: 'platform_act_type',
         platform_act_type: 'platform_act_type',
         shop: 'shop',
         shop: 'shop',
-        goods: '%goods.goods.name%',
+        shop_name: 'shop_name',
+        goods: 'goods',
+        goods_name: 'goods_name',
+        spec: 'spec',
+        spec_name: 'spec_name',
         status: 'status',
         status: 'status',
       },
       },
       // options: {
       // options: {

+ 18 - 80
app/service/shop/goodsJoinAct.js

@@ -3,6 +3,7 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const _ = require('lodash');
 const assert = require('assert');
 const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
 
 
 //
 //
 class GoodsJoinActService extends CrudService {
 class GoodsJoinActService extends CrudService {
@@ -32,84 +33,32 @@ class GoodsJoinActService extends CrudService {
   }
   }
 
 
   async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
   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, platform_act, goods, goods_name, shop_name } = condition;
-    const fMatch = {};
-    if (shop) fMatch.shop = shop;
-    if (platform_act) fMatch.platform_act = platform_act;
-    if (goods) fMatch.goods = goods;
-    if (status) fMatch.status = status;
+    console.log(filter);
+    const { platform_act, platform_act_type, shop, shop_name, goods, goods_name, spec, spec_name, status } = filter;
+    const mq = {};
+    if (platform_act) mq.platform_act = platform_act;
+    if (platform_act_type) mq.platform_act_type = platform_act_type;
+    if (shop)mq.shop = shop;
+    if (goods) mq.goods = goods;
+    if (goods_name) mq['goods.name'] = new RegExp(goods_name);
+    if (spec) mq.spec = spec;
+    if (spec_name) mq['spec.name'] = new RegExp(spec_name);
+    if (status) mq.status = status;
     const pipeline = [];
     const pipeline = [];
-    if (Object.keys(fMatch).length > 0) {
-      pipeline.push({ $match: fMatch });
-    }
-    // 表关联
-    pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' }, goods_id: { $toObjectId: '$goods' } } });
+    if (Object.keys(mq).length > 0) pipeline.push({ $match: mq });
+    pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
+    const sp = [];
+    if (shop_name) sp.push({ $match: { name: new RegExp(shop_name) } });
+    sp.push({ $project: { name: 1 } });
     pipeline.push({
     pipeline.push({
       $lookup: {
       $lookup: {
         from: 'shop',
         from: 'shop',
         localField: 'shop_id',
         localField: 'shop_id',
         foreignField: '_id',
         foreignField: '_id',
-        pipeline: [{ $project: { name: 1 } }],
+        pipeline: sp,
         as: 'shopInfo',
         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({
-      $lookup: {
-        from: 'goodsSpec',
-        localField: 'goods',
-        foreignField: 'goods',
-        pipeline: [
-          {
-            $group: {
-              _id: '$goods',
-              data: { $min: '$$CURRENT' },
-            },
-          },
-          { $project: { name: '$data.name', file: '$data.file', sell_money: '$data.sell_money', flow_money: '$data.flow_money' } },
-        ],
-        as: 'specInfo',
-      },
-    });
-    pipeline.push({ $unwind: '$specInfo' });
-    pipeline.push({
-      $project: {
-        platformAct: 1,
-        shop: '$shopInfo',
-        goods: '$goodsInfo',
-        flow_money: { $toString: '$specInfo.flow_money' },
-        sell_money: { $toString: '$specInfo.sell_money' },
-        spec_file: '$specInfo.file',
-      },
-    });
     const qPipeline = _.cloneDeep(pipeline);
     const qPipeline = _.cloneDeep(pipeline);
     if (parseInt(skip)) qPipeline.push({ $skip: parseInt(skip) });
     if (parseInt(skip)) qPipeline.push({ $skip: parseInt(skip) });
     if (parseInt(limit)) qPipeline.push({ $limit: parseInt(limit) });
     if (parseInt(limit)) qPipeline.push({ $limit: parseInt(limit) });
@@ -117,17 +66,6 @@ class GoodsJoinActService extends CrudService {
     const tPipeline = _.cloneDeep(pipeline);
     const tPipeline = _.cloneDeep(pipeline);
     tPipeline.push({ $count: 'total' });
     tPipeline.push({ $count: 'total' });
     const total = await this.model.aggregate(tPipeline);
     const total = await this.model.aggregate(tPipeline);
-    // 整理图片
-    // const list = [];
-    // for (const r of res) {
-    //   const i = _.cloneDeep(r);
-    //   const { spec_file = [] } = i;
-    //   const goods_file = _.get(r, 'goods.file', []);
-    //   const nf = [ ...spec_file, ...goods_file ];
-    //   delete i.spec_file;
-    //   i.goods.file = nf;
-    //   list.push(i);
-    // }
     return { data: res, total: _.get(_.head(total), 'total', 0) };
     return { data: res, total: _.get(_.head(total), 'total', 0) };
   }
   }
 }
 }