Ver código fonte

服务端更新

reloaded 5 anos atrás
pai
commit
4ec941082f

+ 7 - 7
app/controller/.class.js

@@ -1,10 +1,10 @@
 module.exports = {
   create: {
     requestBody: [
-      'name',
+      '!name',
       'number',
-      'batch',
-      'term',
+      'batchid',
+      'termid',
       'headteacherid',
       'lyteacherid',
       'type'
@@ -19,8 +19,8 @@ module.exports = {
     requestBody: [
       'name',
       'number',
-      'batch',
-      'term',
+      'batchid',
+      'termid',
       'headteacherid',
       'lyteacherid',
       'type'
@@ -37,8 +37,8 @@ module.exports = {
       query: {
         name: 'name',
         number  :'number',
-        batch :'batch',
-        term: 'term',
+        batchid:'batchid',
+        termid: 'termid',
         headteacherid : 'headteacherid',
         lyteacherid : 'lyteacherid',
         type: 'type'

+ 9 - 9
app/controller/.student.js

@@ -18,9 +18,9 @@ module.exports = {
       'family_is_hard',
       'have_grant',
       'job',
-      'term',
-      'batch',
-      'class',
+      'termid',
+      'batchid',
+      'classid',
       'bedroomid',
       'is_fine'
     ]
@@ -49,9 +49,9 @@ module.exports = {
       'family_is_hard',
       'have_grant',
       'job',
-      'term',
-      'batch',
-      'class',
+      'termid',
+      'batchid',
+      'classid',
       'bedroomid',
       'is_fine'
     ]
@@ -82,9 +82,9 @@ module.exports = {
         family_is_hard:'family_is_hard',
         have_grant :'have_grant',
         job:'job',
-        term :'term',
-        batch :'batch',
-        class :'class',
+        termid:'termid',
+        batchid:'batchid',
+        classid:'classid',
         bedroomid : 'bedroomid',
         is_fine:'is_fine'
       }

+ 4 - 0
app/controller/class.js

@@ -13,6 +13,10 @@ class ClassController extends Controller {
     this.service = this.ctx.service.class;
   }
 
+  async divide() {
+    const res = await this.service.divide(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
 
 }
 

+ 2 - 2
app/model/class.js

@@ -6,8 +6,8 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 const ClassSchema = {
   name: { type: String, required: true, maxLength: 500 }, // 班级名称
   number: { type: String, required: false, maxLength: 200 }, // 人数
-  batch: { type: String, required: false, maxLength: 200 }, // 批次
-  term: { type: String, required: false, maxLength: 200 }, // 期
+  batchid: { type: String, required: false, maxLength: 200 }, // 批次
+  termid: { type: String, required: false, maxLength: 200 }, // 期
   headteacherid: { type: String, required: false, maxLength: 200 }, // 班主任id
   lyteacherid: { type: String, required: false, maxLength: 200 }, // 礼仪课老师id
   type: { type: String, required: false, maxLength: 200 }, // 类型:0-正常,1-特殊

+ 3 - 3
app/model/student.js

@@ -21,9 +21,9 @@ const StudentSchema = {
   family_is_hard: { type: String, required: false, maxLength: 200 }, // 家庭是否困难,0-否,1-是
   have_grant: { type: String, required: false, maxLength: 200 }, // 是否获得过助学金,0-否,1-是
   job: { type: String, required: false, maxLength: 200 }, // 职务
-  term: { type: String, required: false, maxLength: 200 }, // 期
-  batch: { type: String, required: false, maxLength: 200 }, // 批次
-  class: { type: String, required: false, maxLength: 200 }, // 班级
+  termid: { type: String, required: false, maxLength: 200 }, // 期
+  batchid: { type: String, required: false, maxLength: 200 }, // 批次
+  classid: { type: String, required: false, maxLength: 200 }, // 班级
   bedroomid: { type: String, required: false, maxLength: 200 }, // 寝室id
   is_fine: { type: String, required: false, maxLength: 200 }, // 是否优秀,0-否,1-是
 }

+ 1 - 0
app/router.js

@@ -54,6 +54,7 @@ module.exports = app => {
   // 班级表设置路由
   router.resources('class', '/api/train/class', controller.class); // index、create、show、destroy
   router.post('class', '/api/train/class/update/:id', controller.class.update);
+  router.post('class', '/api/train/class/divide', controller.class.divide);
 
   // 部门表设置路由
   router.resources('department', '/api/train/department', controller.department); // index、create、show、destroy

+ 27 - 2
app/service/class.js

@@ -11,10 +11,35 @@ class ClassService extends CrudService {
   constructor(ctx) {
     super(ctx, 'class');
     this.model = this.ctx.model.Class;
+    this.stumodel = this.ctx.model.Student;
   }
 
-
-
+  async divide(data) {
+    const termid = await data.termid;
+    const batchid = await data.batchid;
+    const name = await data.classname;
+    const number = await data.students.length;
+    const type = await data.type;
+    const classdata = { name, batchid, termid, number, type };
+    // 根据班级名称查询班级是否已存在
+    const newclass = await this.model.findOne({ name });
+    // 如果已经存在抛出异常
+    if (newclass) {
+      throw new BusinessError(ErrorCode.DATA_EXIST, '班级名称已存在');
+    }
+    // 如果班级不存在则创建班级
+    const newdata = await this.model.create(classdata);
+    // 查询班级id
+    const classid = newdata._id;
+    // 遍历学生id
+    for (const studentid of data.students) {
+      // 根据学生id找到学生数据修改其中的class字段
+      const student = await this.stumodel.findById(studentid);
+      student.classid = classid;
+      student.batchid = batchid;
+      await student.save();
+    }
+  }
 }
 
 module.exports = ClassService;