'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 GoodsSetService extends CrudService { constructor(ctx) { super(ctx, 'goodsset'); this.model = this.ctx.model.Shop.GoodsSet; this.goodsModel = this.ctx.model.Shop.Goods; this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec; } async update(filter, update, { projection } = {}) { assert(filter); assert(update); const beforeUpdateResult = await this.beforeUpdate(filter, update); filter = beforeUpdateResult.filter; update = beforeUpdateResult.update; const { _id, id } = filter; if (_id || id) filter = { _id: ObjectId(_id || id) }; // TODO:检查数据是否存在 const entity = await this.model.findOne(filter).exec(); if (!entity) throw new BusinessError(ErrorCode.DATA_NOT_EXIST); await this.model.updateOne(filter, update); let reSearchData = await this.model.findOne(filter, projection).exec(); reSearchData = await this.afterUpdate(filter, update, reSearchData); return reSearchData; } /** * 获取套装快照 * @param id 套装id */ async getSnapshot(id) { const data = await this.model.findById(id).lean(); const { set = [] } = data; for (const s of set) { const { goods, spec } = s; const goodsData = await this.goodsModel.findById(goods).lean(); const goodsSpecData = await this.goodsSpecModel.findById(spec).lean(); s.spec = goodsSpecData; s.goods = goodsData; } // 换成goods data.goods = set; delete data.set; delete data._id; return data; } } module.exports = GoodsSetService;