class.js 11 KB

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