bedroom.js 5.5 KB

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