teaplan.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const moment = require('moment');
  8. class TeaplanService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'teaplan');
  11. this.model = this.ctx.model.Teaplan;
  12. this.hmodel = this.ctx.model.Headteacher;
  13. this.tmodel = this.ctx.model.Trainplan;
  14. this.cmodel = this.ctx.model.Class;
  15. this.dmodel = this.ctx.model.Department;
  16. this.submodel = this.ctx.model.Subject;
  17. this.teamodel = this.ctx.model.Teacher;
  18. }
  19. // 查询所有班主任带是否能带班标记
  20. async findteacher({ planid, termid, batchid }) {
  21. // 查询所有班主任信息
  22. const headteachers = await this.hmodel.find();
  23. // 根据批次id取得当前批次具体信息
  24. const trainplan = await this.tmodel.findById(planid);
  25. if (!trainplan) {
  26. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  27. }
  28. const term = await trainplan.termnum.id(termid);
  29. if (!term) {
  30. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内期信息不存在');
  31. }
  32. const batch = await term.batchnum.id(batchid);
  33. if (!batch) {
  34. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内批次信息不存在');
  35. }
  36. const newheadteachers = [];
  37. // 遍历班主任信息
  38. for (const headteacher of headteachers) {
  39. // 查询某班主任对应的班主任全年计划表
  40. const teaplan = await this.model.findOne({
  41. headteacherid: headteacher.id,
  42. });
  43. if (teaplan) {
  44. // 取得所有不能排班的日期列表
  45. const nodates = teaplan.nodate;
  46. const iswork = await this.teacheriswork(nodates, batch.startdate, batch.enddate);
  47. if (iswork) {
  48. newheadteachers.push(headteacher);
  49. } else {
  50. newheadteachers.push({
  51. ...JSON.parse(JSON.stringify(headteacher)),
  52. disabled: true,
  53. });
  54. }
  55. } else {
  56. newheadteachers.push(headteacher);
  57. }
  58. }
  59. return newheadteachers;
  60. }
  61. // 判断当前日期是否在两个日期之间
  62. async betweendate(curdate, startdate, enddate) {
  63. // 比较开始日期,如果小于0 为不在此区间
  64. // 比较结束日期 如果大于0 为不在此区间
  65. const sres = moment(curdate).diff(moment(startdate), 'days');
  66. const eres = moment(curdate).diff(moment(enddate), 'days');
  67. let result = true;
  68. if (sres < 0 || eres > 0) {
  69. result = false;
  70. }
  71. return result;
  72. }
  73. // 判断班主任不能带班日期是否在批次日期里
  74. async teacheriswork(nodates, startdate, enddate) {
  75. let result = true;
  76. for (const nodate of nodates) {
  77. const res = await this.betweendate(nodate, startdate, enddate);
  78. if (res) {
  79. result = false;
  80. break;
  81. }
  82. }
  83. return result;
  84. }
  85. async divide({ trainplanid }) {
  86. // 根据全年计划表id查出对应的全年计划详细信息
  87. const trainplan = await this.tmodel.findById(trainplanid);
  88. // 查询本培训计划中班主任全年计划表的全部信息
  89. const teaplanList = await this.model.find({ trainplanid }).lean();
  90. // 查询所有班主任信息
  91. const headteacherList = await this.hmodel.find().lean();
  92. // 将班主任排班次数填入数组中
  93. let teaListAll_ = [];
  94. for (const teac of headteacherList) {
  95. // 计算出每个班主任担任过班主任的次数 bug:这里统计的是全库的,应该是改成 当前培训计划范围内
  96. const teacount = await this.cmodel.count({ headteacherid: teac.id, planid: trainplanid });
  97. // 查出每个班主任上报的日子,也合并在教师数据里
  98. const teaplan = teaplanList.find(f => f.headteacherid === (teac.id || teac._id));
  99. let nodateList = [];
  100. if (teaplan) {
  101. const { nodate = [] } = teaplan;
  102. nodateList = nodate;
  103. }
  104. const newdata = { ...JSON.parse(JSON.stringify(teac)), teacount, nodateList };
  105. teaListAll_.push(newdata);
  106. }
  107. // 现在teaListAll_中有班主任信息及他们在当前培训计划中已经排了多少次班了
  108. // 遍历所有批次信息
  109. // 将全年计划中的批次信息取出放在一个数组中
  110. // 已经安排的时间表,{tea:教师;s:开始时间,e:结束时间,c:班级id}
  111. const arrangeList = [];
  112. for (const term of trainplan.termnum) {
  113. // 获取每个部门
  114. const deptInfo = this.departmentcount(teaListAll_);
  115. for (const batch of term.batchnum) {
  116. // 临时教师数组,为了下面处理用而不影响原来的
  117. // 将班主任按排次数排序
  118. teaListAll_ = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
  119. let tempTeaList = [];
  120. // 过滤出不能在该批次上课的班主任
  121. for (const tea of teaListAll_) {
  122. if (tea.nodateList.length <= 0) {
  123. tempTeaList.push(tea);
  124. continue;
  125. }
  126. // 找所有的请假日期在不在当前的批次开始-结束的范围内
  127. const is_in = tea.nodateList.find(f => moment(f).isBetween(batch.startdate, batch.enddate, null, '[]'));
  128. if (is_in) continue;
  129. // 还得过滤下,已经安排过的数据中,有没有重合的
  130. const teaArrange = arrangeList.find(f => f.tea === (tea.id || tea._id) && !this.isrepeat(batch.startdate, batch.enddate, f.s, f.e));
  131. if (!teaArrange) tempTeaList.push(tea);
  132. }
  133. const cantUseList = [];
  134. // 查出该批次下所有的班级
  135. const classList = await batch.class;
  136. for (const _class of classList) {
  137. // 清除班级已经安排过的班主任
  138. _class.headteacherid = null;
  139. let selectTea;
  140. // 重新排列下
  141. tempTeaList = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
  142. // 循环,直到找到人为止
  143. while (!selectTea) {
  144. const tempSelect = this.findTeacher(tempTeaList, deptInfo, cantUseList);
  145. // 2. 确定该人排完班之后,本期中,他所在的部门还是否有1个人了
  146. // 找选中的班主任的部门
  147. const dept = deptInfo.find(f => f.dept === tempSelect.department);
  148. if (dept.dcount - 1 >= 1) {
  149. // 且再查下这个班主任有没有同时带2个班
  150. const is_in = classList.find(f => f.headteacherid === (tempSelect.id || tempSelect._id));
  151. if (!is_in) selectTea = tempSelect;
  152. } else cantUseList.push(tempSelect.id || tempSelect._id);
  153. // 分不了了.没人了
  154. if (cantUseList.length === tempTeaList.length) break;
  155. }
  156. // 确定人后,修改 教师带班次数 和 重计算本期部门用人百分比
  157. const inListTeaData = teaListAll_.find(f => (f.id || f._id) === (selectTea.id || selectTea._id));
  158. // 没找到教师略过
  159. if (!inListTeaData) continue;
  160. inListTeaData.teacount = inListTeaData.teacount + 1;
  161. const inDeptData = deptInfo.find(f => f.dept === selectTea.department);
  162. inDeptData.notUse = inDeptData.notUse - 1;
  163. const newPer = this.computedPer(inDeptData.notUse, inDeptData.dcount);
  164. inDeptData.per = newPer;
  165. _class.headteacherid = selectTea.id || selectTea._id;
  166. arrangeList.push({ teaName: inListTeaData.name, tea: inListTeaData.id || inListTeaData._id, s: batch.startdate, e: batch.enddate, c: _class.id || _class._id });
  167. }
  168. }
  169. }
  170. // 检查最后结果
  171. // const t1 = _.groupBy(arrangeList, 'tea');
  172. // for (const id in t1) {
  173. // const head = _.head(t1[id]);
  174. // console.log(`${head.teaName}: ${t1[id].length}`);
  175. // }
  176. return await trainplan.save();
  177. }
  178. computedPer(n1, total) {
  179. return _.floor(_.multiply(_.divide(n1, total), 100));
  180. }
  181. /**
  182. * 选择最优教师
  183. * @param {Array} tempTeaList 可以安排的教师列表
  184. * @param {Array} deptInfo 部门情况列表
  185. * @param {Array} cantUseList 分配之后发现不能用的教师
  186. */
  187. findTeacher(tempTeaList, deptInfo, cantUseList) {
  188. // 1. 找教师排班次数最少的.多人并且就查看部门的使用人员的百分比和总人数排名,再多就取第一个
  189. tempTeaList = tempTeaList.filter(f => !cantUseList.includes(f.id || f._id));
  190. const fir = _.head(tempTeaList);
  191. const times = fir.teacount;
  192. const sameTimesTea = tempTeaList.filter(f => f.teacount === times);
  193. // 满足条件的老师
  194. let selectTea;
  195. if (sameTimesTea.length > 0) {
  196. // 同时有多个人次数都为最少的次数,那就看部门:闲人占部门的百分比和闲人的人数降序排列
  197. const deptOrderList = _.orderBy(deptInfo, [ 'per', 'notUse' ], [ 'desc', 'desc' ]);
  198. // 根据排序后的部门,从上往下选个人就行
  199. for (const deptData of deptOrderList) {
  200. selectTea = sameTimesTea.find(f => f.department === deptData.dept);
  201. if (selectTea) break;
  202. }
  203. } else if (sameTimesTea.length === 1) {
  204. // 那就是这个老师了.就这个人满足 排班次数最少的老师
  205. selectTea = _.head(sameTimesTea);
  206. }
  207. return selectTea;
  208. }
  209. // 判断两个时间段是否有重合部分: true 重合;false不重合
  210. isrepeat(startdate1, enddate1, startdate2, enddate2) {
  211. let result = true;
  212. if (moment(enddate1).isSameOrBefore(startdate2)) {
  213. result = false;
  214. }
  215. if (moment(startdate1).isSameOrAfter(enddate2)) {
  216. result = false;
  217. }
  218. return result;
  219. }
  220. // 整理当前批次 每个部门有多少人
  221. // dept 部门id dcount 各部门的总人数 per 未使用人员的百分比; notUse 未使用的人数
  222. departmentcount(teaList) {
  223. const group = _.groupBy(teaList, 'department');
  224. let result = [];
  225. for (const dept in group) {
  226. const obj = { dept, dcount: group[dept].length, notUse: group[dept].length, per: 100 };
  227. result.push(obj);
  228. }
  229. // 按未使用人员的百分比,总数给部门排序
  230. result = _.orderBy(result, [ 'per', 'notUse', 'dcount' ]);
  231. return result;
  232. }
  233. }
  234. module.exports = TeaplanService;