Explorar el Código

增加分寝接口

liuyu hace 5 años
padre
commit
42e24fe036
Se han modificado 4 ficheros con 69 adiciones y 0 borrados
  1. 5 0
      app/controller/bedroom.js
  2. 1 0
      app/model/student.js
  3. 1 0
      app/router.js
  4. 62 0
      app/service/bedroom.js

+ 5 - 0
app/controller/bedroom.js

@@ -13,6 +13,11 @@ class BedroomController extends Controller {
     this.service = this.ctx.service.bedroom;
   }
 
+  // 一键分寝
+  async apart() {
+    const res = await this.service.apart(this.ctx.body);
+    this.ctx.ok({ data: res });
+  }
 
 }
 

+ 1 - 0
app/model/student.js

@@ -25,6 +25,7 @@ const StudentSchema = {
   batchid: { type: String, required: false, maxLength: 200 }, // 批次
   classid: { type: String, required: false, maxLength: 200 }, // 班级
   bedroomid: { type: String, required: false, maxLength: 200 }, // 寝室id
+  bedroom: { type: String, required: false, maxLength: 200 }, // 寝室号
   is_fine: { type: String, required: false, maxLength: 200 }, // 是否优秀,0-否,1-是
 }
   ;

+ 1 - 0
app/router.js

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

+ 62 - 0
app/service/bedroom.js

@@ -11,6 +11,68 @@ class BedroomService extends CrudService {
   constructor(ctx) {
     super(ctx, 'bedroom');
     this.model = this.ctx.model.Bedroom;
+    this.umodel = this.ctx.model.Student;
+    this.tmodel = this.ctx.model.Trainplan;
+  }
+
+  // 一键分寝
+  async apart(data) {
+    const { trainplanid, termid, batchid } = data;
+
+    // 根据计划id取得当前计划
+    const trainplan = await this.tmodel.findById({ id: trainplanid });
+    // 根据期id取得当前期信息
+    const term = trainplan.termnum.find(p => p.id === termid);
+    // 根据批次id查询批次信息
+    const _batch = term.batchnum.find(p => p.id === batchid);
+    // 查询所有寝室列表
+    const bedroomList = await this.model.find({ batch: _batch.batch });
+
+    // 循环所有当前批次下的寝室列表进行分寝处理
+    for (const bedroom of bedroomList) {
+      // 判断当前寝室号是否已有
+      // 根据期id查找所有当期学生列表
+      const studentList = await this.getstudents(termid, batchid);
+      const _stu = studentList.find(p => p.bedroomid === bedroom.id);
+      if (bedroom.number !== _stu.length) {
+        let i = 0;
+        let _gender = '';
+        for (const stud of studentList) {
+          if (i === 0) {
+            if (!bedroom.gender) {
+              stud.bedroomid = bedroom.id;
+              await stud.save();
+              i = i + 1;
+              _gender = stud.gender;
+            } else {
+              if (bedroom.gender === stud.gender) {
+                stud.bedroomid = bedroom.id;
+                await stud.save();
+                i = i + 1;
+                _gender = stud.gender;
+              }
+            }
+          } else if (i < bedroom.number) {
+            if (_gender === stud.gender) {
+              stud.bedroomid = bedroom.id;
+              await stud.save();
+              i = i + 1;
+            }
+          } else if (i === bedroom.number) {
+            i = 0;
+            break;
+          }
+        }
+      }
+    }
+
+  }
+
+  // 取得符合条件的学生列表
+  async getstudents(termid, batchid) {
+    // 根据期id查找所有当期学生列表
+    const studentList = await this.umodel.find({ termid, batchid });
+    return studentList;
   }
 }