|
@@ -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;
|