student.js 4.1 KB

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