bedroom.js 6.0 KB

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