bedroom.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // 根据计划id取得当前计划
  18. const trainplan = await this.tmodel.findById({ id: trainplanid });
  19. // 根据期id取得当前期信息
  20. const term = trainplan.termnum.find(p => p.id === termid);
  21. // 根据批次id查询批次信息
  22. const _batch = term.batchnum.find(p => p.id === batchid);
  23. // 查询所有寝室列表
  24. const bedroomList = await this.model.find({ batch: _batch.batch });
  25. // 循环所有当前批次下的寝室列表进行分寝处理
  26. for (const bedroom of bedroomList) {
  27. // 判断当前寝室号是否已有
  28. // 根据期id查找所有当期学生列表
  29. const studentList = await this.getstudents(termid, batchid);
  30. const _stu = studentList.find(p => p.bedroomid === bedroom.id);
  31. if (bedroom.number !== _stu.length) {
  32. let i = 0;
  33. let _gender = '';
  34. for (const stud of studentList) {
  35. if (i === 0) {
  36. if (!bedroom.gender) {
  37. stud.bedroomid = bedroom.id;
  38. stud.bedroom = bedroom.number;
  39. await stud.save();
  40. i = i + 1;
  41. _gender = stud.gender;
  42. } else {
  43. if (bedroom.gender === stud.gender) {
  44. stud.bedroomid = bedroom.id;
  45. stud.bedroom = bedroom.number;
  46. await stud.save();
  47. i = i + 1;
  48. _gender = stud.gender;
  49. }
  50. }
  51. } else if (i < bedroom.number) {
  52. if (_gender === stud.gender) {
  53. stud.bedroomid = bedroom.id;
  54. stud.bedroom = bedroom.number;
  55. await stud.save();
  56. i = i + 1;
  57. }
  58. } else if (i === bedroom.number) {
  59. i = 0;
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. // 取得符合条件的学生列表
  67. async getstudents(termid, batchid) {
  68. // 根据期id查找所有当期学生列表
  69. const studentList = await this.umodel.find({ termid, batchid });
  70. return studentList;
  71. }
  72. }
  73. module.exports = BedroomService;