lrf 2 роки тому
батько
коміт
dd3a09185a

+ 9 - 0
app/controller/user/config/.storeGoods.js

@@ -46,4 +46,13 @@ module.exports = {
       },
     },
   },
+  userView: {
+    parameters: {},
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['time'],
+      desc: true,
+      count: true,
+    },
+  },
 };

+ 48 - 0
app/service/user/storeGoods.js

@@ -4,12 +4,14 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
 const moment = require('moment');
+const { ObjectId } = require('mongoose').Types;
 
 //
 class StoreGoodsService extends CrudService {
   constructor(ctx) {
     super(ctx, 'storegoods');
     this.model = this.ctx.model.User.StoreGoods;
+    this.goodsModel = this.ctx.model.Shop.Goods;
   }
   async create({ goods }) {
     assert(goods, '缺少商品信息');
@@ -28,6 +30,52 @@ class StoreGoodsService extends CrudService {
     const num = await this.model.count({ goods, customer });
     return num > 0;
   }
+
+  // 用户查看收藏店铺
+  async userView(query, { skip, limit }) {
+    const customer = _.get(this.ctx, 'user._id');
+    assert(customer, '缺少用户信息');
+    const store = await this.model.find({ customer });
+    const goods_ids = store.map(i => i.goods);
+    const goodsList = await this.remakeGoodsData(goods_ids, skip, limit);
+    return goodsList;
+  }
+
+  // 重组商品信息
+  async remakeGoodsData(goods_ids, skip, limit) {
+    goods_ids = goods_ids.map(i => ObjectId(i));
+    const pipline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { _id: { $in: goods_ids } } }];
+    pipline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
+    // 表关联
+    pipline.push({
+      $lookup: {
+        from: 'goodsSpec',
+        localField: 'goods_id',
+        foreignField: 'goods',
+        as: 'specs',
+      },
+    });
+    // 按照规格平铺数据
+    pipline.push({ $unwind: '$specs' });
+    // // 格式化平铺后的数据
+    pipline.push({ $project: { name: 1, view_num: 1, sell_num: 1, file: 1, sell_money: { $toDouble: '$specs.sell_money' }, createdAt: '$meta.createdAt' } });
+    pipline.push({
+      $group: {
+        _id: '$_id',
+        name: { $first: '$name' },
+        view_num: { $first: '$view_num' },
+        sell_money: { $min: '$sell_money' },
+        file: { $first: '$file' },
+        createdAt: { $first: '$createdAt' },
+      },
+    });
+    const qPipline = _.cloneDeep(pipline);
+    qPipline.push({ $sort: { createdAt: -1 } });
+    if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
+    if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
+    const list = await this.goodsModel.aggregate(qPipline);
+    return list;
+  }
 }
 
 module.exports = StoreGoodsService;

+ 1 - 0
app/z_router/user/storeGoods.js

@@ -7,6 +7,7 @@ const rkey = 'storeGoods';
 const ckey = 'user.storeGoods';
 const keyZh = '收藏商品';
 const routes = [
+  { method: 'get', path: `${rkey}/userView`, controller: `${ckey}.userView`, name: `${ckey}userView`, zh: `${keyZh}-用户查看收藏` },
   { method: 'get', path: `${rkey}/check`, controller: `${ckey}.check`, name: `${ckey}check`, zh: `${keyZh}-检查是否收藏` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },