|
@@ -4,12 +4,79 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
const _ = require('lodash');
|
|
|
const assert = require('assert');
|
|
|
|
|
|
-//
|
|
|
+//
|
|
|
class GoodsJoinActService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, 'goodsjoinact');
|
|
|
this.model = this.ctx.model.Shop.GoodsJoinAct;
|
|
|
}
|
|
|
+ async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
|
|
|
+ // 处理排序
|
|
|
+ if (sort && _.isString(sort)) {
|
|
|
+ sort = { [sort]: desc ? -1 : 1 };
|
|
|
+ } else if (sort && _.isArray(sort)) {
|
|
|
+ sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
+ }
|
|
|
+ let condition = _.cloneDeep(filter);
|
|
|
+ condition = await this.beforeQuery(condition);
|
|
|
+ condition = this.dealFilter(condition);
|
|
|
+ const { shop, status, platformAct, goods, goods_name, shop_name } = condition;
|
|
|
+ const fMatch = { };
|
|
|
+ if (shop)fMatch.shop = shop;
|
|
|
+ if (platformAct) fMatch.platformAct = platformAct;
|
|
|
+ if (goods)fMatch.goods = goods;
|
|
|
+ if (status) fMatch.status = status;
|
|
|
+ const pipeline = [];
|
|
|
+ if (Object.keys(fMatch).length > 0) {
|
|
|
+ pipeline.push({ $match: fMatch });
|
|
|
+ }
|
|
|
+ // 表关联
|
|
|
+ pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' }, goods_id: { $toObjectId: '$goods' } } });
|
|
|
+ pipeline.push({
|
|
|
+ $lookup: {
|
|
|
+ from: 'shop',
|
|
|
+ localField: 'shop_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ pipeline: [{ $project: { name: 1 } }],
|
|
|
+ as: 'shopInfo',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ pipeline.push({
|
|
|
+ $lookup: {
|
|
|
+ from: 'goods',
|
|
|
+ localField: 'goods_id',
|
|
|
+ foreignField: '_id',
|
|
|
+ pipeline: [{ $project: { name: 1, file: 1 } }],
|
|
|
+ as: 'goodsInfo',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ pipeline.push({ $unwind: '$shopInfo' });
|
|
|
+ pipeline.push({ $unwind: '$goodsInfo' });
|
|
|
+ // 过滤
|
|
|
+ if (goods_name) {
|
|
|
+ pipeline.push({ $match: { 'goodsInfo.name': new RegExp(goods_name) } });
|
|
|
+ }
|
|
|
+ if (shop_name) {
|
|
|
+ pipeline.push({ $match: { 'shopInfo.name': new RegExp(shop_name) } });
|
|
|
+ }
|
|
|
+ pipeline.push({ $sort: { 'meta.createdAt': -1 } });
|
|
|
+ pipeline.push({
|
|
|
+ $project: {
|
|
|
+ statsu: 1,
|
|
|
+ platformAct: 1,
|
|
|
+ shop: '$shopInfo',
|
|
|
+ goods: '$goodsInfo',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ const qPipeline = _.cloneDeep(pipeline);
|
|
|
+ if (parseInt(skip)) qPipeline.push({ $skip: parseInt(skip) });
|
|
|
+ if (parseInt(limit)) qPipeline.push({ $limit: parseInt(limit) });
|
|
|
+ const list = await this.model.aggregate(qPipeline);
|
|
|
+ const tPipeline = _.cloneDeep(pipeline);
|
|
|
+ tPipeline.push({ $count: 'total' });
|
|
|
+ const total = await this.model.aggregate(tPipeline);
|
|
|
+ return { data: list, total: _.get(_.head(total), 'total', 0) };
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = GoodsJoinActService;
|