bedroom.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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({ id: 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. for (const bedroom of bedroomList) {
  30. // 判断当前寝室号是否已有
  31. // 根据期id查找所有当期学生列表
  32. const studentList = await this.getstudents(termid, batchid);
  33. const _stu = studentList.find(p => p.bedroomid === bedroom.id);
  34. if (bedroom.number !== _stu.length) {
  35. let i = 0;
  36. let _gender = '';
  37. for (const stud of studentList) {
  38. if (i === 0) {
  39. if (!bedroom.gender) {
  40. stud.bedroomid = bedroom.id;
  41. stud.bedroom = bedroom.number;
  42. await stud.save();
  43. i = i + 1;
  44. _gender = stud.gender;
  45. } else {
  46. if (bedroom.gender === stud.gender) {
  47. stud.bedroomid = bedroom.id;
  48. stud.bedroom = bedroom.number;
  49. await stud.save();
  50. i = i + 1;
  51. _gender = stud.gender;
  52. }
  53. }
  54. } else if (i < bedroom.number) {
  55. if (_gender === stud.gender) {
  56. stud.bedroomid = bedroom.id;
  57. stud.bedroom = bedroom.number;
  58. await stud.save();
  59. i = i + 1;
  60. }
  61. } else if (i === bedroom.number) {
  62. i = 0;
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. // 取得符合条件的学生列表
  70. async getstudents(termid, batchid) {
  71. // 根据期id查找所有当期学生列表
  72. const studentList = await this.umodel.find({ termid, batchid });
  73. return studentList;
  74. }
  75. }
  76. module.exports = BedroomService;