lesson.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const moment = require('moment');
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class LessonService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'lesson');
  11. this.model = this.ctx.model.Lesson;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. this.clamodel = this.ctx.model.Class;
  14. this.lmodel = this.ctx.model.Lessonmode;
  15. this.teamodel = this.ctx.model.Teacher;
  16. this.stumodel = this.ctx.model.Student;
  17. this.schmodel = this.ctx.model.School;
  18. this.headteamodel = this.ctx.model.Headteacher;
  19. this.umodel = this.ctx.model.User;
  20. }
  21. // 自动排课私有方法
  22. async autolesson({ id }) {
  23. // 首先将课程表清空
  24. const res = await this.tmodel.findById(id);
  25. if (!res) {
  26. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  27. }
  28. const terms = res.termnum;
  29. const _lessonmode = await this.lmodel.find();
  30. // 循环取得所有期
  31. for (const elm of terms) {
  32. // 根据期id清空课程表
  33. await this.model.deleteMany({ termid: elm.id });
  34. // 清空成功后,循环取得当前期下所有批次信息
  35. const batchs = elm.batchnum;
  36. for (const batch of batchs) {
  37. // 取得当前批次开始结束日期,并根据日期取得所有天数
  38. const sedays = await this.getAllDays(batch.startdate, batch.enddate);
  39. // 根据批次取得当前批次下所有班级
  40. const _classs = await this.clamodel.find({
  41. planid: id,
  42. termid: elm.id,
  43. batchid: batch.id,
  44. });
  45. // 循环班级
  46. const teachids = [];
  47. for (const cla of _classs) {
  48. // 取得课程模板信息
  49. let lessonmode_ = _.find(_lessonmode, { type: cla.type });
  50. if (!lessonmode_) {
  51. lessonmode_ = _lessonmode[0];
  52. if (!lessonmode_) {
  53. throw new BusinessError(
  54. ErrorCode.DATA_NOT_EXIST,
  55. '课程模板信息不存在'
  56. );
  57. }
  58. }
  59. // 取得模板内容并转化成json
  60. const lessons_ = JSON.parse(lessonmode_.lessons);
  61. // 记录天数
  62. let i = 1;
  63. // 循环天数
  64. const newlesson = [];
  65. for (const day of sedays) {
  66. // 循环课程模板,将模板信息排入班级课程表中
  67. for (const lessm of lessons_) {
  68. // 循环插入模板信息
  69. if (lessm['day' + i] !== '--') {
  70. let _subid = '';
  71. if (lessm['day' + i + 'type'] === '课程') {
  72. _subid = lessm['day' + i + 'subid'];
  73. } else {
  74. _subid = '';
  75. }
  76. let allday = 0;
  77. if (i === 6) {
  78. // 判断是否有外市的学生有的时候 将其设置为半天
  79. const ishalfday = await this.ishalfday(cla.id);
  80. if (ishalfday) {
  81. allday = 1;
  82. }
  83. }
  84. const data = {
  85. subid: _subid,
  86. subname: lessm['day' + i],
  87. date: day,
  88. time: lessm.time,
  89. day: allday,
  90. };
  91. // 将教师按照分数的综合成绩排序,上报时间,安排教师.
  92. const teacher_ = await this.autoteacher(_subid, teachids);
  93. if (teacher_) {
  94. data.teaid = teacher_.id;
  95. data.teaname = teacher_.name;
  96. teachids.push(teacher_.id);
  97. }
  98. newlesson.push(data);
  99. }
  100. }
  101. i = i + 1;
  102. }
  103. const newdata = {
  104. termid: elm.id,
  105. batchid: batch.id,
  106. classid: cla.id,
  107. lessons: newlesson,
  108. };
  109. // 将课程信息填入课程表
  110. await this.model.create(newdata);
  111. }
  112. }
  113. }
  114. }
  115. // 自动排教师,按照分数的综合成绩排序,上报时间,安排教师
  116. async autoteacher(subid, teachids) {
  117. // 按照上报时间取得所有老师,进行正序排列
  118. const teachers = await this.teamodel
  119. .find({ subid, status: '4' })
  120. .sort({ zlscore: '-1', msscore: '-1', xsscore: '-1' });
  121. for (const teaid of teachids) {
  122. _.remove(teachers, item => item.id === teaid);
  123. }
  124. let teacher = {};
  125. if (teachers.length > 0) {
  126. teacher = teachers[0];
  127. }
  128. return teacher;
  129. }
  130. // 判断是否为半天
  131. async ishalfday(classid) {
  132. // 通过班级id取得所有学生
  133. const students = await this.stumodel.find({ classid });
  134. let res = false;
  135. for (const stu of students) {
  136. const sch = await this.schmodel.findOne({ code: stu.schid });
  137. if (sch && sch.hascar === '0') {
  138. res = true;
  139. break;
  140. }
  141. }
  142. return res;
  143. }
  144. // 取得日期间所有日期
  145. async getAllDays(begin_date, end_date) {
  146. const errArr = [],
  147. resultArr = [],
  148. dateReg = /^[2]\d{3}-[01]\d-[0123]\d$/;
  149. if (
  150. typeof begin_date !== 'string' ||
  151. begin_date === '' ||
  152. !dateReg.test(begin_date)
  153. ) {
  154. return errArr;
  155. }
  156. if (
  157. typeof end_date !== 'string' ||
  158. end_date === '' ||
  159. !dateReg.test(end_date)
  160. ) {
  161. return errArr;
  162. }
  163. try {
  164. const beginTimestamp = Date.parse(new Date(begin_date)),
  165. endTimestamp = Date.parse(new Date(end_date));
  166. // 开始日期小于结束日期
  167. if (beginTimestamp > endTimestamp) {
  168. return errArr;
  169. }
  170. // 开始日期等于结束日期
  171. if (beginTimestamp === endTimestamp) {
  172. resultArr.push(begin_date);
  173. return resultArr;
  174. }
  175. let tempTimestamp = beginTimestamp,
  176. tempDate = begin_date;
  177. // 新增日期是否和结束日期相等, 相等跳出循环
  178. while (tempTimestamp !== endTimestamp) {
  179. resultArr.push(tempDate);
  180. // 增加一天
  181. tempDate = moment(tempTimestamp).add(1, 'd').format('YYYY-MM-DD');
  182. // 将增加时间变为时间戳
  183. tempTimestamp = Date.parse(new Date(tempDate));
  184. }
  185. // 将最后一天放入数组
  186. resultArr.push(end_date);
  187. return resultArr;
  188. } catch (err) {
  189. return errArr;
  190. }
  191. }
  192. // 根据计划id、教师id查询所有班级信息
  193. async classbyteaid({ planid, teaid }) {
  194. // 取得传入的计划id与教师id
  195. // 根据计划id取得所有期
  196. const plan = await this.tmodel.findById(planid);
  197. if (!plan) {
  198. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  199. }
  200. const terms = await plan.termnum;
  201. // 循环取得所有期信息
  202. const data = [];
  203. for (const term of terms) {
  204. // 根据期id与教师id查出课程班级信息
  205. const lessons = await this.model.find({
  206. termid: term.id,
  207. 'lessons.teaid': teaid,
  208. });
  209. const batchs = await term.batchnum;
  210. for (const elm of lessons) {
  211. const newdata = {};
  212. const batch = await batchs.id(elm.batchid);
  213. newdata.planid = planid;
  214. newdata.title = plan.title;
  215. newdata.termid = elm.termid;
  216. newdata.term = term.term;
  217. newdata.batchid = elm.batchid;
  218. newdata.batch = batch.batch;
  219. newdata.classid = elm.classid;
  220. if (elm.classid) {
  221. const cla = await this.clamodel.findById(elm.classid);
  222. if (cla) {
  223. newdata.classname = cla.name;
  224. }
  225. }
  226. const _lessons = elm.lessons.filter(item => item.teaid === teaid);
  227. newdata.lessons = _lessons;
  228. data.push(newdata);
  229. }
  230. }
  231. return data;
  232. }
  233. // 根据计划id、教师id查询所有班级信息
  234. async teaclass({ planid, teaid }) {
  235. // 取得传入的计划id与教师id
  236. // 根据计划id取得所有期
  237. const plan = await this.tmodel.findById(planid);
  238. if (!plan) {
  239. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  240. }
  241. const terms = await plan.termnum;
  242. // 循环取得所有期信息
  243. const data = [];
  244. for (const term of terms) {
  245. // 根据期id与教师id查出课程班级信息
  246. const lessons = await this.model.find({
  247. termid: term.id,
  248. 'lessons.teaid': teaid,
  249. });
  250. for (const elm of lessons) {
  251. if (elm.classid) {
  252. const cla = await this.ctx.service.class.fetch({ id: elm.classid });
  253. data.push(cla);
  254. }
  255. }
  256. }
  257. return data;
  258. }
  259. async uplessones(data) {
  260. for (const _data of data) {
  261. await this.model.findByIdAndUpdate(_data.id, _data);
  262. }
  263. }
  264. /**
  265. * 修改课表的状态,并发送信息
  266. * @param {Array} ids 要修改的课表
  267. */
  268. async check({ ids }) {
  269. // 1,修改课表状态; TODO 2,拿到所有的班级,获取所有人员;3,然后发送信息
  270. const list = await this.model.find({ _id: { $in: ids } });
  271. const res = await this.model.updateMany({ _id: { $in: ids } }, { status: '1' });
  272. // 循环课表
  273. for (const lessonInfo of list) {
  274. // 获取期数
  275. const { termid, classid, lessons } = lessonInfo;
  276. const planRes = await this.tmodel.findOne({ termnum: { $elemMatch: { _id: termid } } });
  277. if (!planRes) continue;
  278. const term = planRes.termnum.find(f => ObjectId(termid).equals(f._id));
  279. if (!term) continue;
  280. const { term: termnum } = term;
  281. const classInfo = await this.clamodel.findById(classid);
  282. const { headteacherid, lyteacherid } = classInfo;
  283. const headteacher = await this.headteamodel.findById(headteacherid);
  284. if (headteacher) await this.toSendMsg(headteacher, 'headteacher', termnum);
  285. // 礼仪教师和班主任不是一个人时查礼仪教师是班主任还是任课教师,然后发消息
  286. if (lyteacherid !== headteacherid) {
  287. let lyTeacher = await this.headteamodel.findById(lyteacherid);
  288. if (lyTeacher) await this.toSendMsg(lyTeacher, 'headteacher', termnum);
  289. else {
  290. lyTeacher = await this.teamodel.findById(lyteacherid);
  291. if (lyTeacher) await this.toSendMsg(lyTeacher, 'teacher', termnum);
  292. }
  293. }
  294. // 获取所有任课教师ids
  295. const teacherList = _.compact(_.uniq(lessons.map(i => i.teaid)));
  296. if (teacherList) {
  297. for (const tea of teacherList) {
  298. await this.toSendMsg(tea, 'teacher', termnum);
  299. }
  300. }
  301. }
  302. }
  303. async toSendMsg(teacherInfo, type, term) {
  304. if (teacherInfo) {
  305. let email;
  306. if (type === 'headteacher') {
  307. const { qq } = teacherInfo;
  308. if (qq) email = `${qq}@qq.com`;
  309. } else {
  310. email = teacherInfo.email;
  311. }
  312. // 发邮件
  313. if (email) {
  314. console.error(`${teacherInfo.name}-email:${email}`);
  315. const subject = '吉林省高等学校毕业生就业指导中心通知';
  316. const text = teacherInfo.name + `您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”第${term}期,请您尽快登陆双困生培训系统查看您的安排`;
  317. this.ctx.service.util.sendMail(email, subject, text);
  318. }
  319. // 获取openid,推送
  320. const teacherUser = await this.umodel.findOne({ uid: teacherInfo._id });
  321. if (teacherUser) {
  322. const { openid } = teacherUser;
  323. if (openid) {
  324. // TODO 推送
  325. await this.ctx.service.weixin.sendTemplateDesign(
  326. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  327. openid,
  328. '您有一个新的通知',
  329. '您有新的安排',
  330. `欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”第${term}期`,
  331. '感谢您的使用',
  332. 'http://www.baidu.com'
  333. );
  334. }
  335. }
  336. }
  337. }
  338. }
  339. module.exports = LessonService;