lesson.js 6.8 KB

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