group.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class GroupService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'group');
  10. this.model = this.ctx.model.Group;
  11. this.smodel = this.ctx.model.Student;
  12. }
  13. async insert(data) {
  14. const { groupid, stuid, stuname } = data;
  15. const group = await this.model.findById(groupid);
  16. const stuids = [];
  17. for (const student of group.students) {
  18. stuids.push(student.stuid);
  19. }
  20. if (stuids.includes(stuid)) {
  21. throw new BusinessError(ErrorCode.DATA_EXIST, '您已加入该组,请勿重复操作');
  22. } else {
  23. group.students.push({ stuid, stuname });
  24. await group.save();
  25. }
  26. }
  27. async exit(data) {
  28. const { groupid, stuid } = data;
  29. const group = await this.model.findById(groupid);
  30. const students = group.students;
  31. for (const student of students) {
  32. if (student.stuid === stuid) {
  33. students.remove(student);
  34. }
  35. }
  36. await group.save();
  37. }
  38. // 设置学生为组长或组员
  39. async sethead(data) {
  40. const { groupid, stuid, type } = data;
  41. const group = await this.model.findById(groupid);
  42. const students = group.students;
  43. for (const student of students) {
  44. if (student.stuid === stuid) {
  45. student.type = type;
  46. }
  47. }
  48. await group.save();
  49. const student = await this.smodel.findById(stuid);
  50. if (type === '1') {
  51. student.jod = '组长';
  52. }
  53. if (type === '0') {
  54. student.jod = '普通学生';
  55. }
  56. await student.save();
  57. }
  58. }
  59. module.exports = GroupService;