|
@@ -30,35 +30,51 @@ class GoodsService extends CrudService {
|
|
|
// goods: 商品信息; specs:商品规格信息; shop:店铺信息
|
|
|
const returnData = { goods, specs, shop };
|
|
|
|
|
|
-
|
|
|
// 添加浏览次数
|
|
|
await this.goodsModel.updateOne({ _id: id }, { view_num: (goods.view_num || 0) + 1 });
|
|
|
return returnData;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- async indexGoodsList({ skip = 0, limit = 20 } = {}) {
|
|
|
- const list = await this.goodsModel.aggregate([
|
|
|
- {
|
|
|
- $lookup: {
|
|
|
- from: 'goodsSpec',
|
|
|
- localField: 'id',
|
|
|
- foreignField: 'shop',
|
|
|
- as: 'specs',
|
|
|
- },
|
|
|
+ async indexGoodsList(condition, { skip = 0, limit = 20 } = {}) {
|
|
|
+ condition = this.dealFilter(condition);
|
|
|
+ const pipline = [];
|
|
|
+ const { view_num, sell_num, sell_money, name } = condition;
|
|
|
+ const sort = {};
|
|
|
+ if (view_num) sort.view_num = view_num;
|
|
|
+ if (sell_num) sort.sell_num = sell_num;
|
|
|
+ if (sell_money) sort.sell_money = sell_money;
|
|
|
+ if (name) pipline.push({ $match: { name } });
|
|
|
+ // 表关联
|
|
|
+ pipline.push({
|
|
|
+ $lookup: {
|
|
|
+ from: 'goodsSpec',
|
|
|
+ localField: 'id',
|
|
|
+ foreignField: 'shop',
|
|
|
+ as: 'specs',
|
|
|
},
|
|
|
- {
|
|
|
- $project: { name: 1, file: 1, specs: 1 },
|
|
|
+ });
|
|
|
+ // 按照规格平铺数据
|
|
|
+ pipline.push({ $unwind: '$specs' });
|
|
|
+ // 格式化平铺后的数据
|
|
|
+ pipline.push({ $project: { name: 1, view_num: 1, sell_num: 1, file: 1, sell_money: { $toDouble: '$specs.sell_money' } } });
|
|
|
+ pipline.push({
|
|
|
+ $group: {
|
|
|
+ _id: '$_id',
|
|
|
+ name: { $first: '$name' },
|
|
|
+ view_num: { $first: '$view_num' },
|
|
|
+ sell_num: { $first: '$sell_num' },
|
|
|
+ sell_money: { $min: '$sell_money' },
|
|
|
+ // file: { $first: '$file' },
|
|
|
},
|
|
|
- { $skip: parseInt(skip) },
|
|
|
- { $limit: parseInt(limit) },
|
|
|
- ]);
|
|
|
- for (const i of list) {
|
|
|
- const spec = _.minBy(i.specs, 'sell_money');
|
|
|
- i.sell_money = _.get(spec, 'sell_money');
|
|
|
- if (i.sell_money) i.sell_money = i.sell_money.toString() * 1;
|
|
|
- delete i.specs;
|
|
|
- }
|
|
|
+ });
|
|
|
+ // 排序处理
|
|
|
+ if (view_num) pipline.push({ $sort: { view_num } });
|
|
|
+ if (sell_num) pipline.push({ $sort: { sell_num } });
|
|
|
+ if (sell_money) pipline.push({ $sort: { sell_money } });
|
|
|
+ // 分页处理
|
|
|
+ if (parseInt(skip)) pipline.push({ $skip: parseInt(skip) });
|
|
|
+ if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
|
|
|
+ const list = await this.goodsModel.aggregate(pipline);
|
|
|
return list;
|
|
|
}
|
|
|
}
|