student.js 5.7 KB

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