123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- '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 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, '缺少商品信息');
- const customer = _.get(this.ctx, 'user._id');
- assert(customer, '缺少用户信息');
- let data = await this.model.findOne({ customer, goods });
- if (data) {
- await this.model.deleteOne({ customer, goods });
- return { msg: '取消收藏', result: false };
- }
- data = await this.model.create({ customer, goods, time: moment().format('YYYY-MM-DD HH:mm:ss') });
- return { msg: '收藏成功', result: true };
- }
- // 检查是否收藏商品
- async check({ goods, customer }) {
- 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 { view_num, sell_num, sell_money, name, shop, time } = query;
- const searchPips = [];
- if (name) searchPips.push({ $match: { name: new RegExp(name) } });
- if (shop) searchPips.push({ $match: { shop } });
- const pipline = [{ $match: { customer } }];
- // 联表-商品
- pipline.push({ $addFields: { goods_id: { $toObjectId: '$goods' } } });
- pipline.push({
- $lookup: {
- from: 'goods',
- localField: 'goods_id',
- foreignField: '_id',
- as: 'gi',
- },
- });
- // 平铺
- pipline.push({ $unwind: '$gi' });
- // 格式化数据
- pipline.push({
- $project: {
- _id: '$gi._id',
- shop: '$gi.shop',
- time: 1,
- name: '$gi.name',
- file: '$gi.file',
- view_num: '$gi.view_num',
- status: '$gi.status',
- },
- });
- // 商品条件查询
- if (searchPips.length > 0) pipline.push(...searchPips);
- // 联表-规格
- 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: {
- time: 1,
- _id: 1,
- shop: 1,
- name: 1,
- file: 1,
- view_num: 1,
- status: 1,
- sell_money: '$specs.sell_money',
- },
- });
- // 分组取最低价
- pipline.push({
- $group: {
- _id: '$_id',
- name: { $first: '$name' },
- view_num: { $first: '$view_num' },
- sell_money: { $min: '$sell_money' },
- file: { $first: '$file' },
- time: { $first: '$time' },
- },
- });
- const qPipline = _.cloneDeep(pipline);
- // 排序处理
- const sort = { };
- if (view_num) sort.view_num = parseInt(view_num);
- if (sell_num) sort.sell_num = parseInt(sell_num);
- if (sell_money) sort.sell_money = parseInt(sell_money);
- sort.time = parseInt(time) || -1;
- qPipline.push({ $sort: sort });
- if (parseInt(skip)) qPipline.push({ $skip: parseInt(skip) });
- if (parseInt(limit)) qPipline.push({ $limit: parseInt(limit) });
- const list = await this.model.aggregate(qPipline);
- const tPipline = _.cloneDeep(pipline);
- tPipline.push({ $count: 'total' });
- const total = await this.model.aggregate(tPipline);
- return { list, total: _.get(_.head(total), 'total', 0) };
- }
- }
- module.exports = StoreGoodsService;
|