liuyu 5 лет назад
Родитель
Сommit
f86bdf3060
4 измененных файлов с 134 добавлено и 9 удалено
  1. 5 0
      app/controller/lesson.js
  2. 10 9
      app/model/lesson.js
  3. 1 0
      app/router.js
  4. 118 0
      app/service/lesson.js

+ 5 - 0
app/controller/lesson.js

@@ -13,6 +13,11 @@ class LessonController extends Controller {
     this.service = this.ctx.service.lesson;
   }
 
+  // 自动排课
+  async autolesson() {
+    const res = await this.service.autolesson(this.ctx.params);
+    this.ctx.ok({ res });
+  }
 
 }
 

+ 10 - 9
app/model/lesson.js

@@ -3,21 +3,22 @@ const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 
 // 课程信息表
-// const lessonInfo = new Schema({
-//   teaname: { type: String, required: false, maxLength: 500 }, // 教师名称
-//   teaid: { type: String, required: false, maxLength: 500 }, // 教师id
-//   subname: { type: String, required: false, maxLength: 200 }, // 科目名称
-//   subid: { type: String, required: false, maxLength: 200 }, // 科目id
-//   date: { type: String, required: false, maxLength: 500 }, // 日期
-//   day: { type: String, required: false, maxLength: 500 }, // 课程天数,半天或一天
-// });
+const lessonInfo = new Schema({
+  teaname: { type: String, required: false, maxLength: 500 }, // 教师名称
+  teaid: { type: String, required: false, maxLength: 500 }, // 教师id
+  subname: { type: String, required: false, maxLength: 200 }, // 科目名称
+  subid: { type: String, required: false, maxLength: 200 }, // 科目id
+  date: { type: String, required: false, maxLength: 500 }, // 日期
+  time: { type: String, required: false, maxLength: 500 }, // 时间
+  day: { type: String, required: false, maxLength: 500 }, // 课程天数,半天或一天
+});
 
 // 课程表
 const LessonSchema = {
   classid: { type: String, required: true, maxLength: 500 }, // 班级id
   termid: { type: String, required: true, maxLength: 500 }, // 期id
   batchid: { type: String, required: true, maxLength: 500 }, // 批次id
-  lessons: { type: String, required: false }, // 课程信息
+  lessons: { type: [ lessonInfo ], required: false, select: true }, // 课程信息
 };
 
 const schema = new Schema(LessonSchema, { toJSON: { virtuals: true } });

+ 1 - 0
app/router.js

@@ -74,6 +74,7 @@ module.exports = app => {
   // 课程表设置路由
   router.resources('lesson', '/api/train/lesson', controller.lesson); // index、create、show、destroy
   router.post('lesson', '/api/train/lesson/update/:id', controller.lesson.update);
+  router.post('lesson', '/api/train/lesson/autolesson/:id', controller.lesson.autolesson);// 自动排课
 
   // 培训计划学校上报时间表设置路由
   router.resources('schtime', '/api/train/schtime', controller.schtime); // index、create、show、destroy

+ 118 - 0
app/service/lesson.js

@@ -4,6 +4,7 @@
 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;
 
@@ -11,6 +12,123 @@ 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 });
+        // 记录天数
+        let i = 1;
+        // 循环班级
+        for (const cla of _classs) {
+          // 循环天数
+          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 = '';
+                }
+                const data = { subid: _subid, date: day, time: lessm.time };
+                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;
+    }
   }
 
 }