student.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  12. // 查询
  13. async seek({ termid, skip, limit }) {
  14. const students = await this.model.find({ termid, classid: null });
  15. const data = await this.model.find({ termid, classid: null }).skip(Number(skip)).limit(Number(limit));
  16. const total = await students.length;
  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. }
  69. module.exports = StudentService;