class.js 4.6 KB

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