bedroom.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class BedroomService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'bedroom');
  10. this.model = this.ctx.model.Bedroom;
  11. this.umodel = this.ctx.model.Student;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. }
  14. // 一键分寝
  15. async apart(data) {
  16. const { trainplanid, termid, batchid } = data;
  17. assert(trainplanid, 'trainplanid不能为空');
  18. assert(termid, 'termid不能为空');
  19. assert(batchid, 'batchid不能为空');
  20. // 根据计划id取得当前计划
  21. const trainplan = await this.tmodel.findById(trainplanid);
  22. // 根据期id取得当前期信息
  23. const term = trainplan.termnum.find(p => p.id === termid);
  24. // 根据批次id查询批次信息
  25. const _batch = term.batchnum.find(p => p.id === batchid);
  26. // 查询所有寝室列表
  27. const bedroomList = await this.model.find({ batch: _batch.batch });
  28. // 循环所有当前批次下的寝室列表进行分寝处理
  29. const studentList = await this.getstudents(termid, batchid);
  30. console.log('stulen-->' + studentList.length);
  31. for (const bedroom of bedroomList) {
  32. console.log(bedroom.code);
  33. // 判断当前寝室号是否已有
  34. // 根据期id查找所有当期学生列表
  35. const _stu = await this.getbedroomstudents(termid, batchid, bedroom.id);
  36. console.log(_stu.length);
  37. if (bedroom.number !== _stu.length) {
  38. let i = 0;
  39. let _gender = '';
  40. for (const stud of studentList) {
  41. console.log('stu---' + stud.bedroom);
  42. if (stud.bedroomid) {
  43. if (stud.bedroomid === bedroom.id) {
  44. i = i + 1;
  45. }
  46. continue;
  47. }
  48. console.log('i--->' + i);
  49. if (i === 0) {
  50. if (!bedroom.gender) {
  51. stud.bedroomid = bedroom.id;
  52. stud.bedroom = bedroom.code;
  53. await stud.save();
  54. i = i + 1;
  55. _gender = stud.gender;
  56. } else {
  57. if (bedroom.gender === stud.gender) {
  58. stud.bedroomid = bedroom.id;
  59. stud.bedroom = bedroom.code;
  60. await stud.save();
  61. i = i + 1;
  62. _gender = stud.gender;
  63. }
  64. }
  65. } else if (i < bedroom.number) {
  66. if (_gender === stud.gender) {
  67. stud.bedroomid = bedroom.id;
  68. stud.bedroom = bedroom.code;
  69. await stud.save();
  70. i = i + 1;
  71. }
  72. } else if (i === bedroom.number) {
  73. i = 0;
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. // 取得符合条件的学生列表
  81. async getstudents(termid, batchid) {
  82. // 根据期id查找所有当期学生列表
  83. const studentList = await this.umodel.find({ termid, batchid });
  84. return studentList;
  85. }
  86. // 取得符合条件的学生列表
  87. async getbedroomstudents(termid, batchid, bedroomid) {
  88. // 根据期id查找所有当期学生列表
  89. const studentList = await this.umodel.find({ termid, batchid, bedroomid });
  90. return studentList;
  91. }
  92. }
  93. module.exports = BedroomService;