class.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. const lessons = lesson.lessons;
  63. const remark = '感谢您的使用';
  64. const date = await this.ctx.service.util.updatedate();
  65. const detail = '班级各项信息已确认,请注意查收';
  66. // 遍历班级授课教师发送通知
  67. for (const lessoninfo of lessons) {
  68. const teaid = lessoninfo.teaid;
  69. const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
  70. const teaopenid = _teacher.openid;
  71. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  72. }
  73. // 给班主任发送通知
  74. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  75. const headteaopenid = _headteacher.openid;
  76. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  77. // 根据班级的期id查询对应的培训计划
  78. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  79. const term = await trainplan.termnum.id(_class.termid);
  80. const batch = await term.batchnum.id(_class.batchid);
  81. const startdate = batch.startdate;
  82. const classname = _class.name;
  83. // 给班级所有学生发送邮件通知
  84. const students = await this.stumodel.find({ classid });
  85. for (const student of students) {
  86. const { email, name } = student;
  87. const subject = '吉林省高等学校毕业生就业指导中心通知';
  88. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  89. this.ctx.service.util.sendMail(email, subject, text);
  90. }
  91. }
  92. }
  93. }
  94. module.exports = ClassService;