student.js 5.2 KB

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