Selaa lähdekoodia

Merge branch 'master' into dev

lrf 2 vuotta sitten
vanhempi
commit
bc73ac865a
2 muutettua tiedostoa jossa 41 lisäystä ja 24 poistoa
  1. 2 1
      app/model/system/actTags.js
  2. 39 23
      app/service/view/goods.js

+ 2 - 1
app/model/system/actTags.js

@@ -10,7 +10,7 @@ const actTags = {
   status: { type: String, required: false, default: '0', zh: '状态' }, // 字典:use
   file: { type: Array, required: false, zh: '图片' }, //
   show_index: { type: String, required: false, default: '0', zh: '是否在首页展示' }, // 字典:use
-  show_goods: { type: String, default: '1', zh: '是否显示商品标签' }, // 字典:use
+  show_goods: { type: String, required: false, default: '1', zh: '是否显示商品标签' }, // 字典:use
 };
 const schema = new Schema(actTags, { toJSON: { getters: true, virtuals: true } });
 schema.index({ id: 1 });
@@ -18,6 +18,7 @@ schema.index({ 'meta.createdAt': 1 });
 schema.index({ sort: 1 });
 schema.index({ status: 1 });
 schema.index({ show_index: 1 });
+schema.index({ show_goods: 1 });
 
 schema.plugin(metaPlugin);
 

+ 39 - 23
app/service/view/goods.js

@@ -129,18 +129,32 @@ class GoodsService extends CrudService {
 
   async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
     condition = this.dealFilter(condition);
-    const pipline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
+    const pipeline = [{ $match: { status: { $ne: '0' } } }]; // { $sort: { sort: 1 } },
     const { view_num, sell_num, sell_money, name, shop, tags } = condition;
     let sort = {};
     if (view_num) sort.view_num = parseInt(view_num);
     if (sell_num) sort.sell_num = parseInt(sell_num);
     if (sell_money) sort.sell_money = parseInt(sell_money);
-    if (name) pipline.push({ $match: { name: new RegExp(name) } });
-    if (shop) pipline.push({ $match: { shop } });
-    if (tags) pipline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: tags } } } } });
-    pipline.push({ $addFields: { goods_id: { $toString: '$_id' }, create_time: { $dateToString: { date: '$meta.createdAt', format: '%Y-%m-%d %H:%M:%S', timezone: '+08:00' } } } });
+    if (name) pipeline.push({ $match: { name: new RegExp(name) } });
+    if (shop) pipeline.push({ $match: { shop } });
+    if (tags) pipeline.push({ $match: { tags: { $elemMatch: { $elemMatch: { $eq: 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' } } } });
     // 表关联
-    pipline.push({
+    pipeline.push({
       $lookup: {
         from: 'goodsSpec',
         localField: 'goods_id',
@@ -149,9 +163,9 @@ class GoodsService extends CrudService {
       },
     });
     // 按照规格平铺数据
-    pipline.push({ $unwind: '$specs' });
+    pipeline.push({ $unwind: '$specs' });
     // 格式化平铺后的数据
-    pipline.push({
+    pipeline.push({
       $project: {
         name: 1,
         view_num: 1,
@@ -162,15 +176,16 @@ class GoodsService extends CrudService {
         sell_money: { $toDouble: '$specs.sell_money' },
         flow_money: { $toDouble: '$specs.flow_money' },
         num: '$specs.num',
+        actTagsShow: 1,
       },
     });
-    pipline.push({
+    pipeline.push({
       $group: {
         _id: '$_id',
         data: { $min: '$$CURRENT' },
       },
     });
-    pipline.push({
+    pipeline.push({
       $project: {
         name: '$data.name',
         view_num: '$data.view_num',
@@ -181,22 +196,23 @@ class GoodsService extends CrudService {
         num: '$data.num',
         create_time: '$data.create_time',
         sort: '$data.sort',
+        actTagsShow: '$data.actTagsShow',
       },
     });
     if (Object.keys(sort).length <= 0) sort = { sort: -1, create_time: -1 };
-    pipline.push({ $sort: { ...sort, sort: -1, create_time: -1 } });
+    pipeline.push({ $sort: { ...sort, sort: -1, create_time: -1 } });
     // 分页处理
-    const qPipline = _.cloneDeep(pipline);
-    if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
-    if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
-    let list = await this.goodsModel.aggregate(qPipline);
+    const qpipeline = _.cloneDeep(pipeline);
+    if (parseInt(skip)) qpipeline.push({ $skip: parseInt(skip) });
+    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' ]);
+      const obj = _.pick(i, [ 'name', 'file', 'num', 'flow_money', 'sell_money', 'view_num', '_id', 'actTagsShow' ]);
       return obj;
     });
-    const tPipline = _.cloneDeep(pipline);
-    tPipline.push({ $count: 'total' });
-    const total = await this.goodsModel.aggregate(tPipline);
+    const tpipeline = _.cloneDeep(pipeline);
+    tpipeline.push({ $count: 'total' });
+    const total = await this.goodsModel.aggregate(tpipeline);
     return { list, total: _.get(_.head(total), 'total', 0) };
   }
 
@@ -226,10 +242,10 @@ class GoodsService extends CrudService {
   }
 
   async searchActTagsGoods(act_tags, limit = 2) {
-    const pipline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
-    pipline.push({ $project: { name: 1, file: 1 } });
-    if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
-    const list = await this.goodsModel.aggregate(pipline);
+    const pipeline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
+    pipeline.push({ $project: { name: 1, file: 1 } });
+    if (parseInt(limit)) pipeline.push({ $limit: parseInt(limit) });
+    const list = await this.goodsModel.aggregate(pipeline);
     return list;
   }
 }