student.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const { ObjectId } = require('mongoose').Types;
  7. // 学生视图接口
  8. class StudentService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'lessonprivate');
  11. this.lessonModel = this.ctx.model.Business.Lesson;
  12. this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
  13. }
  14. async lessonList({ student_id, school_id, skip = 0, limit = 0, ...otherQuery }) {
  15. // 查出该学生的课表
  16. const query = {};
  17. if (student_id)query.student_id = student_id;
  18. if (school_id) query.school_id = school_id;
  19. const lsList = await this.lessonStudentModel.find(query, { lesson_id: 1, is_pay: 1, is_try: 1 });
  20. const lessonIds = lsList.map(i => i.lesson_id);
  21. let data = await this.lessonModel.find({ _id: lessonIds, ...otherQuery }).skip(parseInt(skip)).limit(parseInt(limit));
  22. if (data.length > 0) data = JSON.parse(JSON.stringify(data));
  23. data = data.map(i => {
  24. const { _id } = i;
  25. const r = lsList.find(f => ObjectId(f.lesson_id).equals(_id));
  26. if (r) i = { ...i, ..._.pick(r, [ 'is_try', 'is_pay' ]) };
  27. return i;
  28. });
  29. const total = await this.lessonModel.count({ _id: lessonIds, ...otherQuery });
  30. return { data, total };
  31. }
  32. }
  33. module.exports = StudentService;