bedroom.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.smodel = this.ctx.model.Student;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. this.cmodel = this.ctx.model.Class;
  14. this.umodel = this.ctx.model.User;
  15. }
  16. // 一键分寝
  17. async apart(data) {
  18. const { trainplanid, termid, batchid } = data;
  19. assert(trainplanid, 'trainplanid不能为空');
  20. assert(termid, 'termid不能为空');
  21. assert(batchid, 'batchid不能为空');
  22. // 根据计划id取得当前计划
  23. const trainplan = await this.tmodel.findById(trainplanid);
  24. // 根据期id取得当前期信息
  25. const term = trainplan.termnum.find(p => p.id === termid);
  26. // 根据批次id查询批次信息
  27. const _batch = term.batchnum.find(p => p.id === batchid);
  28. // 查询所有寝室列表
  29. const bedroomList = await this.model.find({ batch: _batch.batch }).sort({ floor: -1 });
  30. // 循环所有当前批次下的寝室列表进行分寝处理
  31. const studentList = await this.getstudents(termid, batchid);
  32. console.log('stulen-->' + studentList.length);
  33. for (const bedroom of bedroomList) {
  34. console.log(bedroom.code);
  35. // 判断当前寝室号是否已有
  36. // 根据期id查找所有当期学生列表
  37. const _stu = await this.getbedroomstudents(termid, batchid, bedroom.id);
  38. console.log(_stu.length);
  39. if (bedroom.number !== _stu.length) {
  40. let i = 0;
  41. let _gender = '';
  42. for (const stud of studentList) {
  43. console.log('stu---' + stud.bedroom);
  44. if (stud.bedroomid) {
  45. if (stud.bedroomid === bedroom.id) {
  46. i = i + 1;
  47. }
  48. continue;
  49. }
  50. console.log('i--->' + i);
  51. if (i === 0) {
  52. if (!bedroom.gender) {
  53. stud.bedroomid = bedroom.id;
  54. stud.bedroom = bedroom.code;
  55. await stud.save();
  56. i = i + 1;
  57. _gender = stud.gender;
  58. } else {
  59. if (bedroom.gender === stud.gender) {
  60. stud.bedroomid = bedroom.id;
  61. stud.bedroom = bedroom.code;
  62. await stud.save();
  63. i = i + 1;
  64. _gender = stud.gender;
  65. }
  66. }
  67. } else if (i < bedroom.number) {
  68. if (_gender === stud.gender) {
  69. stud.bedroomid = bedroom.id;
  70. stud.bedroom = bedroom.code;
  71. await stud.save();
  72. i = i + 1;
  73. }
  74. } else if (i === bedroom.number) {
  75. i = 0;
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. // 取得当前批次的所有班级
  82. const classes = await this.cmodel.find({ batchid });
  83. for (const _class of classes) {
  84. // 取得每个班级的班主任id
  85. const headteacherid = _class.headteacherid;
  86. const headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  87. const openid = headteacher.openid;
  88. const remark = '感谢您的使用';
  89. const date = await this.ctx.service.util.updatedate();
  90. const detail = '班级学生名单与寝室安排已确认,请及时查收';
  91. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, _class.id);
  92. }
  93. }
  94. // 取得符合条件的学生列表
  95. async getstudents(termid, batchid) {
  96. // 根据期id查找所有当期学生列表
  97. const studentList = await this.smodel.find({ termid, batchid }).sort({ gender: -1 });
  98. return studentList;
  99. }
  100. // 取得符合条件的学生列表
  101. async getbedroomstudents(termid, batchid, bedroomid) {
  102. // 根据期id查找所有当期学生列表
  103. const studentList = await this.smodel.find({ termid, batchid, bedroomid });
  104. return studentList;
  105. }
  106. }
  107. module.exports = BedroomService;