lesson.js 7.0 KB

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