class.js 13 KB

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