123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- '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;
- }
- async query({ skip, limit, ...info }) {
- const total = await this.model.count(info);
- const res = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
- const data = [];
- for (const _group of res) {
- const stus = [];
- for (const stu of _group.students) {
- const student = await this.smodel.findById(stu.stuid);
- let job = '';
- if (student) {
- job = student.job;
- }
- stus.push({ ...JSON.parse(JSON.stringify(stu)), job });
- }
- const newdata = { ...JSON.parse(JSON.stringify(_group)) };
- newdata.students = stus;
- data.push(newdata);
- }
- return { total, data };
- }
- async returns({ classid }) {
- const res = await this.model.updateMany({ classid }, { status: '0' });
- return res;
- }
- }
- module.exports = GroupService;
|