student.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. if (batchid) {
  24. const students = await this.model.find({ batchid });
  25. const bedroomList = new Set();
  26. for (const student of students) {
  27. bedroomList.add(student.bedroom);
  28. }
  29. let studentList = [];
  30. for (const bedroom of bedroomList) {
  31. const newstudents = await this.model.find({ bedroom });
  32. for (const newstudent of newstudents) {
  33. studentList.push(newstudent.name);
  34. }
  35. result.push({ bedroom, studentList });
  36. studentList = [];
  37. }
  38. }
  39. if (classid) {
  40. const students = await this.model.find({ classid });
  41. const bedroomList = new Set();
  42. for (const student of students) {
  43. bedroomList.add(student.bedroom);
  44. }
  45. let studentList = [];
  46. for (const bedroom of bedroomList) {
  47. const newstudents = await this.model.find({ bedroom });
  48. for (const newstudent of newstudents) {
  49. if (newstudent.classid === classid) {
  50. studentList.push(newstudent.name);
  51. }
  52. }
  53. result.push({ bedroom, studentList });
  54. studentList = [];
  55. }
  56. }
  57. return result;
  58. }
  59. }
  60. module.exports = StudentService;