Browse Source

分组表新增接口

reloaded 5 years ago
parent
commit
46f00a7524
4 changed files with 28 additions and 0 deletions
  1. 4 0
      app/controller/group.js
  2. 1 0
      app/model/group.js
  3. 1 0
      app/router.js
  4. 22 0
      app/service/group.js

+ 4 - 0
app/controller/group.js

@@ -23,6 +23,10 @@ class GroupController extends Controller {
     this.ctx.ok({ msg: 'ok', data: res });
   }
 
+  async sethead() {
+    const res = await this.service.sethead(this.ctx.request.body);
+    this.ctx.ok({ msg: 'ok', data: res });
+  }
 
 }
 

+ 1 - 0
app/model/group.js

@@ -6,6 +6,7 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 const stuInfo = new Schema({
   stuid: { type: String, required: false, maxLength: 500 }, // 学生id
   stuname: { type: String, required: false, maxLength: 500 }, // 学生姓名
+  type: { type: String, required: false, maxLength: 500, default: '0' }, // 类型,0-组员,1-组长
 });
 
 // 分组表

+ 1 - 0
app/router.js

@@ -97,6 +97,7 @@ module.exports = app => {
   router.post('group', '/api/train/group/update/:id', controller.group.update);
   router.post('group', '/api/train/group/insert', controller.group.insert);
   router.post('group', '/api/train/group/exit', controller.group.exit);
+  router.post('group', '/api/train/group/sethead', controller.group.sethead);
 
   // 职责说明表设置路由
   router.resources('duty', '/api/train/duty', controller.duty); // index、create、show、destroy

+ 22 - 0
app/service/group.js

@@ -11,6 +11,7 @@ class GroupService extends CrudService {
   constructor(ctx) {
     super(ctx, 'group');
     this.model = this.ctx.model.Group;
+    this.smodel = this.ctx.model.Student;
   }
 
   async insert(data) {
@@ -39,6 +40,27 @@ class GroupService extends CrudService {
     }
     await group.save();
   }
+
+  // 设置学生为组长或组员
+  async sethead(data) {
+    const { groupid, stuid, type } = data;
+    const group = await this.model.findById(groupid);
+    const students = group.students;
+    for (const student of students) {
+      if (student.stuid === stuid) {
+        student.type = type;
+      }
+    }
+    await group.save();
+    const student = await this.smodel.findById(stuid);
+    if (type === '1') {
+      student.jod = '组长';
+    }
+    if (type === '0') {
+      student.jod = '普通学生';
+    }
+    await student.save();
+  }
 }