lrf 2 years ago
parent
commit
a3b5e4da27
1 changed files with 38 additions and 6 deletions
  1. 38 6
      app/service/view/goods.js

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

@@ -13,6 +13,8 @@ class GoodsService extends CrudService {
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
     this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
     this.goodsTagsModel = this.ctx.model.System.GoodsTags;
+    this.platformActModel = this.ctx.model.System.PlatformAct;
+    this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
   }
   /**
    *
@@ -174,16 +176,14 @@ class GoodsService extends CrudService {
         from: 'actTags',
         localField: 'act_tags',
         foreignField: 'value',
-        pipeline: [
-          { $match: { status: '0', show_goods: '0' } },
-          { $sort: { sort: 1 } },
-          { $project: { label: 1, _id: 0 } },
-        ],
+        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' } } } });
+    pipeline.push({
+      $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } },
+    });
     // 表关联
     pipeline.push({
       $lookup: {
@@ -241,12 +241,44 @@ class GoodsService extends CrudService {
       const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow' ]);
       return obj;
     });
+    // 实时匹配活动
+    // 只找: 买赠;特价;满减/折; 特价需要修改价格; 剩下的打标签
+    const platformActList = await this.platformActModel.find({ is_use: '0', type: [ '2', '3', '5', '6' ] });
+    await this.getAboutAct(platformActList, list);
     const tpipeline = _.cloneDeep(pipeline);
     tpipeline.push({ $count: 'total' });
     const total = await this.goodsModel.aggregate(tpipeline);
     return { list, total: _.get(_.head(total), 'total', 0) };
   }
 
+  /**
+   * 处理商品有关活动部分
+   * @param {Array} platformActList 正在进行中的活动
+   * @param {Array} list 查询商品
+   */
+  async getAboutAct(platformActList, list) {
+    const platform_act = platformActList.map(i => ObjectId(i._id).toString());
+    for (const goods of list) {
+      const { _id: goods_id, p_act = [], sell_money } = goods;
+      const gjaList = await this.gjaModel.find({ platform_act, 'goods._id': ObjectId(goods_id).toString() });
+      if (gjaList.length <= 0) continue;
+      for (const gja of gjaList) {
+        const { platform_act_type: type } = gja;
+        if (type === '2' && gjaList.length > 0) p_act.push('买赠');
+        else if (type === '3' && gjaList.length > 0) {
+          p_act.push('特价');
+          let sortList = JSON.parse(JSON.stringify(gjaList));
+          sortList = sortList.map(i => ({ ...i, sp_price: _.get(i, 'config.sp_price', 0) }));
+          sortList = _.orderBy(sortList, [ 'sp_price' ], [ 'asc' ]);
+          const lp = _.get(_.head(sortList), 'sp_price');
+          if (lp && this.ctx.minus(lp, sell_money) < 0) goods.sell_money = lp;
+        } else if (type === '5' && gjaList.length > 0) p_act.push('满减');
+        else if (type === '6' && gjaList.length > 0) p_act.push('满折');
+        goods.p_act = p_act;
+      }
+    }
+  }
+
   async indexActTagsGoods() {
     // 将使用中且展示在首页的查出来排序
     const list = await this.ctx.model.System.ActTags.find({ status: '0', show_index: '0' }).sort({ sort: 1 });