lrf402788946 há 4 anos atrás
pai
commit
d9ab91b0c4
3 ficheiros alterados com 60 adições e 3 exclusões
  1. 5 1
      app/controller/.bedroom.js
  2. 2 0
      app/router.js
  3. 53 2
      app/service/bedroom.js

+ 5 - 1
app/controller/.bedroom.js

@@ -52,7 +52,11 @@ module.exports = {
     },
   },
   updateStudent: {
-    requestBody: ["code", "ids", "bedroomid"],
+    requestBody: ["code", "ids", "bedroomid", 'termid'],
     service: "updateStudent",
   },
+  getAssignRoom: {
+    parameters: { query: { termid: "termid" } },
+    service: "getAssignRoom",
+  },
 };

+ 2 - 0
app/router.js

@@ -152,6 +152,8 @@ module.exports = app => {
     '/api/train/headteacher/update/:id',
     controller.headteacher.update
   );
+  // 查询该期还可以使用的寝室
+  router.get('bedroom', '/api/train/bedroom/assignroom', controller.bedroom.getAssignRoom);
 
   // 批量修改寝室(新)
   router.post('bedroom', '/api/train/bedroom/batch', controller.bedroom.updateStudent);

+ 53 - 2
app/service/bedroom.js

@@ -184,9 +184,60 @@ class BedroomService extends CrudService {
     return studentList;
   }
 
-  // 批量修改学生寝室(新)
+  // 新 分配寝室查询可以的列表
+  async getAssignRoom({ termid }) {
+    console.log(termid);
+    const bedroomList = await this.model.find();
+    const stuList = await this.smodel.find({ termid });
+    const stuBedIdGroup = _.groupBy(stuList, 'bedroomid');
+    const keys = Object.keys(stuBedIdGroup);
+    // 过滤出没有人的寝室
+    let noperson = bedroomList.filter(f => !(keys.find(k => ObjectId(k).equals(f._id))));
+    noperson = JSON.parse(JSON.stringify(noperson));
+    const nopersonList = [];
+    for (const i of noperson) {
+      const n = `${i.code}(${i.number}人)`;
+      const obj = { ...i, name: n };
+      nopersonList.push(obj);
+    }
+    const havepersonList = [];
+    for (const key of keys) {
+      // 取出分组后,以寝室id为key的学生列表
+      const list = stuBedIdGroup[key];
+      // 找到寝室obj
+      const bedroom = bedroomList.find(f => ObjectId(key).equals(f._id));
+      // 找不到寝室就算了,下一个吧
+      if (!bedroom) continue;
+      // 找到这个寝室人数限制
+      const { number } = bedroom;
+      // 超出,等于限制人数,继续下个寝室
+      if (list.length >= number * 1) continue;
+      // 这个寝室人没满,需要组织数据了
+      const { _id, code, floor } = bedroom;
+      const elsenum = number * 1 - list.length;
+      const stu = _.head(stuList);
+      const { gender } = stu;
+      let ncode = `${code}(`;
+      if (elsenum) ncode = `${ncode} 剩余${elsenum}人`;
+      if (gender) ncode = `${ncode} ${gender}性`;
+      if (floor)ncode = `${ncode} ${floor}楼`;
+      ncode = `${ncode})`;
+      const obj = { _id, code, name: ncode };
+      havepersonList.push(obj);
+    }
+    return [ ...nopersonList, ...havepersonList ];
+  }
+
+  // 批量修改学生寝室(新) TODO,需要添加期id,然后找这个寝室,这期同学是否占满/性别错误的问题
   async updateStudent(data, body) {
-    const { code, ids, bedroomid } = body;
+    const { code, ids, bedroomid, termid } = body;
+    const bedroom = await this.model.findById(bedroomid);
+    if (!bedroom) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '不存在该寝室');
+    const { number } = bedroom;
+    // 找到多少人在这个寝室
+    const inRoom = await this.ctx.model.Student.count({ termid, bedroomid });
+    const ifTotal = inRoom * 1 + ids.length;
+    if (ifTotal >= number * 1) throw new BusinessError(ErrorCode.BUSINESS, `超出人数,该寝室人最多为${number}人`);
     for (const id of ids) {
       const r = await this.ctx.model.Student.findById(id);
       r.bedroom = code;