|
@@ -25,12 +25,56 @@ class StoreShopService extends CrudService {
|
|
|
return { msg: '收藏成功', result: true };
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// 检查是否收藏店铺
|
|
|
async check({ shop, customer }) {
|
|
|
const num = await this.model.count({ shop, customer });
|
|
|
return num > 0;
|
|
|
}
|
|
|
+
|
|
|
+ async userView(query = {}, { skip, limit } = {}) {
|
|
|
+ const customer = _.get(this.ctx, 'user._id');
|
|
|
+ assert(customer, '缺少用户信息');
|
|
|
+ const { name, time } = query;
|
|
|
+ const searchPips = { };
|
|
|
+ const sort = { time: -1 };
|
|
|
+ if (name) searchPips.name = new RegExp(name);
|
|
|
+ if (time) sort.time = time;
|
|
|
+
|
|
|
+ const pipline = [{ $match: { customer } }];
|
|
|
+ // 关联店铺
|
|
|
+ pipline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
|
|
|
+ pipline.push({
|
|
|
+ $lookup: {
|
|
|
+ from: 'shop',
|
|
|
+ localField: 'shop_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ as: 's',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ pipline.push({ $unwind: '$s' });
|
|
|
+ // 格式化数据
|
|
|
+ pipline.push({
|
|
|
+ $project: {
|
|
|
+ _id: '$s._id',
|
|
|
+ time: 1,
|
|
|
+ name: '$s.name',
|
|
|
+ logo: '$s.logo',
|
|
|
+ status: '$s.status',
|
|
|
+ address: '$s.address',
|
|
|
+ phone: '$s.phone',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // 条件查询
|
|
|
+ if (searchPips.length > 0) pipline.push(...searchPips);
|
|
|
+ // 联表-规格
|
|
|
+ const qPipline = _.cloneDeep(pipline);
|
|
|
+ qPipline.push({ $sort: sort });
|
|
|
+ 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 = StoreShopService;
|