class.js 5.9 KB

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