lrf 2 tahun lalu
induk
melakukan
6528450705
1 mengubah file dengan 26 tambahan dan 13 penghapusan
  1. 26 13
      app/service/view/goods.js

+ 26 - 13
app/service/view/goods.js

@@ -15,6 +15,7 @@ class GoodsService extends CrudService {
     this.goodsTagsModel = this.ctx.model.System.GoodsTags;
     this.platformActModel = this.ctx.model.System.PlatformAct;
     this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
+    this.actTagsModel = this.ctx.model.System.ActTags;
   }
   /**
    *
@@ -171,16 +172,6 @@ class GoodsService extends CrudService {
     if (shop) pipeline.push({ $match: { shop } });
     if (tags) pipeline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
     if (act_tags) pipeline.push({ $match: { act_tags: { $elemMatch: { $eq: act_tags } } } });
-    pipeline.push({
-      $lookup: {
-        from: 'actTags',
-        localField: 'act_tags',
-        foreignField: 'value',
-        pipeline: [{ $match: { status: '0', show_goods: '0' } }, { $sort: { sort: 1 } }, { $project: { label: 1, _id: 0 } }],
-        as: 'actTagsShow',
-      },
-    });
-
     pipeline.push({
       $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } },
     });
@@ -207,7 +198,7 @@ class GoodsService extends CrudService {
         sell_money: { $toDouble: '$specs.sell_money' },
         flow_money: { $toDouble: '$specs.flow_money' },
         num: '$specs.num',
-        actTagsShow: 1,
+        act_tags: 1,
       },
     });
     pipeline.push({
@@ -227,7 +218,7 @@ class GoodsService extends CrudService {
         num: '$data.num',
         create_time: '$data.create_time',
         sort: '$data.sort',
-        // actTagsShow: '$data.actTagsShow',
+        act_tags: '$data.act_tags',
       },
     });
     if (Object.keys(sort).length <= 0) sort = { sort: -1, create_time: -1 };
@@ -238,9 +229,11 @@ class GoodsService extends CrudService {
     if (parseInt(limit)) qpipeline.push({ $limit: parseInt(limit) });
     let list = await this.goodsModel.aggregate(qpipeline);
     list = list.map(i => {
-      const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow' ]);
+      const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow', 'act_tags' ]);
       return obj;
     });
+    // 处理活动标签
+    await this.getActTags(list);
     // 实时匹配活动
     // 只找: 买赠;特价;满减/折; 特价需要修改价格; 剩下的打标签
     const platformActList = await this.platformActModel.find({ is_use: '0', type: [ '2', '3', '5', '6' ] });
@@ -251,6 +244,26 @@ class GoodsService extends CrudService {
     return { list, total: _.get(_.head(total), 'total', 0) };
   }
 
+  async getActTags(list) {
+    let tags = list.map(i => i.act_tags);
+    tags = _.flattenDeep(tags);
+    tags = _.uniq(tags);
+    tags = _.compact(tags);
+    const tagsData = await this.actTagsModel.find({ value: tags, show_goods: '0' });
+    for (const i of list) {
+      const { act_tags = [] } = i;
+      const actTagsShow = [];
+      if (act_tags.length > 0) {
+        for (const t of act_tags) {
+          const r = tagsData.find(f => f.value === t);
+          if (r) actTagsShow.push(r.label);
+        }
+      }
+      i.actTagsShow = actTagsShow;
+      delete i.act_tags;
+    }
+  }
+
   /**
    * 处理商品有关活动部分
    * @param {Array} platformActList 正在进行中的活动