lesson.js 7.8 KB

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