1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- '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;
- }
- /**
- * 获取设置
- * @param {PlatformAct} data 活动数据
- * @param {Goods} goods 商品数据
- */
- async getActText(data, goods) {
- const { type } = data;
- let tag = '';
- let text = '';
- const aboutList = [];
- if (type === '2') {
- tag = '赠品';
- // 寻找赠品信息
- const list = await this.gjaModel.find({ platform_act: data._id, 'goods._id': goods._id });
- for (const i of list) {
- const gift = _.get(i, 'config.gift', []);
- const goods = _.get(i, 'goods._id');
- const spec = _.get(i, 'spec._id');
- aboutList.push({ goods, spec, gift });
- }
- } else if (type === '3') {
- tag = '特价';
- // 寻找商品的特价
- const list = await this.gjaModel.find({ platform_act: data._id, 'goods._id': goods._id });
- for (const i of list) {
- const goods = _.get(i, 'goods._id');
- const spec = _.get(i, 'spec._id');
- const price = _.get(i, 'config.sp_price');
- aboutList.push({ goods, spec, price });
- }
- } 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};`;
- }
- text = _.trimEnd(text, ';');
- } else if (type === '6') {
- tag = '满折';
- const discount = _.get(data, 'config.discount', []);
- for (const i of discount) {
- const { limit, number, max } = i;
- text = `${text}满${limit}打${number}折`;
- if (max) text = `${text}(最多减免${max}元)`;
- text = `${text};`;
- }
- text = _.trimEnd(text, ';');
- } else if (type === '7') {
- tag = '套装';
- }
- return { tag, text, aboutList };
- }
- 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;
|