|
@@ -31,17 +31,11 @@ class TeaplanService extends CrudService {
|
|
|
}
|
|
|
const term = await trainplan.termnum.id(termid);
|
|
|
if (!term) {
|
|
|
- throw new BusinessError(
|
|
|
- ErrorCode.DATA_NOT_EXIST,
|
|
|
- '全年计划内期信息不存在'
|
|
|
- );
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内期信息不存在');
|
|
|
}
|
|
|
const batch = await term.batchnum.id(batchid);
|
|
|
if (!batch) {
|
|
|
- throw new BusinessError(
|
|
|
- ErrorCode.DATA_NOT_EXIST,
|
|
|
- '全年计划内批次信息不存在'
|
|
|
- );
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内批次信息不存在');
|
|
|
}
|
|
|
const newheadteachers = [];
|
|
|
|
|
@@ -53,11 +47,7 @@ class TeaplanService extends CrudService {
|
|
|
if (teaplan) {
|
|
|
|
|
|
const nodates = teaplan.nodate;
|
|
|
- const iswork = await this.teacheriswork(
|
|
|
- nodates,
|
|
|
- batch.startdate,
|
|
|
- batch.enddate
|
|
|
- );
|
|
|
+ const iswork = await this.teacheriswork(nodates, batch.startdate, batch.enddate);
|
|
|
if (iswork) {
|
|
|
newheadteachers.push(headteacher);
|
|
|
} else {
|
|
@@ -100,91 +90,134 @@ class TeaplanService extends CrudService {
|
|
|
}
|
|
|
|
|
|
async divide({ trainplanid }) {
|
|
|
- const data = [];
|
|
|
|
|
|
const trainplan = await this.tmodel.findById(trainplanid);
|
|
|
|
|
|
- const teaplanList = await this.model.find({ trainplanid });
|
|
|
+ const teaplanList = await this.model.find({ trainplanid }).lean();
|
|
|
|
|
|
- const headteacherList = await this.hmodel.find();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ const headteacherList = await this.hmodel.find().lean();
|
|
|
+
|
|
|
+
|
|
|
+ let teaListAll_ = [];
|
|
|
+ for (const teac of headteacherList) {
|
|
|
+
|
|
|
+ const teacount = await this.cmodel.count({ headteacherid: teac.id, planid: trainplanid });
|
|
|
+
|
|
|
+ const teaplan = teaplanList.find(f => f.headteacherid === (teac.id || teac._id));
|
|
|
+ let nodateList = [];
|
|
|
+ if (teaplan) {
|
|
|
+ const { nodate = [] } = teaplan;
|
|
|
+ nodateList = nodate;
|
|
|
+ }
|
|
|
+ const newdata = { ...JSON.parse(JSON.stringify(teac)), teacount, nodateList };
|
|
|
+ teaListAll_.push(newdata);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
+
|
|
|
+ const arrangeList = [];
|
|
|
for (const term of trainplan.termnum) {
|
|
|
+
|
|
|
+ const deptInfo = this.departmentcount(teaListAll_);
|
|
|
for (const batch of term.batchnum) {
|
|
|
- const teaListAll = _.cloneDeep(
|
|
|
- JSON.parse(JSON.stringify(headteacherList))
|
|
|
- );
|
|
|
- for (const teaplan of teaplanList) {
|
|
|
- if (teaplan.nodate && teaplan.nodate.length > 0) {
|
|
|
-
|
|
|
- for (const nodate of teaplan.nodate) {
|
|
|
-
|
|
|
- if (
|
|
|
- moment(nodate).isBetween(
|
|
|
- batch.startdate,
|
|
|
- batch.enddate,
|
|
|
- null,
|
|
|
- '[]'
|
|
|
- )
|
|
|
- ) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- _.remove(
|
|
|
- teaListAll,
|
|
|
- item => item.id === teaplan.headteacherid
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- let teaListAll_ = [];
|
|
|
- for (const teac of teaListAll) {
|
|
|
-
|
|
|
- const teacount = await this.cmodel.count({ headteacherid: teac.id });
|
|
|
- const newdata = { ...JSON.parse(JSON.stringify(teac)), teacount };
|
|
|
- teaListAll_.push(newdata);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
teaListAll_ = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
|
|
|
+ let tempTeaList = [];
|
|
|
+
|
|
|
+ for (const tea of teaListAll_) {
|
|
|
+ if (tea.nodateList.length <= 0) {
|
|
|
+ tempTeaList.push(tea);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ const is_in = tea.nodateList.find(f => moment(f).isBetween(batch.startdate, batch.enddate, null, '[]'));
|
|
|
+ if (is_in) continue;
|
|
|
+
|
|
|
+ const teaArrange = arrangeList.find(f => f.tea === (tea.id || tea._id) && !this.isrepeat(batch.startdate, batch.enddate, f.s, f.e));
|
|
|
+ if (!teaArrange) tempTeaList.push(tea);
|
|
|
+ }
|
|
|
+ const cantUseList = [];
|
|
|
|
|
|
-
|
|
|
const classList = await batch.class;
|
|
|
for (const _class of classList) {
|
|
|
-
|
|
|
- const departmentList = await this.departmentcount(teaListAll_);
|
|
|
- let index = 0;
|
|
|
- for (const _tea of teaListAll_) {
|
|
|
-
|
|
|
- const deptinfo = _.find(departmentList, function(o) {
|
|
|
- return o.deparmentid === _tea.department;
|
|
|
- });
|
|
|
- if (deptinfo.dcount - index > 1) {
|
|
|
- _class.headteacherid = _tea.id;
|
|
|
- _.remove(teaListAll_, item => item.id === _tea.id);
|
|
|
- _.remove(teaListAll, item => item.id === _tea.id);
|
|
|
- index = index + 1;
|
|
|
- break;
|
|
|
- }
|
|
|
+
|
|
|
+ _class.headteacherid = null;
|
|
|
+ let selectTea;
|
|
|
+
|
|
|
+ tempTeaList = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
|
|
|
+
|
|
|
+ while (!selectTea) {
|
|
|
+ const tempSelect = this.findTeacher(tempTeaList, deptInfo, cantUseList);
|
|
|
+
|
|
|
+
|
|
|
+ const dept = deptInfo.find(f => f.dept === tempSelect.department);
|
|
|
+ if (dept.dcount - 1 >= 1) {
|
|
|
+
|
|
|
+ const is_in = classList.find(f => f.headteacherid === (tempSelect.id || tempSelect._id));
|
|
|
+ if (!is_in) selectTea = tempSelect;
|
|
|
+ } else cantUseList.push(tempSelect.id || tempSelect._id);
|
|
|
+
|
|
|
+ if (cantUseList.length === tempTeaList.length) break;
|
|
|
}
|
|
|
+
|
|
|
+ const inListTeaData = teaListAll_.find(f => (f.id || f._id) === (selectTea.id || selectTea._id));
|
|
|
+
|
|
|
+ if (!inListTeaData) continue;
|
|
|
+ inListTeaData.teacount = inListTeaData.teacount + 1;
|
|
|
+ const inDeptData = deptInfo.find(f => f.dept === selectTea.department);
|
|
|
+ inDeptData.notUse = inDeptData.notUse - 1;
|
|
|
+ const newPer = this.computedPer(inDeptData.notUse, inDeptData.dcount);
|
|
|
+ inDeptData.per = newPer;
|
|
|
+ _class.headteacherid = selectTea.id || selectTea._id;
|
|
|
+ arrangeList.push({ teaName: inListTeaData.name, tea: inListTeaData.id || inListTeaData._id, s: batch.startdate, e: batch.enddate, c: _class.id || _class._id });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
return await trainplan.save();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- async isrepeat(startdate1, enddate1, startdate2, enddate2) {
|
|
|
+ computedPer(n1, total) {
|
|
|
+ return _.floor(_.multiply(_.divide(n1, total), 100));
|
|
|
+ }
|
|
|
+
|
|
|
+ * 选择最优教师
|
|
|
+ * @param {Array} tempTeaList 可以安排的教师列表
|
|
|
+ * @param {Array} deptInfo 部门情况列表
|
|
|
+ * @param {Array} cantUseList 分配之后发现不能用的教师
|
|
|
+ */
|
|
|
+ findTeacher(tempTeaList, deptInfo, cantUseList) {
|
|
|
+
|
|
|
+ tempTeaList = tempTeaList.filter(f => !cantUseList.includes(f.id || f._id));
|
|
|
+ const fir = _.head(tempTeaList);
|
|
|
+ const times = fir.teacount;
|
|
|
+ const sameTimesTea = tempTeaList.filter(f => f.teacount === times);
|
|
|
+
|
|
|
+ let selectTea;
|
|
|
+ if (sameTimesTea.length > 0) {
|
|
|
+
|
|
|
+ const deptOrderList = _.orderBy(deptInfo, [ 'per', 'notUse' ], [ 'desc', 'desc' ]);
|
|
|
+
|
|
|
+ for (const deptData of deptOrderList) {
|
|
|
+ selectTea = sameTimesTea.find(f => f.department === deptData.dept);
|
|
|
+ if (selectTea) break;
|
|
|
+ }
|
|
|
+ } else if (sameTimesTea.length === 1) {
|
|
|
+
|
|
|
+ selectTea = _.head(sameTimesTea);
|
|
|
+ }
|
|
|
+ return selectTea;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ isrepeat(startdate1, enddate1, startdate2, enddate2) {
|
|
|
let result = true;
|
|
|
if (moment(enddate1).isSameOrBefore(startdate2)) {
|
|
|
result = false;
|
|
@@ -195,32 +228,19 @@ class TeaplanService extends CrudService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- async departmentcount(teaList) {
|
|
|
- let departmentList = [];
|
|
|
- for (const teaplan of teaList) {
|
|
|
- departmentList.push({ deparmentid: teaplan.department });
|
|
|
+
|
|
|
+
|
|
|
+ departmentcount(teaList) {
|
|
|
+ const group = _.groupBy(teaList, 'department');
|
|
|
+ let result = [];
|
|
|
+ for (const dept in group) {
|
|
|
+ const obj = { dept, dcount: group[dept].length, notUse: group[dept].length, per: 100 };
|
|
|
+ result.push(obj);
|
|
|
}
|
|
|
-
|
|
|
- departmentList = _.uniqWith(departmentList, _.isEqual);
|
|
|
- for (const department of departmentList) {
|
|
|
-
|
|
|
- const dnum = _.filter(
|
|
|
- teaList,
|
|
|
- item => item.department === department.deparmentid
|
|
|
- ).length;
|
|
|
- department.dnum = dnum;
|
|
|
-
|
|
|
- const dcount = await this.hmodel.count({
|
|
|
- department: department.deparmentid,
|
|
|
- });
|
|
|
- department.dcount = dcount;
|
|
|
- }
|
|
|
- return departmentList;
|
|
|
+
|
|
|
+ result = _.orderBy(result, [ 'per', 'notUse', 'dcount' ]);
|
|
|
+ return result;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
module.exports = TeaplanService;
|