'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class GroupService extends CrudService { constructor(ctx) { super(ctx, 'group'); this.model = this.ctx.model.Group; } async insert(data) { const { groupid, stuid, stuname } = data; const group = await this.model.findById(groupid); const stuids = []; for (const student of group.students) { stuids.push(student.stuid); } if (stuids.includes(stuid)) { throw new BusinessError(ErrorCode.DATA_EXIST, '您已加入该组,请勿重复操作'); } else { group.students.push({ stuid, stuname }); await group.save(); } } async exit(data) { const { groupid, stuid } = data; const group = await this.model.findById(groupid); const students = group.students; for (const student of students) { if (student.stuid === stuid) { students.remove(student); } } await group.save(); } } module.exports = GroupService;