student.js 4.3 KB

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