student.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 StudentService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'student');
  10. this.model = this.ctx.model.Student;
  11. this.umodel = this.ctx.model.User;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. this.clamodel = this.ctx.model.Class;
  14. this.upmodel = this.ctx.model.Uploadtask;
  15. }
  16. // 查询
  17. async query({ skip, limit, ...info }) {
  18. const total = await this.model.count(info);
  19. const res = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  20. const data = [];
  21. for (const elm of res) {
  22. const plan = await this.tmodel.findOne({ 'termnum._id': ObjectId(elm.termid) });
  23. const newdata = { ...JSON.parse(JSON.stringify(elm)) };
  24. if (plan) {
  25. const term = await plan.termnum.id(elm.termid);
  26. newdata.termname = term.term;
  27. if (elm.batchid) {
  28. const _batch = await term.batchnum.id(elm.batchid);
  29. newdata.batchname = _batch.batch;
  30. }
  31. }
  32. if (elm.classid) {
  33. const classs = await this.clamodel.findById(elm.classid);
  34. if (classs) {
  35. newdata.classname = classs.name;
  36. }
  37. }
  38. data.push(newdata);
  39. }
  40. const result = { total, data };
  41. return result;
  42. }
  43. // 查询
  44. async seek({ termid, skip, limit }) {
  45. const total = await this.model.count({ termid, $or: [{ classid: null }, { classid: '' }] });
  46. const data = await this.model.find({ termid, $or: [{ classid: null }, { classid: '' }] }).skip(Number(skip)).limit(Number(limit));
  47. const result = { total, data };
  48. return result;
  49. }
  50. async findbedroom(data) {
  51. const { batchid, classid } = data;
  52. const result = [];
  53. // 如果传的是批次id
  54. if (batchid) {
  55. // 查询该批次下的所有学生
  56. const students = await this.model.find({ batchid });
  57. const bedroomList = new Set();
  58. // 查询该批次的所有寝室号
  59. for (const student of students) {
  60. bedroomList.add(student.bedroom);
  61. }
  62. let studentList = [];
  63. // 查询该批次所有寝室下的学生名单
  64. for (const bedroom of bedroomList) {
  65. const newstudents = await this.model.find({ bedroom });
  66. for (const newstudent of newstudents) {
  67. studentList.push(newstudent.name);
  68. }
  69. result.push({ bedroom, studentList });
  70. studentList = [];
  71. }
  72. }
  73. // 如果传的是班级id
  74. if (classid) {
  75. // 查询该班级所有学生
  76. const students = await this.model.find({ classid });
  77. const bedroomList = new Set();
  78. // 查询该班级所有寝室号
  79. for (const student of students) {
  80. bedroomList.add(student.bedroom);
  81. }
  82. let studentList = [];
  83. // 查询该班级所有寝室的学生名单
  84. for (const bedroom of bedroomList) {
  85. const newstudents = await this.model.find({ bedroom });
  86. for (const newstudent of newstudents) {
  87. // 如果寝室中有非本班级学生(混寝),则过滤掉不予显示
  88. if (newstudent.classid === classid) {
  89. studentList.push(newstudent.name);
  90. }
  91. }
  92. result.push({ bedroom, studentList });
  93. studentList = [];
  94. }
  95. }
  96. return result;
  97. }
  98. async upjob(data) {
  99. const { stuid, job } = data;
  100. const student = await this.model.findById(stuid);
  101. student.job = job;
  102. if (job === '班长' || job === '学委') {
  103. const user = await this.umodel.findOne({ uid: stuid, type: '4' });
  104. const date = await this.ctx.service.util.updatedate();
  105. const openid = user.openid;
  106. const detail = '你已被班主任设置为' + job + ',请及时登录查看';
  107. const remark = '感谢您的使用';
  108. if (openid) {
  109. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  110. }
  111. }
  112. return await student.save();
  113. }
  114. // 根据学生id删除班级
  115. async deleteclass(data) {
  116. for (const el of data) {
  117. const student = await this.model.findById(el);
  118. if (student) {
  119. student.classid = '';
  120. await student.save();
  121. }
  122. }
  123. }
  124. // 根据班级id查出班级各个学生的分数
  125. async findscore({ skip, limit, ...info }) {
  126. const total = await this.model.count(info);
  127. const students = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  128. const data = [];
  129. for (const student of students) {
  130. const _student = _.cloneDeep(student);
  131. const group = await this.ctx.service.group.findbystuid({ stuid: _student.id });
  132. if (group && group.score) {
  133. _student.groupscore = group.score;
  134. }
  135. const tasks = await this.upmodel.find({ studentid: _student.id });
  136. _student.tasks = tasks;
  137. data.push(_student);
  138. }
  139. return { total, data };
  140. }
  141. }
  142. module.exports = StudentService;