bedroom.js 5.7 KB

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