class.js 10 KB

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