class.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 { planid, termid } = data;
  19. assert(planid, '计划id为必填项');
  20. assert(termid, '期id为必填项');
  21. // 根据计划id与期id查询所有批次下的班级
  22. const newclass = await this.model.find({ planid, termid });
  23. if (!newclass) {
  24. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  25. }
  26. // 根据计划和期查询所有上报的学生 并按照学校排序
  27. const newstudent = await this.stumodel.find({ termid }).sort({ schid: 1 });
  28. if (!newstudent) {
  29. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  30. }
  31. let _students = _.map(newstudent, 'id');
  32. for (const _class of newclass) {
  33. // 取得班级人数
  34. const stunum = _class.number;
  35. // 取出班级人数下的学生id
  36. const beforestu = _.take(_students, stunum);
  37. // 将取出的数据调用更新学生的班级信息方法
  38. await this.studentup(_class.id, beforestu);
  39. // 取出的学生数据从原数据中删除
  40. _students = _.difference(_students, beforestu);
  41. }
  42. }
  43. // 根据传入的学生列表和班级id更新学生信息
  44. async studentup(classid, beforestu) {
  45. // 循环学生id
  46. for (const stuid of beforestu) {
  47. const student = await this.stumodel.findById(stuid);
  48. if (student) {
  49. student.classid = classid;
  50. await student.save();
  51. }
  52. }
  53. }
  54. async notice(data) {
  55. for (const classid of data.classids) {
  56. // 根据班级id找到需要通知的班级
  57. const _class = await this.model.findById(classid);
  58. const { headteacherid } = _class;
  59. // 根据班级id找到对应的课程表
  60. const lesson = await this.lessmodel.findOne({ classid });
  61. if (lesson) {
  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. if (_teacher) {
  71. const teaopenid = _teacher.openid;
  72. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  73. }
  74. }
  75. // 给班主任发送通知
  76. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  77. if (_headteacher) {
  78. const headteaopenid = _headteacher.openid;
  79. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  80. }
  81. // 根据班级的期id查询对应的培训计划
  82. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  83. const term = await trainplan.termnum.id(_class.termid);
  84. const batch = await term.batchnum.id(_class.batchid);
  85. const startdate = batch.startdate;
  86. const classname = _class.name;
  87. // 给班级所有学生发送邮件通知
  88. const students = await this.stumodel.find({ classid });
  89. for (const student of students) {
  90. const { email, name } = student;
  91. const subject = '吉林省高等学校毕业生就业指导中心通知';
  92. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  93. this.ctx.service.util.sendMail(email, subject, text);
  94. }
  95. }
  96. }
  97. }
  98. async uptea(data) {
  99. for (const _data of data) {
  100. const classInfo = await this.model.findById(_data.id);
  101. classInfo.headteacherid = _data.headteacherid;
  102. await classInfo.save();
  103. }
  104. }
  105. }
  106. module.exports = ClassService;