'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); const { ObjectId } = require('mongoose').Types; // 学生视图接口 class StudentService extends CrudService { constructor(ctx) { super(ctx, 'lessonprivate'); this.lessonModel = this.ctx.model.Business.Lesson; this.lessonStudentModel = this.ctx.model.Business.LessonStudent; } async lessonList({ student_id, school_id, skip = 0, limit = 0, ...otherQuery }) { // 查出该学生的课表 const query = {}; if (student_id)query.student_id = student_id; if (school_id) query.school_id = school_id; const lsList = await this.lessonStudentModel.find(query, { lesson_id: 1, is_pay: 1, is_try: 1, is_sign: 1 }); const lessonIds = lsList.map(i => i.lesson_id); let data = await this.lessonModel.find({ _id: lessonIds, ...otherQuery }).skip(parseInt(skip)).limit(parseInt(limit)); if (data.length > 0) data = JSON.parse(JSON.stringify(data)); data = data.map(i => { const { _id } = i; const r = lsList.find(f => ObjectId(f.lesson_id).equals(_id)); if (r) i = { ...i, ..._.pick(r, [ 'is_try', 'is_pay', 'is_sign' ]) }; return i; }); const total = await this.lessonModel.count({ _id: lessonIds, ...otherQuery }); return { data, total }; } } module.exports = StudentService;