class.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. await this.groupcreate(termid, _class.batchid, _class.id);
  41. // 取出的学生数据从原数据中删除
  42. _students = _.difference(_students, beforestu);
  43. }
  44. }
  45. // 根据传入的学生列表和班级id更新学生信息
  46. async studentup(classid, beforestu) {
  47. // 循环学生id
  48. for (const stuid of beforestu) {
  49. const student = await this.stumodel.findById(stuid);
  50. if (student) {
  51. student.classid = classid;
  52. await student.save();
  53. }
  54. }
  55. }
  56. // 自动分组
  57. async groupcreate(termid, batchid, classid) {
  58. const group = await this.gmodel.find({ termid, batchid, classid });
  59. if (group.length === 0) {
  60. for (let i = 1; i < 8; i++) {
  61. const name = i + '组';
  62. const newdata = { name, termid, batchid, classid };
  63. await this.gmodel.create(newdata);
  64. }
  65. }
  66. }
  67. // 根据传入的学生列表和班级id更新学生信息
  68. async studentupclass({ id }, data) {
  69. assert(id, '班级id为必填项');
  70. // 循环学生id
  71. for (const stuid of data) {
  72. const student = await this.stumodel.findById(stuid);
  73. if (student) {
  74. student.classid = id;
  75. await student.save();
  76. }
  77. }
  78. }
  79. async notice(data) {
  80. for (const classid of data.classids) {
  81. // 根据班级id找到需要通知的班级
  82. const _class = await this.model.findById(classid);
  83. const { headteacherid } = _class;
  84. // 根据班级id找到对应的课程表
  85. const lesson = await this.lessmodel.findOne({ classid });
  86. if (lesson) {
  87. const lessons = lesson.lessons;
  88. const remark = '感谢您的使用';
  89. const date = await this.ctx.service.util.updatedate();
  90. const detail = '班级各项信息已确认,请注意查收';
  91. // 遍历班级授课教师发送通知
  92. for (const lessoninfo of lessons) {
  93. const teaid = lessoninfo.teaid;
  94. const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
  95. if (_teacher) {
  96. const teaopenid = _teacher.openid;
  97. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  98. }
  99. }
  100. // 给班主任发送通知
  101. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  102. if (_headteacher) {
  103. const headteaopenid = _headteacher.openid;
  104. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  105. }
  106. // 根据班级的期id查询对应的培训计划
  107. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  108. const term = await trainplan.termnum.id(_class.termid);
  109. const batch = await term.batchnum.id(_class.batchid);
  110. const startdate = batch.startdate;
  111. const classname = _class.name;
  112. // 给班级所有学生发送邮件通知
  113. const students = await this.stumodel.find({ classid });
  114. for (const student of students) {
  115. const { email, name } = student;
  116. const subject = '吉林省高等学校毕业生就业指导中心通知';
  117. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  118. this.ctx.service.util.sendMail(email, subject, text);
  119. }
  120. }
  121. }
  122. }
  123. async uptea(data) {
  124. for (const _data of data) {
  125. const classInfo = await this.model.findById(_data.id);
  126. classInfo.headteacherid = _data.headteacherid;
  127. await classInfo.save();
  128. }
  129. }
  130. }
  131. module.exports = ClassService;