|
@@ -432,6 +432,103 @@ class LessonService extends CrudService {
|
|
|
return person;
|
|
|
}
|
|
|
|
|
|
+ // 新排课,从计划中拿出来对应的课表
|
|
|
+ async newArrange({ planid, termid }) {
|
|
|
+ const lmodelList = await this.lmodel.find();
|
|
|
+ const trainplan = await this.tmodel.findById(planid);
|
|
|
+ if (!trainplan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '计划不存在');
|
|
|
+ let { termnum } = trainplan;
|
|
|
+ // 指定期的数据
|
|
|
+ termnum = JSON.parse(JSON.stringify(termnum));
|
|
|
+ if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该期不存在');
|
|
|
+ // 该期的课表,没有就添加
|
|
|
+ await this.model.deleteMany({ termid });
|
|
|
+ const sterm = termnum.find(f => f._id === termid);
|
|
|
+ const { batchnum } = sterm;
|
|
|
+ // 确保batchnum是数组
|
|
|
+ if (!_.isArray(batchnum)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该期下批次数据错误');
|
|
|
+ for (const b of batchnum) {
|
|
|
+ const { _id: batchid, class: classArrange, type, startdate, enddate } = b;
|
|
|
+ const classList = await this.clamodel.find({ planid, termid, batchid });
|
|
|
+ // 确保list是数组
|
|
|
+ if (!classList || classList.length === 0) continue;
|
|
|
+ const clist = JSON.parse(JSON.stringify(classList));
|
|
|
+ for (const c of clist) {
|
|
|
+ // 排指定班
|
|
|
+ const { name, ...lessinfo } = await this.partsOfClass(c);
|
|
|
+ // 找到指定班级的安排 这里目前只能用name找,这个地方很不稳定,需要找到其他的具有唯一性的判断来执行这个find
|
|
|
+ const clalr = classArrange.find(f => f.name === name);
|
|
|
+ // 没找到指定班级的安排
|
|
|
+ if (!clalr) {
|
|
|
+ console.error('没有找到指定安排');
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ const { lessons: ntemplate } = clalr;
|
|
|
+ // 如果没有模板
|
|
|
+ if (!ntemplate) {
|
|
|
+ console.error('没有找到指定班级安排模板');
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 根据班级类型找到原课表模板
|
|
|
+ let lessonModel = lmodelList.find(f => f.code === type);
|
|
|
+ lessonModel = JSON.parse(JSON.stringify(lessonModel));
|
|
|
+ // 原模板
|
|
|
+ let { lessons: otemplate } = lessonModel;
|
|
|
+ otemplate = JSON.parse(otemplate);
|
|
|
+ // 日期列表,感谢裕哥能给我留个可用的东西
|
|
|
+ const dayList = await this.getAllDays(startdate, enddate);
|
|
|
+ const lessons = [];
|
|
|
+ for (let i = 0; i < dayList.length; i++) {
|
|
|
+ for (const ot of otemplate) {
|
|
|
+ const date = dayList[i];
|
|
|
+ const { time } = ot;
|
|
|
+ const keys = Object.keys(ot).filter(f => f.includes(`day${i + 1}`));
|
|
|
+ const kvs = {};
|
|
|
+ for (const key of keys) {
|
|
|
+ // 将原课表的每日,每个时间段的安排整理成object
|
|
|
+ if (key.startsWith('day') && key.endsWith('type')) {
|
|
|
+ // console.log(`${key}=>${ot[key]}`);
|
|
|
+ // kvs.type = ot[key];
|
|
|
+ } else if (key.startsWith('day') && key.endsWith('subid')) {
|
|
|
+ kvs.subid = ot[key];
|
|
|
+ } else {
|
|
|
+ kvs.subname = ot[key];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 整理完的object,如果有subid,就是课程,找教师
|
|
|
+ const tsubid = _.get(kvs, 'subid');
|
|
|
+ if (tsubid) {
|
|
|
+ const r = ntemplate.find(f => f.subid === tsubid);
|
|
|
+ if (r) {
|
|
|
+ const { teaid, teaname } = r;
|
|
|
+ if (teaid && teaname) {
|
|
|
+ kvs.teaid = teaid;
|
|
|
+ kvs.teaname = teaname;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const obj = { date, time, ...kvs };
|
|
|
+ lessons.push(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ // 最后整合
|
|
|
+ const classLesson = { ...lessinfo, lessons };
|
|
|
+ // 保存
|
|
|
+ await this.model.create(classLesson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新排课,将班级层面处理抽出来
|
|
|
+ async partsOfClass(classinfo) {
|
|
|
+ classinfo = JSON.parse(JSON.stringify(classinfo));
|
|
|
+ const { termid, batchid, _id: classid, name } = classinfo;
|
|
|
+ const obj = { termid, batchid, classid, name };
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
module.exports = LessonService;
|