'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const moment = require('moment'); const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class LessonService extends CrudService { constructor(ctx) { super(ctx, 'lesson'); this.model = this.ctx.model.Lesson; this.tmodel = this.ctx.model.Trainplan; this.clamodel = this.ctx.model.Class; this.lmodel = this.ctx.model.Lessonmode; } // 自动排课私有方法 async autolesson({ id }) { // 首先将课程表清空 const res = await this.tmodel.findById(id); if (!res) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在'); } // 取得课程模板信息 const _lessonmode = await this.lmodel.findOne({ type: '0' }); if (!_lessonmode) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '课程模板信息不存在'); } // 取得模板内容并转化成json const lessonmode = JSON.parse(_lessonmode.lessons); const terms = res.termnum; // 循环取得所有期 for (const elm of terms) { // 根据期id清空课程表 await this.model.deleteMany({ termid: elm.id }); // 清空成功后,循环取得当前期下所有批次信息 const batchs = elm.batchnum; for (const batch of batchs) { // 取得当前批次开始结束日期,并根据日期取得所有天数 const sedays = await this.getAllDays(batch.startdate, batch.enddate); // 根据批次取得当前批次下所有班级 const _classs = await this.clamodel.find({ planid: id, termid: elm.id, batchid: batch.id }); // 循环班级 for (const cla of _classs) { // 记录天数 let i = 1; // 循环天数 const newlesson = []; for (const day of sedays) { // 循环课程模板,将模板信息排入班级课程表中 for (const lessm of lessonmode) { // 循环插入模板信息 if (lessm['day' + i] !== '--') { let _subid = ''; if (lessm['day' + i + 'type'] === '课程') { _subid = lessm['day' + i + 'subid']; } else { _subid = ''; } let allday = 0; if (i === 6) { allday = _lessonmode.allday; } const data = { subid: _subid, subname: lessm['day' + i], date: day, time: lessm.time, day: allday }; newlesson.push(data); } } i = i + 1; } const newdata = { termid: elm.id, batchid: batch.id, classid: cla.id, lessons: newlesson }; // 将课程信息填入课程表 await this.model.create(newdata); } } } } // 取得日期间所有日期 async getAllDays(begin_date, end_date) { const errArr = [], resultArr = [], dateReg = /^[2]\d{3}-[01]\d-[0123]\d$/; if (typeof begin_date !== 'string' || begin_date === '' || !dateReg.test(begin_date)) { return errArr; } if (typeof end_date !== 'string' || end_date === '' || !dateReg.test(end_date)) { return errArr; } try { const beginTimestamp = Date.parse(new Date(begin_date)), endTimestamp = Date.parse(new Date(end_date)); // 开始日期小于结束日期 if (beginTimestamp > endTimestamp) { return errArr; } // 开始日期等于结束日期 if (beginTimestamp === endTimestamp) { resultArr.push(begin_date); return resultArr; } let tempTimestamp = beginTimestamp, tempDate = begin_date; // 新增日期是否和结束日期相等, 相等跳出循环 while (tempTimestamp !== endTimestamp) { resultArr.push(tempDate); // 增加一天 tempDate = moment(tempTimestamp) .add(1, 'd') .format('YYYY-MM-DD'); // 将增加时间变为时间戳 tempTimestamp = Date.parse(new Date(tempDate)); } // 将最后一天放入数组 resultArr.push(end_date); return resultArr; } catch (err) { return errArr; } } } module.exports = LessonService;