class.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. this.heamodel = this.ctx.model.Headteacher;
  17. this.teamodel = this.ctx.model.Teacher;
  18. }
  19. async divide(data) {
  20. const { planid, termid } = data;
  21. assert(planid, '计划id为必填项');
  22. assert(termid, '期id为必填项');
  23. // 根据计划id与期id查询所有批次下的班级
  24. const newclass = await this.model.find({ planid, termid });
  25. if (!newclass) {
  26. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  27. }
  28. // 根据计划和期查询所有上报的学生 并按照学校排序
  29. const newstudent = await this.stumodel.find({ termid }).sort({ schid: 1 });
  30. if (!newstudent) {
  31. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  32. }
  33. let _students = _.map(newstudent, 'id');
  34. for (const _class of newclass) {
  35. // 取得班级人数
  36. const stunum = _class.number;
  37. // 取出班级人数下的学生id
  38. const beforestu = _.take(_students, stunum);
  39. // 将取出的数据调用更新学生的班级信息方法
  40. await this.studentup(_class.id, _class.batchid, beforestu);
  41. // 将班级自动分为组
  42. await this.groupcreate(termid, _class.batchid, _class.id);
  43. // 取出的学生数据从原数据中删除
  44. _students = _.difference(_students, beforestu);
  45. }
  46. }
  47. // 根据传入的学生列表和班级id更新学生信息
  48. async studentup(classid, batchid, beforestu) {
  49. // 循环学生id
  50. for (const stuid of beforestu) {
  51. const student = await this.stumodel.findById(stuid);
  52. if (student) {
  53. student.classid = classid;
  54. student.batchid = batchid;
  55. await student.save();
  56. }
  57. }
  58. }
  59. // 自动分组
  60. async groupcreate(termid, batchid, classid) {
  61. const group = await this.gmodel.find({ termid, batchid, classid });
  62. if (group.length === 0) {
  63. for (let i = 1; i < 8; i++) {
  64. const name = i + '组';
  65. const newdata = { name, termid, batchid, classid };
  66. await this.gmodel.create(newdata);
  67. }
  68. }
  69. }
  70. // 根据传入的学生列表和班级id更新学生信息
  71. async studentupclass({ id }, data) {
  72. assert(id, '班级id为必填项');
  73. // 循环学生id
  74. for (const stuid of data) {
  75. const student = await this.stumodel.findById(stuid);
  76. if (student) {
  77. student.classid = id;
  78. await student.save();
  79. }
  80. }
  81. }
  82. async notice(data) {
  83. for (const classid of data.classids) {
  84. // 根据班级id找到需要通知的班级
  85. const _class = await this.model.findById(classid);
  86. const { headteacherid } = _class;
  87. // 根据班级id找到对应的课程表
  88. const lesson = await this.lessmodel.findOne({ classid });
  89. if (lesson) {
  90. const lessons = lesson.lessons;
  91. const remark = '感谢您的使用';
  92. const date = await this.ctx.service.util.updatedate();
  93. const detail = '班级各项信息已确认,请注意查收';
  94. // 遍历班级授课教师发送通知
  95. for (const lessoninfo of lessons) {
  96. const teaid = lessoninfo.teaid;
  97. const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
  98. if (_teacher) {
  99. const teaopenid = _teacher.openid;
  100. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  101. }
  102. }
  103. // 给班主任发送通知
  104. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  105. if (_headteacher) {
  106. const headteaopenid = _headteacher.openid;
  107. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  108. }
  109. // 根据班级的期id查询对应的培训计划
  110. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  111. const term = await trainplan.termnum.id(_class.termid);
  112. const batch = await term.batchnum.id(_class.batchid);
  113. const startdate = batch.startdate;
  114. const classname = _class.name;
  115. // 给班级所有学生发送邮件通知
  116. const students = await this.stumodel.find({ classid });
  117. for (const student of students) {
  118. const { email, name } = student;
  119. const subject = '吉林省高等学校毕业生就业指导中心通知';
  120. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  121. this.ctx.service.util.sendMail(email, subject, text);
  122. }
  123. }
  124. }
  125. }
  126. async uptea(data) {
  127. for (const _data of data) {
  128. const classInfo = await this.model.findById(_data.id);
  129. classInfo.headteacherid = _data.headteacherid;
  130. await classInfo.save();
  131. }
  132. }
  133. async query({ skip, limit, ...info }) {
  134. const classes = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  135. const data = [];
  136. for (const _class of classes) {
  137. const classInfo = await this.fetch({ id: _class.id });
  138. data.push(classInfo);
  139. }
  140. return data;
  141. }
  142. async fetch({ id }) {
  143. const classInfo = _.cloneDeep(JSON.parse(JSON.stringify(await this.model.findById(id))));
  144. const trainplan = await this.tmodel.findById(classInfo.planid);
  145. if (trainplan) {
  146. const term = _.filter(trainplan.termnum, item => item.id === classInfo.termid);
  147. if (term.length > 0) {
  148. classInfo.term = term[0].term;
  149. const batch = _.filter(term[0].batchnum, item => item.id === classInfo.batchid);
  150. if (batch.length > 0) {
  151. classInfo.batch = batch[0].batch;
  152. classInfo.startdate = batch[0].startdate;
  153. classInfo.enddate = batch[0].enddate;
  154. }
  155. }
  156. }
  157. return classInfo;
  158. }
  159. async upclasses(data) {
  160. for (const _data of data) {
  161. await this.model.findByIdAndUpdate(_data.id, _data);
  162. }
  163. }
  164. async classinfo({ id: classid }) {
  165. const _classes = await this.model.findById(classid);
  166. // 班级信息
  167. const classes = _.cloneDeep(JSON.parse(JSON.stringify(_classes)));
  168. // 学生信息
  169. const students = await this.stumodel.find({ classid });
  170. if (students) {
  171. classes.students = students;
  172. }
  173. // 班主任信息
  174. let headteacher;
  175. if (classes.headteacherid) {
  176. headteacher = await this.heamodel.findById(classes.headteacherid);
  177. }
  178. // 礼仪课老师信息
  179. let lyteacher;
  180. if (classes.lyteacherid) {
  181. lyteacher = await this.heamodel.findById(classes.lyteacherid);
  182. if (!lyteacher) {
  183. lyteacher = await this.teamodel.findById(classes.lyteacherid);
  184. }
  185. }
  186. // 教课老师信息
  187. let teachers = [];
  188. const lessones = await this.lessmodel.findOne({ classid });
  189. if (lessones) {
  190. for (const lesson of lessones.lessons) {
  191. if (lesson.teaid) {
  192. const teacher = await this.teamodel.findById(lesson.teaid);
  193. teachers.push(teacher);
  194. }
  195. }
  196. }
  197. teachers.push(lyteacher);
  198. teachers.push(headteacher);
  199. teachers = _.uniq(_.compact(teachers));
  200. classes.teachers = teachers;
  201. return classes;
  202. }
  203. }
  204. module.exports = ClassService;