'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 LessonStudentService extends CrudService { constructor(ctx) { super(ctx, 'lessonstudent'); this.model = this.ctx.model.Business.LessonStudent; } async beforeCreate(body) { const { lesson_id, school_id, student_id } = body; // 检查是否已经有数据 const data = await this.model.findOne({ lesson_id, school_id, student_id }); // 数据已存在,检查是否是申请试听 if (data) { const { is_try } = data; if (is_try === '0') throw new BusinessError(ErrorCode.DATA_EXISTED, '您已经报名'); } // 数据不存在 const { is_try } = body; // 检查是否使用试听名额 if (is_try === '1') { const num = await this.model.count({ student_id, is_try: '1' }); if (num > 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '您已使用过免费试听的机会'); } return body; } } module.exports = LessonStudentService;