12345678910111213141516171819202122232425262728293031323334353637 |
- '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 }).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' ]);
- delete goods.shop;
- // goods: 商品信息; specs:商品规格信息; shop:店铺信息
- const returnData = { goods, specs, shop };
- return returnData;
- }
- }
- module.exports = GoodsService;
|