student.js 3.3 KB

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