|
@@ -31,22 +31,43 @@ class StoreGoodsService extends CrudService {
|
|
|
return num > 0;
|
|
|
}
|
|
|
|
|
|
- // 用户查看收藏店铺
|
|
|
- async userView(query, { skip, limit }) {
|
|
|
+ // 用户查看收藏商品
|
|
|
+ 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 } } }];
|
|
|
+ const { view_num, sell_num, sell_money, name, shop } = query;
|
|
|
+ const searchPips = [];
|
|
|
+ if (name) searchPips.push({ $match: { name: new RegExp(name) } });
|
|
|
+ if (shop) searchPips.push({ $match: { shop } });
|
|
|
+ const pipline = [{ $match: { customer } }];
|
|
|
+ // 联表-商品
|
|
|
+ pipline.push({ $addFields: { goods_id: { $toObjectId: '$goods' } } });
|
|
|
+ pipline.push({
|
|
|
+ $lookup: {
|
|
|
+ from: 'goods',
|
|
|
+ localField: 'goods_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ as: 'gi',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // 平铺
|
|
|
+ pipline.push({ $unwind: '$gi' });
|
|
|
+ // 格式化数据
|
|
|
+ pipline.push({
|
|
|
+ $project: {
|
|
|
+ _id: '$gi._id',
|
|
|
+ shop: '$gi.shop',
|
|
|
+ time: 1,
|
|
|
+ name: '$gi.name',
|
|
|
+ file: '$gi.file',
|
|
|
+ view_num: '$gi.view_num',
|
|
|
+ status: '$gi.status',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // 商品条件查询
|
|
|
+ if (searchPips.length > 0) pipline.push(...searchPips);
|
|
|
+ // 联表-规格
|
|
|
pipline.push({ $addFields: { goods_id: { $toString: '$_id' } } });
|
|
|
- // 表关联
|
|
|
pipline.push({
|
|
|
$lookup: {
|
|
|
from: 'goodsSpec',
|
|
@@ -55,10 +76,22 @@ class StoreGoodsService extends CrudService {
|
|
|
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({
|
|
|
+ $project: {
|
|
|
+ time: 1,
|
|
|
+ _id: 1,
|
|
|
+ shop: 1,
|
|
|
+ name: 1,
|
|
|
+ file: 1,
|
|
|
+ view_num: 1,
|
|
|
+ status: 1,
|
|
|
+ sell_money: '$specs.sell_money',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // 分组取最低价
|
|
|
pipline.push({
|
|
|
$group: {
|
|
|
_id: '$_id',
|
|
@@ -66,16 +99,27 @@ class StoreGoodsService extends CrudService {
|
|
|
view_num: { $first: '$view_num' },
|
|
|
sell_money: { $min: '$sell_money' },
|
|
|
file: { $first: '$file' },
|
|
|
- createdAt: { $first: '$createdAt' },
|
|
|
+ time: { $first: '$time' },
|
|
|
},
|
|
|
});
|
|
|
const qPipline = _.cloneDeep(pipline);
|
|
|
- qPipline.push({ $sort: { createdAt: -1 } });
|
|
|
+ // 排序处理
|
|
|
+ const 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);
|
|
|
+ sort.time = -1;
|
|
|
+ qPipline.push({ $sort: sort });
|
|
|
+ console.log(qPipline);
|
|
|
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;
|
|
|
+ const list = await this.model.aggregate(qPipline);
|
|
|
+ const tPipline = _.cloneDeep(pipline);
|
|
|
+ tPipline.push({ $count: 'total' });
|
|
|
+ const total = await this.model.aggregate(tPipline);
|
|
|
+ return { list, total: _.get(_.head(total), 'total', 0) };
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
module.exports = StoreGoodsService;
|