student.js 6.0 KB

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