|
@@ -6,12 +6,16 @@ const _ = require('lodash');
|
|
|
const { ObjectId } = require('mongoose').Types;
|
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const moment = require('moment');
|
|
|
|
|
|
class TeaplanService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, 'teaplan');
|
|
|
this.model = this.ctx.model.Teaplan;
|
|
|
this.hmodel = this.ctx.model.Headteacher;
|
|
|
+ this.tmodel = this.ctx.model.Trainplan;
|
|
|
+ this.cmodel = this.ctx.model.Class;
|
|
|
+ this.dmodel = this.ctx.model.Department;
|
|
|
}
|
|
|
|
|
|
// async findteacher({ batchid }) {
|
|
@@ -37,6 +41,101 @@ class TeaplanService extends CrudService {
|
|
|
// return newheadteachers;
|
|
|
// }
|
|
|
|
|
|
+ async divide({ trainplanid }) {
|
|
|
+ const data = [];
|
|
|
+ // 根据全年计划表id查出对应的全年计划详细信息
|
|
|
+ const trainplan = await this.tmodel.findById(trainplanid);
|
|
|
+ // 将全年计划中的批次信息取出放在一个数组中
|
|
|
+ let batchList = [];
|
|
|
+ for (const term of trainplan.termnum) {
|
|
|
+ for (const batch of term.batchnum) {
|
|
|
+ batchList.push(batch);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查询班主任全年计划表的全部信息
|
|
|
+ const teaplanList = await this.model.find();
|
|
|
+ let noteaList = [];
|
|
|
+ batchList = _.slice(batchList, 0, 3);
|
|
|
+ // 遍历所有批次信息
|
|
|
+ for (const _batch of batchList) {
|
|
|
+ // 遍历班主任全年计划表
|
|
|
+ for (const teaplan of teaplanList) {
|
|
|
+ // 遍历班主任全年计划表中的不能上课的日期
|
|
|
+ for (const nodate of teaplan.nodate) {
|
|
|
+ // 如果不能上课的日期在该批次的开始时间和结束时间中,将该班主任加入不能上课的班主任数组中
|
|
|
+ if (moment(nodate).isBetween(_batch.startdate, _batch.enddate)) {
|
|
|
+ noteaList.push(teaplan);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 过滤出已分配的数据中该班主任的数据
|
|
|
+ const teaInfo = _.filter(data, item => item.headteacherid === teaplan.headteacherid);
|
|
|
+ // 如果已分配的数据中该班主任的数据长度大于0
|
|
|
+ if (teaInfo.length > 0) {
|
|
|
+ // 遍历已分配的数据中该班主任的数据
|
|
|
+ for (const _teaInfo of teaInfo) {
|
|
|
+ // 查询该班主任已分配的数据中的班级信息
|
|
|
+ const _class = await this.cmodel.findById(_teaInfo.classid);
|
|
|
+ // 根据班级的批次id查出该班级的开始时间和结束时间
|
|
|
+ const batchInfo = _.filter(batchList, item => item.id === _class.batchid);
|
|
|
+ // 如果该班级的开始时间或结束时间在当前遍历批次的开始时间和结束时间中,将该班主任加入不能上课的班主任数组中
|
|
|
+ if (moment(batchInfo.startdate).isBetween(_batch.startdate, _batch.enddate) || moment(batchInfo.enddate).isBetween(_batch.startdate, _batch.enddate)) {
|
|
|
+ noteaList.push(teaplan);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 将数组去重
|
|
|
+ noteaList = _.uniqWith(noteaList, _.isEqual);
|
|
|
+ // 将班主任全年计划表过滤,去除在不能上课的班主任数组中已存在的数据
|
|
|
+ let teaList = _.difference(teaplanList, noteaList);
|
|
|
+ // 将不能上课的班主任数组清空
|
|
|
+ noteaList = [];
|
|
|
+ // 遍历该批次能上课的班主任列表
|
|
|
+ for (const teaplan of teaList) {
|
|
|
+ // 计算出每个班主任担任过班主任的次数
|
|
|
+ teaplan.teacount = await this.cmodel.count({ headteacherid: teaplan.headteacherid });
|
|
|
+ }
|
|
|
+ let departmentList = [];
|
|
|
+ // 遍历排序过的班主任数组
|
|
|
+ for (const teaplan of teaList) {
|
|
|
+ // 将班主任的部门放到一个数组中
|
|
|
+ const headteacher = await this.hmodel.findById(teaplan.headteacherid);
|
|
|
+ departmentList.push({ deparmentid: headteacher.department });
|
|
|
+ teaplan.deparmentid = headteacher.department;
|
|
|
+
|
|
|
+ }
|
|
|
+ // 将数组去重
|
|
|
+ departmentList = _.uniqWith(departmentList, _.isEqual);
|
|
|
+ for (const department of departmentList) {
|
|
|
+ // 计算出能该批次能上课的班主任中各部门有多少人
|
|
|
+ const teacount = _.filter(teaList, item => item.deparmentid === department.deparmentid).length;
|
|
|
+ // 计算出各部门有多少人
|
|
|
+ const departmentcount = await this.hmodel.count({ department: department.deparmentid });
|
|
|
+ // 如果部门中能上课的班主任数量减去部门中人员的数量小于一,那么将能上课的班主任数组中部门为该部门的班主任移除一个
|
|
|
+ if ((departmentcount - teacount) < 1) {
|
|
|
+ let _noteaList = _.filter(teaList, item => item.deparmentid === department.deparmentid);
|
|
|
+ _noteaList = _.orderBy(_noteaList, [ 'teacount' ], [ 'desc' ]);
|
|
|
+ teaList = _.difference(teaList, _noteaList[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 将该批次最终筛选出的能上课的班主任数组按每个班主任担任过班主任的次数倒序排序
|
|
|
+ teaList = _.orderBy(teaList, [ 'teacount' ], [ 'asc' ]);
|
|
|
+ // 查出该批次下所有的班级
|
|
|
+ const classList = await this.cmodel.find({ batchid: _batch.id });
|
|
|
+ // 循环该批次下所有的班级
|
|
|
+ let index = 0;
|
|
|
+ for (const _class of classList) {
|
|
|
+ if (teaList[index]) {
|
|
|
+ data.push({ classid: _class.id, headteacherid: teaList[index].headteacherid });
|
|
|
+ } else {
|
|
|
+ data.push({ classid: _class.id, headteacherid: undefined });
|
|
|
+ }
|
|
|
+ index = index + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
module.exports = TeaplanService;
|