student.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 StudentService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'student');
  10. this.model = this.ctx.model.Student;
  11. this.umodel = this.ctx.model.User;
  12. }
  13. // 查询
  14. async seek({ termid, skip, limit }) {
  15. const students = await this.model.find({ termid, classid: null });
  16. const data = await this.model.find({ termid, classid: null }).skip(Number(skip)).limit(Number(limit));
  17. const total = await students.length;
  18. const result = { total, data };
  19. return result;
  20. }
  21. async findbedroom(data) {
  22. const { batchid, classid } = data;
  23. const result = [];
  24. // 如果传的是批次id
  25. if (batchid) {
  26. // 查询该批次下的所有学生
  27. const students = await this.model.find({ batchid });
  28. const bedroomList = new Set();
  29. // 查询该批次的所有寝室号
  30. for (const student of students) {
  31. bedroomList.add(student.bedroom);
  32. }
  33. let studentList = [];
  34. // 查询该批次所有寝室下的学生名单
  35. for (const bedroom of bedroomList) {
  36. const newstudents = await this.model.find({ bedroom });
  37. for (const newstudent of newstudents) {
  38. studentList.push(newstudent.name);
  39. }
  40. result.push({ bedroom, studentList });
  41. studentList = [];
  42. }
  43. }
  44. // 如果传的是班级id
  45. if (classid) {
  46. // 查询该班级所有学生
  47. const students = await this.model.find({ classid });
  48. const bedroomList = new Set();
  49. // 查询该班级所有寝室号
  50. for (const student of students) {
  51. bedroomList.add(student.bedroom);
  52. }
  53. let studentList = [];
  54. // 查询该班级所有寝室的学生名单
  55. for (const bedroom of bedroomList) {
  56. const newstudents = await this.model.find({ bedroom });
  57. for (const newstudent of newstudents) {
  58. // 如果寝室中有非本班级学生(混寝),则过滤掉不予显示
  59. if (newstudent.classid === classid) {
  60. studentList.push(newstudent.name);
  61. }
  62. }
  63. result.push({ bedroom, studentList });
  64. studentList = [];
  65. }
  66. }
  67. return result;
  68. }
  69. async upjob(data) {
  70. const { stuid, job } = data;
  71. const student = await this.model.findById(stuid);
  72. student.job = job;
  73. if (job === '班长' || job === '学委') {
  74. const user = await this.umodel.findOne({ uid: stuid, type: '4' });
  75. const date = await this.ctx.service.util.updatedate();
  76. const openid = user.openid;
  77. const detail = '你已被班主任设置为' + job + ',请及时登录查看';
  78. const remark = '感谢您的使用';
  79. if (openid) {
  80. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  81. }
  82. }
  83. return await student.save();
  84. }
  85. }
  86. module.exports = StudentService;