'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 PlatformActService extends CrudService { constructor(ctx) { super(ctx, 'platformact'); this.model = this.ctx.model.System.PlatformAct; this.gjaModel = this.ctx.model.Shop.GoodsJoinAct; } async query(filter, { skip = 0, limit, projection } = {}) { let condition = _.cloneDeep(filter); condition = await this.beforeQuery(condition); condition = this.dealFilter(condition); // 过滤出ref字段 const { populate } = this.getRefMods(); // 带ref查询 let rs = await this.model .find(condition, projection, { skip, limit, sort: { sort: 1, 'meta.createdAt': -1 } }) .populate(populate) .exec(); rs = JSON.parse(JSON.stringify(rs)); // 整理ref数据 rs = await this.afterQuery(filter, rs); return rs; } getActText(data) { const { type } = data; let tag = ''; let text = ''; if (type === '2') { tag = '赠品'; } else if (type === '3') { tag = '特价'; } else if (type === '4') { tag = '加价购'; } else if (type === '5') { tag = '满减'; const discount = _.get(data, 'config.discount', []); for (const i of discount) { const { limit, number } = i; text = `${text}满${limit}减${number};`; } } else if (type === '6') { tag = '满折'; const discount = _.get(data, 'config.discount', []); for (const i of discount) { const { limit, number, max } = i; text = `满${limit}打${number}折`; if (max) text = `${text}(最多减免${max}元)`; text = `${text};`; } } else if (type === '7') { tag = '套装'; } return { tag, text }; } async beforeUpdate(filter, update) { const id = _.get(filter, 'id'); const num = await this.gjaModel.count({ platform_act: id }); // 如果活动已经有商品关联了.那就不能修改活动类型了 if (num > 0) delete update.type; return { filter, update }; } } module.exports = PlatformActService;