lesson.js 4.1 KB

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