123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- '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');
- const { ObjectId } = require('mongoose').Types;
- //
- 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 = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' } } }];
- const { view_num, sell_num, sell_money, name, shop } = 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: new RegExp(name) } });
- if (shop) pipline.push({ $match: { shop } });
- 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_num: { $first: '$sell_num' },
- sell_money: { $min: '$sell_money' },
- file: { $first: '$file' },
- createdAt: { $first: '$createdAt' },
- },
- });
- // 排序处理
- if (view_num) pipline.push({ $sort: { view_num } });
- if (sell_num) pipline.push({ $sort: { sell_num } });
- if (sell_money) pipline.push({ $sort: { sell_money } });
- // 分页处理
- 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);
- const tPipline = _.cloneDeep(pipline);
- tPipline.push({ $count: 'total' });
- const total = await this.goodsModel.aggregate(tPipline);
- return { list, total: _.get(_.head(total), 'total', 0) };
- }
- async indexActTagsGoods() {
- const dictData = await this.ctx.model.Dev.DictData.find({ code: 'act_tags' });
- const result = [];
- for (const t of dictData) {
- const { label, value } = t;
- const list = await this.searchActTagsGoods(value);
- const arr = [];
- for (const g of list) {
- const obj = {
- url: _.get(g, 'file'),
- name: _.get(g, 'name'),
- id: _.get(g, '_id'),
- };
- if (arr.length === 0) {
- obj.title = label;
- }
- arr.push(obj);
- }
- result.push({ list: arr });
- }
- return result;
- }
- async searchActTagsGoods(act_tags, limit = 2) {
- const pipline = [{ $sort: { 'meta.createdAt': -1 } }, { $match: { status: { $ne: '0' }, act_tags } }];
- pipline.push({ $project: { name: 1, file: 1 } });
- if (parseInt(limit)) pipline.push({ $limit: parseInt(limit) });
- const list = await this.goodsModel.aggregate(pipline);
- return list;
- }
- }
- module.exports = GoodsService;
|