123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- //
- class GoodsService extends CrudService {
- constructor(ctx) {
- super(ctx, 'goods');
- this.goodsModel = this.ctx.model.Shop.Goods;
- this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
- }
- /**
- *
- * @param {Object} query 查询条件
- * @param query.id 商品数据id
- */
- async goodsDetail({ id }) {
- const { populate } = this.ctx.service.shop.goods.getRefMods();
- let goods = await this.goodsModel.findById(id, { file: 1, tags: 1, name: 1, shot_brief: 1, brief: 1, send_time: 1, shop: 1, view_num: 1 }).populate(populate);
- if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到商品数据');
- goods = JSON.parse(JSON.stringify(goods));
- let specs = await this.goodsSpecModel.find({ goods: id, status: '0' }, { sell_money: 1, flow_money: 1, freight: 1, name: 1, num: 1 });
- specs = JSON.parse(JSON.stringify(specs));
- goods = _.omit(goods, [ 'meta', '__v' ]);
- const shop = _.pick(goods.shop, [ 'logo', 'name', 'person', 'phone', '_id' ]);
- delete goods.shop;
- // 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(condition, { skip = 0, limit = 20 } = {}) {
- condition = this.dealFilter(condition);
- const pipline = [{ $match: { status: { $ne: '0' } } }];
- 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({ $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' } } });
- 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' },
- },
- });
- // 排序处理
- 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;
- }
- }
- module.exports = GoodsService;
|