|
@@ -3,6 +3,7 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
const _ = require('lodash');
|
|
|
const assert = require('assert');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
|
|
|
//
|
|
|
class GoodsJoinActService extends CrudService {
|
|
@@ -32,84 +33,32 @@ class GoodsJoinActService extends CrudService {
|
|
|
}
|
|
|
|
|
|
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, platform_act, goods, goods_name, shop_name } = condition;
|
|
|
- const fMatch = {};
|
|
|
- if (shop) fMatch.shop = shop;
|
|
|
- if (platform_act) fMatch.platform_act = platform_act;
|
|
|
- if (goods) fMatch.goods = goods;
|
|
|
- if (status) fMatch.status = status;
|
|
|
+ console.log(filter);
|
|
|
+ const { platform_act, platform_act_type, shop, shop_name, goods, goods_name, spec, spec_name, status } = filter;
|
|
|
+ const mq = {};
|
|
|
+ if (platform_act) mq.platform_act = platform_act;
|
|
|
+ if (platform_act_type) mq.platform_act_type = platform_act_type;
|
|
|
+ if (shop)mq.shop = shop;
|
|
|
+ if (goods) mq.goods = goods;
|
|
|
+ if (goods_name) mq['goods.name'] = new RegExp(goods_name);
|
|
|
+ if (spec) mq.spec = spec;
|
|
|
+ if (spec_name) mq['spec.name'] = new RegExp(spec_name);
|
|
|
+ if (status) mq.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' } } });
|
|
|
+ if (Object.keys(mq).length > 0) pipeline.push({ $match: mq });
|
|
|
+ pipeline.push({ $addFields: { shop_id: { $toObjectId: '$shop' } } });
|
|
|
+ const sp = [];
|
|
|
+ if (shop_name) sp.push({ $match: { name: new RegExp(shop_name) } });
|
|
|
+ sp.push({ $project: { name: 1 } });
|
|
|
pipeline.push({
|
|
|
$lookup: {
|
|
|
from: 'shop',
|
|
|
localField: 'shop_id',
|
|
|
foreignField: '_id',
|
|
|
- pipeline: [{ $project: { name: 1 } }],
|
|
|
+ pipeline: sp,
|
|
|
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({
|
|
|
- $lookup: {
|
|
|
- from: 'goodsSpec',
|
|
|
- localField: 'goods',
|
|
|
- foreignField: 'goods',
|
|
|
- pipeline: [
|
|
|
- {
|
|
|
- $group: {
|
|
|
- _id: '$goods',
|
|
|
- data: { $min: '$$CURRENT' },
|
|
|
- },
|
|
|
- },
|
|
|
- { $project: { name: '$data.name', file: '$data.file', sell_money: '$data.sell_money', flow_money: '$data.flow_money' } },
|
|
|
- ],
|
|
|
- as: 'specInfo',
|
|
|
- },
|
|
|
- });
|
|
|
- pipeline.push({ $unwind: '$specInfo' });
|
|
|
- pipeline.push({
|
|
|
- $project: {
|
|
|
- platformAct: 1,
|
|
|
- shop: '$shopInfo',
|
|
|
- goods: '$goodsInfo',
|
|
|
- flow_money: { $toString: '$specInfo.flow_money' },
|
|
|
- sell_money: { $toString: '$specInfo.sell_money' },
|
|
|
- spec_file: '$specInfo.file',
|
|
|
- },
|
|
|
- });
|
|
|
const qPipeline = _.cloneDeep(pipeline);
|
|
|
if (parseInt(skip)) qPipeline.push({ $skip: parseInt(skip) });
|
|
|
if (parseInt(limit)) qPipeline.push({ $limit: parseInt(limit) });
|
|
@@ -117,17 +66,6 @@ class GoodsJoinActService extends CrudService {
|
|
|
const tPipeline = _.cloneDeep(pipeline);
|
|
|
tPipeline.push({ $count: 'total' });
|
|
|
const total = await this.model.aggregate(tPipeline);
|
|
|
- // 整理图片
|
|
|
- // const list = [];
|
|
|
- // for (const r of res) {
|
|
|
- // const i = _.cloneDeep(r);
|
|
|
- // const { spec_file = [] } = i;
|
|
|
- // const goods_file = _.get(r, 'goods.file', []);
|
|
|
- // const nf = [ ...spec_file, ...goods_file ];
|
|
|
- // delete i.spec_file;
|
|
|
- // i.goods.file = nf;
|
|
|
- // list.push(i);
|
|
|
- // }
|
|
|
return { data: res, total: _.get(_.head(total), 'total', 0) };
|
|
|
}
|
|
|
}
|