lrf 2 年之前
父節點
當前提交
65770db40a

+ 3 - 0
app/controller/apply/config/.tryLessonApply.js

@@ -38,4 +38,7 @@ module.exports = {
       count: true,
     },
   },
+  checkCanUse: {
+    requestBody: ['school_id', 'lesson_id', 'student_id'],
+  },
 };

+ 13 - 6
app/service/apply/tryLessonApply.js

@@ -11,16 +11,23 @@ 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;
+  // 检查该学员是否可以报该节课
+  async checkCanUse({ school_id, lesson_id, student_id }) {
+    // 先检查是否用过试听机会
+    let num = await this.model.count({ school_id, student_id, result: '1' });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '您已试听过课程,无法再次申请');
     // 该学校,该节课,这名学员. 申请试听的申请状态为 未审核或审核通过时
-    let num = await this.model.count({ school_id, lesson_id, student_id, result: '0' });
+    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 true;
+  }
+
+
+  async beforeCreate(body) {
+    const { school_id, lesson_id, student_id } = body;
+    await this.checkCanUse({ school_id, lesson_id, student_id });
     return body;
   }
 

+ 1 - 0
app/z_router/apply/tryLessonApply.js

@@ -7,6 +7,7 @@ const rkey = 'tryLessonApply';
 const ckey = 'apply.tryLessonApply';
 const keyZh = '试课申请';
 const routes = [
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.checkCanUse`, name: `${ckey}checkCanUse`, zh: `检查是否可以报名${keyZh}` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },