student.js 5.7 KB

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