lrf 2 år sedan
förälder
incheckning
eb8921b87e

+ 2 - 2
app/controller/relation/config/.relationStudentCoach.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['!student_id', '!coach_id', 'doc', 'config'],
+    requestBody: ['!student_id', '!coach_id', '!school_id', 'doc', 'config'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['student_id', 'coach_id', 'doc', 'config'],
+    requestBody: ['student_id', 'coach_id', '!school_id', 'doc', 'config'],
   },
   show: {
     parameters: {

+ 40 - 0
app/controller/relation/config/.studentApplyForSchool.js

@@ -0,0 +1,40 @@
+module.exports = {
+  create: {
+    requestBody: ['!student_id', '!school_id', 'result', 'time', 'reason'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['student_id', 'school_id', 'result', 'time', 'reason'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        student_id: 'student_id',
+        school_id: 'school_id',
+        result: 'result',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 13 - 0
app/controller/relation/studentApplyForSchool.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.studentApplyForSchool.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class StudentApplyForSchoolController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.relation.studentApplyForSchool;
+  }
+}
+module.exports = CrudController(StudentApplyForSchoolController, meta);

+ 2 - 0
app/model/relation/relationStudentCoach.js

@@ -6,6 +6,7 @@ const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 const relationStudentCoach = {
   student_id: { type: String, required: true, zh: '学生id', ref: 'Student', getProp: [ 'name' ] }, //
   coach_id: { type: String, required: true, zh: '教练id', ref: 'Coach', getProp: [ 'icon', 'name', 'phone', 'age', 'gender', 'level', 'honor' ] }, //
+  school_id: { type: String, required: true, zh: '学校id', ref: 'School', getProp: [ 'name', 'phone' ] }, //
   doc: { type: Object, required: false, zh: '档案' }, //
   config: { type: Object, required: false, zh: '设置' }, //
 };
@@ -14,6 +15,7 @@ schema.index({ id: 1 });
 schema.index({ 'meta.createdAt': 1 });
 schema.index({ student_id: 1 });
 schema.index({ coach_id: 1 });
+schema.index({ school_id: 1 });
 
 schema.plugin(metaPlugin);
 module.exports = app => {

+ 24 - 0
app/model/relation/studentApplyForSchool.js

@@ -0,0 +1,24 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+// 学员入学申请
+const studentApplyForSchool = {
+  student_id: { type: String, required: true, zh: '学生id', ref: 'Student', getProp: [ 'name' ] }, //
+  school_id: { type: String, required: true, zh: '学校id', ref: 'School', getProp: [ 'name' ] }, //
+  result: { type: String, required: false, default: '0', zh: '审核结果' }, // 0-未审核;1-审核通过;-1-审核拒绝
+  time: { type: String, required: false, zh: '申请时间' }, //
+  reason: { type: String, required: false, zh: '审核理由' }, //
+};
+const schema = new Schema(studentApplyForSchool, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ student_id: 1 });
+schema.index({ school_id: 1 });
+schema.index({ result: 1 });
+
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('StudentApplyForSchool', schema, 'studentApplyForSchool');
+};

+ 1 - 1
app/router.js

@@ -31,6 +31,6 @@ module.exports = app => {
   require('./z_router/user/student')(app); // 学员
   require('./z_router/relation/relationCoachSchool')(app); // 教练与学校关系
   require('./z_router/relation/relationStudentSchool')(app); // 学员与学校关系
-  require('./z_router/relation/relationStudentCoach')(app); // 学员与教练关系
+  require('./z_router/relation/studentApplyForSchool')(app); // 学员入学申请
   require('./z_router/business/lesson')(app); // 课程
 };

+ 10 - 0
app/service/relation/relationCoachSchool.js

@@ -10,6 +10,16 @@ class RelationCoachSchoolService extends CrudService {
     super(ctx, 'relationcoachschool');
     this.model = this.ctx.model.Relation.RelationCoachSchool;
   }
+
+  // 创建关系前,先查下有没有,有就别创建了
+  async beforeCreate(data) {
+    const { coach_id, school_id } = data;
+    const num = await this.model.count({ coach_id, school_id });
+    if (num <= 0) {
+      return data;
+    }
+    throw new BusinessError(ErrorCode.DATA_EXISTED, '教练已在学校备案,无需重复添加');
+  }
 }
 
 module.exports = RelationCoachSchoolService;

+ 10 - 0
app/service/relation/relationStudentCoach.js

@@ -10,6 +10,16 @@ class RelationStudentCoachService extends CrudService {
     super(ctx, 'relationstudentcoach');
     this.model = this.ctx.model.Relation.RelationStudentCoach;
   }
+
+  // 创建关系前,先查下有没有,有就别创建了
+  async beforeCreate(data) {
+    const { coach_id, student_id, school_id } = data;
+    const num = await this.model.count({ student_id, coach_id, school_id });
+    if (num <= 0) {
+      return data;
+    }
+    throw new BusinessError(ErrorCode.DATA_EXISTED, '教练已在该学校有与学生的关联,无需重复添加');
+  }
 }
 
 module.exports = RelationStudentCoachService;

+ 7 - 0
app/service/relation/relationStudentSchool.js

@@ -10,6 +10,13 @@ class RelationStudentSchoolService extends CrudService {
     super(ctx, 'relationstudentschool');
     this.model = this.ctx.model.Relation.RelationStudentSchool;
   }
+  // 创建关系前,先查下有没有,有就别创建了
+  async beforeCreate(data) {
+    const { student_id, school_id } = data;
+    const num = await this.model.count({ student_id, school_id });
+    if (num <= 0) { return data; }
+    throw new BusinessError(ErrorCode.DATA_EXISTED, '学员已入学,无需重复添加');
+  }
 }
 
 module.exports = RelationStudentSchoolService;

+ 25 - 0
app/service/relation/studentApplyForSchool.js

@@ -0,0 +1,25 @@
+'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 StudentApplyForSchoolService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'studentapplyforschool');
+    this.model = this.ctx.model.Relation.StudentApplyForSchool;
+    this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
+  }
+
+  async afterUpdate(filter, body, data) {
+    const { result, student_id, school_id } = data;
+    if (result !== '1') return data;
+    // 审核通过,建立关系
+    const obj = { school_id, student_id };
+    await this.rssModel.create(obj);
+    return data;
+  }
+}
+
+module.exports = StudentApplyForSchoolService;

+ 19 - 0
app/z_router/relation/studentApplyForSchool.js

@@ -0,0 +1,19 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'sas';
+const ckey = 'relation.studentApplyForSchool';
+const keyZh = '学员入学申请';
+const routes = [
+  { 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}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};

+ 1 - 1
package.json

@@ -14,7 +14,7 @@
     "egg-scripts": "^2.11.0",
     "lodash": "^4.17.21",
     "moment": "^2.29.1",
-    "naf-framework-mongoose-free": "^0.0.26"
+    "naf-framework-mongoose-free": "^0.0.28"
   },
   "devDependencies": {
     "autod": "^3.0.1",