class.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ClassService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'class');
  10. this.model = this.ctx.model.Class;
  11. this.stumodel = this.ctx.model.Student;
  12. this.lessmodel = this.ctx.model.Lesson;
  13. this.umodel = this.ctx.model.User;
  14. this.tmodel = this.ctx.model.Trainplan;
  15. }
  16. async divide(data) {
  17. const name = await data.classname;
  18. const number = await data.students.length;
  19. const type = await data.type;
  20. const termid = await data.termid;
  21. const batchid = await data.batchid;
  22. const classdata = { name, batchid, termid, number, type };
  23. // 根据班级名称查询班级是否已存在
  24. const newclass = await this.model.findOne({ name });
  25. // 如果已经存在
  26. if (newclass) {
  27. throw new BusinessError(ErrorCode.DATA_EXIST, '班级名称已存在');
  28. // const classid = newclass._id;
  29. // const oldstudents = this.stumodel.find({ classid });
  30. // for (const oldstudent of oldstudents) {
  31. // oldstudent.classid = null;
  32. // }
  33. // // 遍历学生id
  34. // for (const studentid of data.students) {
  35. // // 根据学生id找到学生数据修改其中的class字段
  36. // const student = await this.stumodel.findById(studentid);
  37. // student.batchid = batchid;
  38. // student.classid = classid;
  39. // await student.save();
  40. // }
  41. } else {
  42. // 如果班级不存在则创建班级
  43. const newdata = await this.model.create(classdata);
  44. // 查询班级id
  45. const classid = newdata._id;
  46. // 遍历学生id
  47. for (const studentid of data.students) {
  48. // 根据学生id找到学生数据修改其中的class字段
  49. const student = await this.stumodel.findById(studentid);
  50. student.classid = classid;
  51. student.batchid = batchid;
  52. student.termid = termid;
  53. await student.save();
  54. }
  55. }
  56. }
  57. async notice(data) {
  58. for (const classid of data.classids) {
  59. const _class = await this.model.findById(classid);
  60. const { headteacherid } = _class;
  61. const lesson = await this.lessmodel.findOne({ classid });
  62. if (lesson) {
  63. const lessons = lesson.lessons;
  64. const remark = '感谢您的使用';
  65. const date = await this.ctx.service.util.updatedate();
  66. const detail = '班级各项信息已确认,请注意查收';
  67. // 遍历班级授课教师发送通知
  68. for (const lessoninfo of lessons) {
  69. const teaid = lessoninfo.teaid;
  70. const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
  71. if (_teacher) {
  72. const teaopenid = _teacher.openid;
  73. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  74. }
  75. }
  76. // 给班主任发送通知
  77. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  78. if (_headteacher) {
  79. const headteaopenid = _headteacher.openid;
  80. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  81. }
  82. // 根据班级的期id查询对应的培训计划
  83. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  84. const term = await trainplan.termnum.id(_class.termid);
  85. const batch = await term.batchnum.id(_class.batchid);
  86. const startdate = batch.startdate;
  87. const classname = _class.name;
  88. // 给班级所有学生发送邮件通知
  89. const students = await this.stumodel.find({ classid });
  90. for (const student of students) {
  91. const { email, name } = student;
  92. const subject = '吉林省高等学校毕业生就业指导中心通知';
  93. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  94. this.ctx.service.util.sendMail(email, subject, text);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. module.exports = ClassService;