group.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(
  22. ErrorCode.DATA_EXIST,
  23. '您已加入该组,请勿重复操作'
  24. );
  25. } else {
  26. group.students.push({ stuid, stuname });
  27. await group.save();
  28. }
  29. }
  30. async exit(data) {
  31. const { groupid, stuid } = data;
  32. const group = await this.model.findById(groupid);
  33. const students = group.students;
  34. for (const student of students) {
  35. if (student.stuid === stuid) {
  36. students.remove(student);
  37. }
  38. }
  39. await group.save();
  40. }
  41. // 设置学生为组长或组员
  42. async sethead(data) {
  43. const { groupid, stuid, type } = data;
  44. const group = await this.model.findById(groupid);
  45. const students = group.students;
  46. for (const student of students) {
  47. if (student.stuid === stuid) {
  48. student.type = type;
  49. }
  50. }
  51. await group.save();
  52. const student = await this.smodel.findById(stuid);
  53. if (type === '1') {
  54. student.jod = '组长';
  55. }
  56. if (type === '0') {
  57. student.jod = '普通学生';
  58. }
  59. await student.save();
  60. }
  61. async findbystuid(data) {
  62. const { stuid } = data;
  63. const _student = await this.smodel.findById(stuid);
  64. const groups = await this.model.find({ classid: _student.classid });
  65. let result;
  66. for (const group of groups) {
  67. const students = group.students;
  68. for (const student of students) {
  69. if (student.stuid === stuid) {
  70. result = group;
  71. }
  72. }
  73. }
  74. if (!result) {
  75. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该学生尚未进组');
  76. }
  77. return result;
  78. }
  79. async query({ skip, limit, ...info }) {
  80. const total = await this.model.count(info);
  81. const res = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  82. const data = [];
  83. for (const _group of res) {
  84. const stus = [];
  85. for (const stu of _group.students) {
  86. const student = await this.smodel.findById(stu.stuid);
  87. let job = '';
  88. if (student) {
  89. job = student.job;
  90. }
  91. stus.push({ ...JSON.parse(JSON.stringify(stu)), job });
  92. }
  93. const newdata = { ...JSON.parse(JSON.stringify(_group)) };
  94. newdata.students = stus;
  95. data.push(newdata);
  96. }
  97. return { total, data };
  98. }
  99. }
  100. module.exports = GroupService;