class.js 9.4 KB

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