1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- '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;
- this.smodel = this.ctx.model.Student;
- }
- 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();
- }
- // 设置学生为组长或组员
- async sethead(data) {
- const { groupid, stuid, type } = data;
- const group = await this.model.findById(groupid);
- const students = group.students;
- for (const student of students) {
- if (student.stuid === stuid) {
- student.type = type;
- }
- }
- await group.save();
- const student = await this.smodel.findById(stuid);
- if (type === '1') {
- student.jod = '组长';
- }
- if (type === '0') {
- student.jod = '普通学生';
- }
- await student.save();
- }
- async findbystuid(data) {
- const { stuid } = data;
- const _student = await this.smodel.findById(stuid);
- const groups = await this.model.find({ classid: _student.classid });
- let result;
- for (const group of groups) {
- const students = group.students;
- for (const student of students) {
- if (student.stuid === stuid) {
- result = group;
- }
- }
- }
- if (!result) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该学生尚未进组');
- }
- return result;
- }
- }
- module.exports = GroupService;
|