bedroom.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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
  66. .find({ batch: _batch.batch, status: '0' })
  67. .sort({ floor: -1 });
  68. // 循环所有当前批次下的寝室列表进行分寝处理
  69. const studentList = await this.getstudents(termid, batchid);
  70. console.log('stulen-->' + studentList.length);
  71. for (const bedroom of bedroomList) {
  72. console.log(bedroom.code);
  73. // 判断当前寝室号是否已有
  74. // 根据期id查找所有当期学生列表
  75. const _stu = _.filter(studentList, { bedroom: bedroom.code });
  76. console.log(_stu.length);
  77. if (bedroom.number !== _stu.length) {
  78. let i = 0;
  79. let _gender = '';
  80. for (const stud of studentList) {
  81. console.log('stu---' + stud.bedroom);
  82. if (stud.bedroom) {
  83. if (stud.bedroom === bedroom.code) {
  84. i = i + 1;
  85. }
  86. continue;
  87. }
  88. console.log('i--->' + i);
  89. if (i === 0) {
  90. if (!bedroom.gender) {
  91. stud.bedroomid = bedroom.id;
  92. stud.bedroom = bedroom.code;
  93. await stud.save();
  94. i = i + 1;
  95. _gender = stud.gender;
  96. } else {
  97. if (bedroom.gender === stud.gender) {
  98. stud.bedroomid = bedroom.id;
  99. stud.bedroom = bedroom.code;
  100. await stud.save();
  101. i = i + 1;
  102. _gender = stud.gender;
  103. }
  104. }
  105. } else if (i < bedroom.number) {
  106. if (_gender === stud.gender) {
  107. stud.bedroomid = bedroom.id;
  108. stud.bedroom = bedroom.code;
  109. await stud.save();
  110. i = i + 1;
  111. }
  112. } else if (i === bedroom.number) {
  113. i = 0;
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. // 取得当前批次的所有班级
  120. const classes = await this.cmodel.find({ batchid });
  121. const detail = '班级学生名单与寝室安排已确认,请及时查收';
  122. const nres = await this.nmodel.create({
  123. planyearid: trainplan.planyearid,
  124. planid: trainplanid,
  125. termid,
  126. noticeid: 'system',
  127. content: detail,
  128. type: '5',
  129. });
  130. for (const _class of classes) {
  131. // 取得每个班级的班主任id
  132. const headteacherid = _class.headteacherid;
  133. const headteacher = await this.umodel.findOne({
  134. uid: headteacherid,
  135. type: '1',
  136. });
  137. if (headteacher && headteacher.openid) {
  138. const openid = headteacher.openid;
  139. const remark = '感谢您的使用';
  140. const date = await this.ctx.service.util.updatedate();
  141. this.ctx.service.weixin.sendTemplateMsg(
  142. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  143. openid,
  144. '您有一个新的通知',
  145. detail,
  146. date,
  147. remark,
  148. _class.id
  149. );
  150. nres.notified.push({
  151. notifiedid: headteacher.uid,
  152. username: headteacher.name,
  153. });
  154. }
  155. await nres.save();
  156. }
  157. }
  158. // 取得符合条件的学生列表
  159. async getstudents(termid, batchid) {
  160. // 根据期id查找所有当期学生列表
  161. const cltype = await this.ctmodel.find({ bedroom: '0' });
  162. const types = _.map(cltype, 'code');
  163. console.log('-----types-start--');
  164. console.log(types);
  165. console.log('-----types-end--');
  166. const studentList = await this.smodel
  167. .find({ termid, batchid, type: { $in: types } })
  168. .sort({ gender: -1 });
  169. return studentList;
  170. }
  171. // 取得符合条件的学生列表
  172. async getbedroomstudents(termid, batchid, bedroom) {
  173. // 根据期id查找所有当期学生列表
  174. const studentList = await this.smodel.find({ termid, batchid, bedroom });
  175. return studentList;
  176. }
  177. // 新 分配寝室查询可以的列表
  178. async getAssignRoom({ termid }) {
  179. console.log(termid);
  180. const bedroomList = await this.model.find();
  181. const stuList = await this.smodel.find({ termid });
  182. const stuBedIdGroup = _.groupBy(stuList, 'bedroomid');
  183. const keys = Object.keys(stuBedIdGroup);
  184. // 过滤出没有人的寝室
  185. let noperson = bedroomList.filter(f => !(keys.find(k => {
  186. if (k === undefined || k === 'undefined') return false;
  187. return ObjectId(k).equals(f._id);
  188. })));
  189. noperson = JSON.parse(JSON.stringify(noperson));
  190. const nopersonList = [];
  191. for (const i of noperson) {
  192. const n = `${i.code}(${i.number}人)`;
  193. const obj = { ...i, name: n };
  194. nopersonList.push(obj);
  195. }
  196. const havepersonList = [];
  197. for (const key of keys) {
  198. if (key === undefined || key === 'undefined') continue;
  199. // 取出分组后,以寝室id为key的学生列表
  200. const list = stuBedIdGroup[key];
  201. // 找到寝室obj
  202. const bedroom = bedroomList.find(f => ObjectId(key).equals(f._id));
  203. // 找不到寝室就算了,下一个吧
  204. if (!bedroom) continue;
  205. // 找到这个寝室人数限制
  206. const { number } = bedroom;
  207. // 超出,等于限制人数,继续下个寝室
  208. if (list.length >= number * 1) continue;
  209. // 这个寝室人没满,需要组织数据了
  210. const { _id, code, floor } = bedroom;
  211. const elsenum = number * 1 - list.length;
  212. const stu = _.head(stuList);
  213. const { gender } = stu;
  214. let ncode = `${code}(`;
  215. if (elsenum) ncode = `${ncode} 剩余${elsenum}人`;
  216. if (gender) ncode = `${ncode} ${gender}性`;
  217. if (floor)ncode = `${ncode} ${floor}楼`;
  218. ncode = `${ncode})`;
  219. const obj = { _id, code, name: ncode };
  220. havepersonList.push(obj);
  221. }
  222. return [ ...nopersonList, ...havepersonList ];
  223. }
  224. // 批量修改学生寝室(新) TODO,需要添加期id,然后找这个寝室,这期同学是否占满/性别错误的问题
  225. async updateStudent(data, body) {
  226. const { code, ids, bedroomid, termid } = body;
  227. const bedroom = await this.model.findById(bedroomid);
  228. if (!bedroom) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '不存在该寝室');
  229. let { number, gender } = bedroom;
  230. console.log(gender);
  231. // 找到多少人在这个寝室
  232. const inRoom = await this.ctx.model.Student.find({ termid, bedroomid });
  233. const ifTotal = inRoom.length * 1 + ids.length;
  234. if (ifTotal > number * 1) throw new BusinessError(ErrorCode.BUSINESS, `超出人数,该寝室人最多为${number}人`);
  235. if (!gender) {
  236. // 寝室没设置性别,从这个寝室的学生中取出性别
  237. const stu = _.head(inRoom);
  238. gender = _.get(stu, 'gender');
  239. }
  240. console.log(gender);
  241. for (const id of ids) {
  242. const r = await this.ctx.model.Student.findById(id);
  243. // 性别查询,是否有误
  244. const { gender: sg, name } = r;
  245. // 寝室或有已入住学生,产生的性别结果,
  246. if (!gender) {
  247. r.bedroom = code;
  248. r.bedroomid = bedroomid;
  249. await r.save();
  250. } else {
  251. if (sg && sg.includes(gender)) {
  252. // 有性别判断
  253. r.bedroom = code;
  254. r.bedroomid = bedroomid;
  255. await r.save();
  256. } else {
  257. throw new BusinessError(ErrorCode.BusinessError, `${name} 与该寝室已分配的学生性别不符!`);
  258. }
  259. }
  260. }
  261. }
  262. }
  263. module.exports = BedroomService;