lesson.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. }
  17. // 自动排课私有方法
  18. async autolesson({ id }) {
  19. // 首先将课程表清空
  20. const res = await this.tmodel.findById(id);
  21. if (!res) {
  22. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  23. }
  24. const terms = res.termnum;
  25. const _lessonmode = await this.lmodel.find();
  26. // 循环取得所有期
  27. for (const elm of terms) {
  28. // 根据期id清空课程表
  29. await this.model.deleteMany({ termid: elm.id });
  30. // 清空成功后,循环取得当前期下所有批次信息
  31. const batchs = elm.batchnum;
  32. for (const batch of batchs) {
  33. // 取得当前批次开始结束日期,并根据日期取得所有天数
  34. const sedays = await this.getAllDays(batch.startdate, batch.enddate);
  35. // 根据批次取得当前批次下所有班级
  36. const _classs = await this.clamodel.find({
  37. planid: id,
  38. termid: elm.id,
  39. batchid: batch.id,
  40. });
  41. // 循环班级
  42. for (const cla of _classs) {
  43. // 取得课程模板信息
  44. let lessonmode_ = _.find(_lessonmode, { type: cla.type });
  45. if (!lessonmode_) {
  46. lessonmode_ = _lessonmode[0];
  47. if (!lessonmode_) {
  48. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '课程模板信息不存在');
  49. }
  50. }
  51. // 取得模板内容并转化成json
  52. const lessons_ = JSON.parse(lessonmode_.lessons);
  53. // 记录天数
  54. let i = 1;
  55. // 循环天数
  56. const newlesson = [];
  57. for (const day of sedays) {
  58. // 循环课程模板,将模板信息排入班级课程表中
  59. const teachids = [];
  60. for (const lessm of lessons_) {
  61. // 循环插入模板信息
  62. if (lessm['day' + i] !== '--') {
  63. let _subid = '';
  64. if (lessm['day' + i + 'type'] === '课程') {
  65. _subid = lessm['day' + i + 'subid'];
  66. } else {
  67. _subid = '';
  68. }
  69. let allday = 0;
  70. if (i === 6) {
  71. allday = _lessonmode.allday;
  72. }
  73. const data = {
  74. subid: _subid,
  75. subname: lessm['day' + i],
  76. date: day,
  77. time: lessm.time,
  78. day: allday,
  79. };
  80. // 将教师按照分数的综合成绩排序,上报时间,安排教师.
  81. const teacher_ = await this.autoteacher(_subid, teachids);
  82. if (teacher_) {
  83. data.teaid = teacher_.id;
  84. data.teaname = teacher_.name;
  85. teachids.push(teacher_.id);
  86. }
  87. newlesson.push(data);
  88. }
  89. }
  90. i = i + 1;
  91. }
  92. const newdata = {
  93. termid: elm.id,
  94. batchid: batch.id,
  95. classid: cla.id,
  96. lessons: newlesson,
  97. };
  98. // 将课程信息填入课程表
  99. await this.model.create(newdata);
  100. }
  101. }
  102. }
  103. }
  104. // 自动排教师,按照分数的综合成绩排序,上报时间,安排教师
  105. async autoteacher(subid, teachids) {
  106. // 按照上报时间取得所有老师,进行正序排列
  107. const teachers = await this.teamodel.find({ subid, status: '4' }).sort({ zlscore: '-1', msscore: '-1', xsscore: '-1' });
  108. for (const teaid of teachids) {
  109. _.remove(teachers, item => item.id === teaid);
  110. }
  111. let teacher = {};
  112. if (teachers.length > 0) {
  113. teacher = teachers[0];
  114. }
  115. return teacher;
  116. }
  117. // 取得日期间所有日期
  118. async getAllDays(begin_date, end_date) {
  119. const errArr = [],
  120. resultArr = [],
  121. dateReg = /^[2]\d{3}-[01]\d-[0123]\d$/;
  122. if (
  123. typeof begin_date !== 'string' ||
  124. begin_date === '' ||
  125. !dateReg.test(begin_date)
  126. ) {
  127. return errArr;
  128. }
  129. if (
  130. typeof end_date !== 'string' ||
  131. end_date === '' ||
  132. !dateReg.test(end_date)
  133. ) {
  134. return errArr;
  135. }
  136. try {
  137. const beginTimestamp = Date.parse(new Date(begin_date)),
  138. endTimestamp = Date.parse(new Date(end_date));
  139. // 开始日期小于结束日期
  140. if (beginTimestamp > endTimestamp) {
  141. return errArr;
  142. }
  143. // 开始日期等于结束日期
  144. if (beginTimestamp === endTimestamp) {
  145. resultArr.push(begin_date);
  146. return resultArr;
  147. }
  148. let tempTimestamp = beginTimestamp,
  149. tempDate = begin_date;
  150. // 新增日期是否和结束日期相等, 相等跳出循环
  151. while (tempTimestamp !== endTimestamp) {
  152. resultArr.push(tempDate);
  153. // 增加一天
  154. tempDate = moment(tempTimestamp).add(1, 'd').format('YYYY-MM-DD');
  155. // 将增加时间变为时间戳
  156. tempTimestamp = Date.parse(new Date(tempDate));
  157. }
  158. // 将最后一天放入数组
  159. resultArr.push(end_date);
  160. return resultArr;
  161. } catch (err) {
  162. return errArr;
  163. }
  164. }
  165. // 根据计划id、教师id查询所有班级信息
  166. async classbyteaid({ planid, teaid }) {
  167. // 取得传入的计划id与教师id
  168. // 根据计划id取得所有期
  169. const plan = await this.tmodel.findById(planid);
  170. if (!plan) {
  171. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  172. }
  173. const terms = await plan.termnum;
  174. // 循环取得所有期信息
  175. const data = [];
  176. for (const term of terms) {
  177. // 根据期id与教师id查出课程班级信息
  178. const lessons = await this.model.find({
  179. termid: term.id,
  180. 'lessons.teaid': teaid,
  181. });
  182. const batchs = await term.batchnum;
  183. for (const elm of lessons) {
  184. const newdata = {};
  185. const batch = await batchs.id(elm.batchid);
  186. newdata.planid = planid;
  187. newdata.title = plan.title;
  188. newdata.termid = elm.termid;
  189. newdata.term = term.term;
  190. newdata.batchid = elm.batchid;
  191. newdata.batch = batch.batch;
  192. newdata.classid = elm.classid;
  193. if (elm.classid) {
  194. const cla = await this.clamodel.findById(elm.classid);
  195. if (cla) {
  196. newdata.classname = cla.name;
  197. }
  198. }
  199. const _lessons = elm.lessons.filter(item => item.teaid === teaid);
  200. newdata.lessons = _lessons;
  201. data.push(newdata);
  202. }
  203. }
  204. return data;
  205. }
  206. // 根据计划id、教师id查询所有班级信息
  207. async teaclass({ planid, teaid }) {
  208. // 取得传入的计划id与教师id
  209. // 根据计划id取得所有期
  210. const plan = await this.tmodel.findById(planid);
  211. if (!plan) {
  212. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  213. }
  214. const terms = await plan.termnum;
  215. // 循环取得所有期信息
  216. const data = [];
  217. for (const term of terms) {
  218. // 根据期id与教师id查出课程班级信息
  219. const lessons = await this.model.find({
  220. termid: term.id,
  221. 'lessons.teaid': teaid,
  222. });
  223. for (const elm of lessons) {
  224. if (elm.classid) {
  225. const cla = await this.ctx.service.class.fetch({ id: elm.classid });
  226. data.push(cla);
  227. }
  228. }
  229. }
  230. return data;
  231. }
  232. async uplessones(data) {
  233. for (const _data of data) {
  234. await this.model.findByIdAndUpdate(_data.id, _data);
  235. }
  236. }
  237. }
  238. module.exports = LessonService;