lesson.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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({ planid: id, termid: elm.id, batchid: batch.id });
  42. // 循环班级
  43. for (const cla of _classs) {
  44. // 记录天数
  45. let i = 1;
  46. // 循环天数
  47. const newlesson = [];
  48. for (const day of sedays) {
  49. // 循环课程模板,将模板信息排入班级课程表中
  50. for (const lessm of lessonmode) {
  51. // 循环插入模板信息
  52. if (lessm['day' + i] !== '--') {
  53. let _subid = '';
  54. if (lessm['day' + i + 'type'] === '课程') {
  55. _subid = lessm['day' + i + 'subid'];
  56. } else {
  57. _subid = '';
  58. }
  59. let allday = 0;
  60. if (i === 6) {
  61. allday = _lessonmode.allday;
  62. }
  63. const data = { subid: _subid, subname: lessm['day' + i], date: day, time: lessm.time, day: allday };
  64. newlesson.push(data);
  65. }
  66. }
  67. i = i + 1;
  68. }
  69. const newdata = { termid: elm.id, batchid: batch.id, classid: cla.id, lessons: newlesson };
  70. // 将课程信息填入课程表
  71. await this.model.create(newdata);
  72. }
  73. }
  74. }
  75. }
  76. // 取得日期间所有日期
  77. async getAllDays(begin_date, end_date) {
  78. const errArr = [],
  79. resultArr = [],
  80. dateReg = /^[2]\d{3}-[01]\d-[0123]\d$/;
  81. if (typeof begin_date !== 'string' || begin_date === '' || !dateReg.test(begin_date)) {
  82. return errArr;
  83. }
  84. if (typeof end_date !== 'string' || end_date === '' || !dateReg.test(end_date)) {
  85. return errArr;
  86. }
  87. try {
  88. const beginTimestamp = Date.parse(new Date(begin_date)),
  89. endTimestamp = Date.parse(new Date(end_date));
  90. // 开始日期小于结束日期
  91. if (beginTimestamp > endTimestamp) {
  92. return errArr;
  93. }
  94. // 开始日期等于结束日期
  95. if (beginTimestamp === endTimestamp) {
  96. resultArr.push(begin_date);
  97. return resultArr;
  98. }
  99. let tempTimestamp = beginTimestamp,
  100. tempDate = begin_date;
  101. // 新增日期是否和结束日期相等, 相等跳出循环
  102. while (tempTimestamp !== endTimestamp) {
  103. resultArr.push(tempDate);
  104. // 增加一天
  105. tempDate = moment(tempTimestamp)
  106. .add(1, 'd')
  107. .format('YYYY-MM-DD');
  108. // 将增加时间变为时间戳
  109. tempTimestamp = Date.parse(new Date(tempDate));
  110. }
  111. // 将最后一天放入数组
  112. resultArr.push(end_date);
  113. return resultArr;
  114. } catch (err) {
  115. return errArr;
  116. }
  117. }
  118. }
  119. module.exports = LessonService;