123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- '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;
- this.teamodel = this.ctx.model.Teacher;
- this.stumodel = this.ctx.model.Student;
- this.schmodel = this.ctx.model.School;
- }
- // 自动排课私有方法
- async autolesson({ id }) {
- // 首先将课程表清空
- const res = await this.tmodel.findById(id);
- if (!res) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
- }
- const terms = res.termnum;
- const _lessonmode = await this.lmodel.find();
- // 循环取得所有期
- 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 lessonmode_ = _.find(_lessonmode, { type: cla.type });
- if (!lessonmode_) {
- lessonmode_ = _lessonmode[0];
- if (!lessonmode_) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '课程模板信息不存在');
- }
- }
- // 取得模板内容并转化成json
- const lessons_ = JSON.parse(lessonmode_.lessons);
- // 记录天数
- let i = 1;
- // 循环天数
- const newlesson = [];
- for (const day of sedays) {
- // 循环课程模板,将模板信息排入班级课程表中
- const teachids = [];
- for (const lessm of lessons_) {
- // 循环插入模板信息
- if (lessm['day' + i] !== '--') {
- let _subid = '';
- if (lessm['day' + i + 'type'] === '课程') {
- _subid = lessm['day' + i + 'subid'];
- } else {
- _subid = '';
- }
- let allday = 0;
- if (i === 6) {
- // 判断是否有外市的学生有的时候 将其设置为半天
- const ishalfday = await this.ishalfday(cla.id);
- if (ishalfday) {
- allday = 1;
- }
- }
- const data = {
- subid: _subid,
- subname: lessm['day' + i],
- date: day,
- time: lessm.time,
- day: allday,
- };
- // 将教师按照分数的综合成绩排序,上报时间,安排教师.
- const teacher_ = await this.autoteacher(_subid, teachids);
- if (teacher_) {
- data.teaid = teacher_.id;
- data.teaname = teacher_.name;
- teachids.push(teacher_.id);
- }
- 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 autoteacher(subid, teachids) {
- // 按照上报时间取得所有老师,进行正序排列
- const teachers = await this.teamodel.find({ subid, status: '4' }).sort({ zlscore: '-1', msscore: '-1', xsscore: '-1' });
- for (const teaid of teachids) {
- _.remove(teachers, item => item.id === teaid);
- }
- let teacher = {};
- if (teachers.length > 0) {
- teacher = teachers[0];
- }
- return teacher;
- }
- // 判断是否为半天
- async ishalfday(classid) {
- // 通过班级id取得所有学生
- const students = await this.stumodel.find({ classid });
- let res = false;
- for (const stu of students) {
- const sch = await this.schmodel.findOne({ code: stu.schid });
- if (sch && sch.hascar === '0') {
- res = true;
- break;
- }
- }
- return res;
- }
- // 取得日期间所有日期
- 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;
- }
- }
- // 根据计划id、教师id查询所有班级信息
- async classbyteaid({ planid, teaid }) {
- // 取得传入的计划id与教师id
- // 根据计划id取得所有期
- const plan = await this.tmodel.findById(planid);
- if (!plan) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
- }
- const terms = await plan.termnum;
- // 循环取得所有期信息
- const data = [];
- for (const term of terms) {
- // 根据期id与教师id查出课程班级信息
- const lessons = await this.model.find({
- termid: term.id,
- 'lessons.teaid': teaid,
- });
- const batchs = await term.batchnum;
- for (const elm of lessons) {
- const newdata = {};
- const batch = await batchs.id(elm.batchid);
- newdata.planid = planid;
- newdata.title = plan.title;
- newdata.termid = elm.termid;
- newdata.term = term.term;
- newdata.batchid = elm.batchid;
- newdata.batch = batch.batch;
- newdata.classid = elm.classid;
- if (elm.classid) {
- const cla = await this.clamodel.findById(elm.classid);
- if (cla) {
- newdata.classname = cla.name;
- }
- }
- const _lessons = elm.lessons.filter(item => item.teaid === teaid);
- newdata.lessons = _lessons;
- data.push(newdata);
- }
- }
- return data;
- }
- // 根据计划id、教师id查询所有班级信息
- async teaclass({ planid, teaid }) {
- // 取得传入的计划id与教师id
- // 根据计划id取得所有期
- const plan = await this.tmodel.findById(planid);
- if (!plan) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
- }
- const terms = await plan.termnum;
- // 循环取得所有期信息
- const data = [];
- for (const term of terms) {
- // 根据期id与教师id查出课程班级信息
- const lessons = await this.model.find({
- termid: term.id,
- 'lessons.teaid': teaid,
- });
- for (const elm of lessons) {
- if (elm.classid) {
- const cla = await this.ctx.service.class.fetch({ id: elm.classid });
- data.push(cla);
- }
- }
- }
- return data;
- }
- async uplessones(data) {
- for (const _data of data) {
- await this.model.findByIdAndUpdate(_data.id, _data);
- }
- }
- }
- module.exports = LessonService;
|