bedroom.js 4.8 KB

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