|
@@ -11,6 +11,31 @@ class TryLessonApplyService extends CrudService {
|
|
|
this.model = this.ctx.model.Apply.TryLessonApply;
|
|
|
this.lessonStudentService = this.ctx.service.business.lessonStudent;
|
|
|
}
|
|
|
+ async beforeCreate(body) {
|
|
|
+ const { school_id, lesson_id, student_id } = body;
|
|
|
+ // 该学校,该节课,这名学员. 申请试听的申请状态为 未审核或审核通过时
|
|
|
+ let num = await this.model.count({ school_id, lesson_id, student_id, result: '0' });
|
|
|
+ if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '本节课的试听申请在审核中,无法重复申请');
|
|
|
+ num = await this.model.count({ school_id, lesson_id, student_id, result: '1' });
|
|
|
+ if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '本节课的试听申请已通过,无法重复申请');
|
|
|
+ // 查询这个学员在该学校是否使用过试听
|
|
|
+ num = await this.model.count({ school_id, student_id, result: '1' });
|
|
|
+ if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该学员已经在本校试听过课程,无法重复申请');
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ async beforeUpdate(filter, update) {
|
|
|
+ // 不是 审核通过的情况就不需要继续检查是否使用过试听名额了
|
|
|
+ if (update.result !== '1') return { filter, update };
|
|
|
+ const data = await this.model.findById(filter.id);
|
|
|
+ if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
|
|
|
+ const { school_id, student_id } = data;
|
|
|
+ const num = await this.model.count({ school_id, student_id, result: '1' });
|
|
|
+ if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该学员已经在本校试听过课程,无法重复申请');
|
|
|
+ return { filter, update };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
// 处理审核成功的情况
|
|
|
async afterUpdate(filter, body, data) {
|
|
|
const { result } = data;
|