teaplan.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. }
  17. // 查询所有班主任带是否能带班标记
  18. async findteacher({ planid, termid, batchid }) {
  19. // 查询所有班主任信息
  20. const headteachers = await this.hmodel.find();
  21. // 根据批次id取得当前批次具体信息
  22. const trainplan = await this.tmodel.findById(planid);
  23. if (!trainplan) {
  24. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  25. }
  26. const term = await trainplan.termnum.id(termid);
  27. if (!term) {
  28. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内期信息不存在');
  29. }
  30. const batch = await term.batchnum.id(batchid);
  31. if (!batch) {
  32. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划内批次信息不存在');
  33. }
  34. const newheadteachers = [];
  35. // 遍历班主任信息
  36. for (const headteacher of headteachers) {
  37. // 查询某班主任对应的班主任全年计划表
  38. const teaplan = await this.model.findOne({ headteacherid: headteacher.id });
  39. if (teaplan) {
  40. // 取得所有不能排班的日期列表
  41. const nodates = teaplan.nodate;
  42. const iswork = await this.teacheriswork(nodates, batch.startdate, batch.enddate);
  43. if (iswork) {
  44. newheadteachers.push(headteacher);
  45. } else {
  46. newheadteachers.push({ ...JSON.parse(JSON.stringify(headteacher)), disabled: true });
  47. }
  48. } else {
  49. newheadteachers.push(headteacher);
  50. }
  51. }
  52. return newheadteachers;
  53. }
  54. // 判断当前日期是否在两个日期之间
  55. async betweendate(curdate, startdate, enddate) {
  56. // 比较开始日期,如果小于0 为不在此区间
  57. // 比较结束日期 如果大于0 为不在此区间
  58. const sres = moment(curdate).diff(moment(startdate), 'days');
  59. const eres = moment(curdate).diff(moment(enddate), 'days');
  60. let result = true;
  61. if (sres < 0 || eres > 0) {
  62. result = false;
  63. }
  64. return result;
  65. }
  66. // 判断班主任不能带班日期是否在批次日期里
  67. async teacheriswork(nodates, startdate, enddate) {
  68. let result = true;
  69. for (const nodate of nodates) {
  70. const res = await this.betweendate(nodate, startdate, enddate);
  71. if (res) {
  72. result = false;
  73. break;
  74. }
  75. }
  76. return result;
  77. }
  78. async divide({ trainplanid }) {
  79. const data = [];
  80. // 根据全年计划表id查出对应的全年计划详细信息
  81. const trainplan = await this.tmodel.findById(trainplanid);
  82. // 将全年计划中的批次信息取出放在一个数组中
  83. const batchList = [];
  84. for (const term of trainplan.termnum) {
  85. for (const batch of term.batchnum) {
  86. batchList.push(batch);
  87. }
  88. }
  89. // 查询本培训计划中班主任全年计划表的全部信息
  90. const teaplanList = await this.model.find({ trainplanid });
  91. // 查询所有班主任信息
  92. const headteacherList = await this.hmodel.find();
  93. for (const teaplan of teaplanList) {
  94. // 过滤出班主任信息表中id为此班主任全年计划信息班主任id的数据
  95. const headteacherInfo = _.filter(headteacherList, item => item.id === teaplan.headteacherid);
  96. // 如果班主任信息列表中班主任上报了班主任全年计划,将其移除
  97. if (headteacherInfo.length >= 1) {
  98. _.remove(headteacherList, item => item.id === teaplan.headteacherid);
  99. teaplan.department = headteacherInfo[0].department;
  100. }
  101. }
  102. // 遍历班主任信息,将班主任的id放在headteacherid字段中
  103. for (const headteacher of headteacherList) {
  104. headteacher.headteacherid = headteacher.id;
  105. }
  106. const teaList = _.concat(teaplanList, headteacherList);
  107. let noteaList = [];
  108. // 遍历所有批次信息
  109. for (const batch of batchList) {
  110. let _teaList = [];
  111. for (const teaplan of teaList) {
  112. if (teaplan.nodate && (teaplan.nodate).length > 0) {
  113. // 遍历班主任全年计划表中的不能上课的日期
  114. for (const nodate of teaplan.nodate) {
  115. // 如果不能上课的日期在该批次的开始时间和结束时间中,将该班主任从班主任列表中删除
  116. if (moment(nodate).isBetween(batch.startdate, batch.enddate, null, '[]')) {
  117. noteaList.push(teaplan);
  118. }
  119. }
  120. }
  121. // 过滤出已分配的数据中该班主任的数据
  122. const teaInfo = _.filter(data, item => item.headteacherid === teaplan.headteacherid);
  123. // 计算出每个班主任担任过班主任的次数
  124. teaplan.teacount = await this.cmodel.count({ headteacherid: teaplan.headteacherid }) + teaInfo.length;
  125. // 如果已分配的数据中该班主任的数据长度大于0
  126. if (teaInfo.length > 0) {
  127. // 遍历已分配的数据中该班主任的数据
  128. for (const _teaInfo of teaInfo) {
  129. // 查询该班主任已分配的数据中的班级信息
  130. const _class = await this.cmodel.findById(_teaInfo.classid);
  131. // 根据班级的批次id查出该班级的开始时间和结束时间
  132. const batchInfo = _.filter(batchList, item => item.id === _class.batchid);
  133. // 如果该班级的开始时间和结束时间于当前遍历批次的开始时间和结束时间有重合部分,将该班主任加入不能上课的班主任数组中
  134. if (await this.isrepeat(batchInfo.startdate, batchInfo.enddate, batch.startdate, batch.enddate)) {
  135. noteaList.push(teaplan);
  136. }
  137. }
  138. }
  139. // 将数组去重
  140. noteaList = _.uniqWith(noteaList, _.isEqual);
  141. // 将班主任全年计划表过滤,去除在不能上课的班主任数组中已存在的数据
  142. _teaList = _.difference(teaList, noteaList);
  143. // 将不能上课的班主任数组清空
  144. noteaList = [];
  145. const departmentList = await this.departmentcount(_teaList);
  146. for (const department of departmentList) {
  147. // 如果部门中人员的数量减去能部门中上课的班主任数量小于一,那么将能上课的班主任数组中部门为该部门的班主任移除一个
  148. if ((department.dcount - department.dnum) < 1) {
  149. let _noteaList = _.filter(_teaList, item => item.deparment === department.deparmentid);
  150. _noteaList = _.orderBy(_noteaList, [ 'teacount' ], [ 'desc' ]);
  151. _teaList = _.difference(_teaList, _noteaList[0]);
  152. }
  153. }
  154. // 将该批次最终筛选出的能上课的班主任数组按每个班主任担任过班主任的次数倒序排序
  155. _teaList = _.orderBy(_teaList, [ 'teacount' ], [ 'asc' ]);
  156. }
  157. // 查出该批次下所有的班级
  158. const classList = await this.cmodel.find({ batchid: batch.id });
  159. // 循环该批次下所有的班级
  160. let index = 0;
  161. for (const _class of classList) {
  162. if (_teaList.length > 0 && _teaList[index]) {
  163. data.push({ classid: _class.id, headteacherid: _teaList[index].headteacherid, teacount: _teaList[index].teacount });
  164. } else {
  165. data.push({ classid: _class.id, headteacherid: undefined });
  166. }
  167. index = index + 1;
  168. }
  169. }
  170. return data;
  171. }
  172. async adivide({ trainplanid }) {
  173. const data = [];
  174. // 根据全年计划表id查出对应的全年计划详细信息
  175. const trainplan = await this.tmodel.findById(trainplanid);
  176. // 将全年计划中的批次信息取出放在一个数组中
  177. const batchList = [];
  178. for (const term of trainplan.termnum) {
  179. for (const batch of term.batchnum) {
  180. batchList.push(batch);
  181. }
  182. }
  183. // 查询本培训计划中班主任全年计划表的全部信息
  184. const teaplanList = await this.model.find({ trainplanid });
  185. // 查询所有班主任信息
  186. const headteacherList = await this.hmodel.find();
  187. let noteaList = [];
  188. // 遍历所有批次信息
  189. for (const _batch of batchList) {
  190. // 遍历班主任全年计划表
  191. for (const teaplan of teaplanList) {
  192. if ((teaplan.nodate).length > 0) {
  193. // 遍历班主任全年计划表中的不能上课的日期
  194. for (const nodate of teaplan.nodate) {
  195. // 如果不能上课的日期在该批次的开始时间和结束时间中,将该班主任加入不能上课的班主任数组中
  196. if (moment(nodate).isBetween(_batch.startdate, _batch.enddate, null, '[]')) {
  197. noteaList.push(teaplan);
  198. }
  199. }
  200. }
  201. const headteacherInfo = _.filter(headteacherList, item => item.id === teaplan.headteacherid);
  202. // 如果班主任信息列表中班主任上报了班主任全年计划,将其移除
  203. if (headteacherInfo.length >= 1) {
  204. _.remove(headteacherList, item => item.id === teaplan.headteacherid);
  205. }
  206. // 过滤出已分配的数据中该班主任的数据
  207. const teaInfo = _.filter(data, item => item.headteacherid === teaplan.headteacherid);
  208. // 计算出每个班主任担任过班主任的次数
  209. teaplan.teacount = await this.cmodel.count({ headteacherid: teaplan.headteacherid }) + teaInfo.length;
  210. // 如果已分配的数据中该班主任的数据长度大于0
  211. if (teaInfo.length > 0) {
  212. // 遍历已分配的数据中该班主任的数据
  213. for (const _teaInfo of teaInfo) {
  214. // 查询该班主任已分配的数据中的班级信息
  215. const _class = await this.cmodel.findById(_teaInfo.classid);
  216. // 根据班级的批次id查出该班级的开始时间和结束时间
  217. const batchInfo = _.filter(batchList, item => item.id === _class.batchid);
  218. // 如果该班级的开始时间和结束时间于当前遍历批次的开始时间和结束时间有重合部分,将该班主任加入不能上课的班主任数组中
  219. if (await this.isrepeat(batchInfo.startdate, batchInfo.enddate, _batch.startdate, _batch.enddate)) {
  220. noteaList.push(teaplan);
  221. }
  222. }
  223. }
  224. }
  225. for (const headteacher of headteacherList) {
  226. headteacher.headteacherid = headteacher.id;
  227. // 过滤出已分配的数据中该班主任的数据
  228. const teaInfo = _.filter(data, item => item.headteacherid === headteacher.headteacherid);
  229. // 计算出每个班主任担任过班主任的次数
  230. headteacher.teacount = await this.cmodel.count({ headteacherid: headteacher.id }) + teaInfo.length;
  231. // 如果已分配的数据中该班主任的数据长度大于0
  232. if (teaInfo.length > 0) {
  233. // 遍历已分配的数据中该班主任的数据
  234. for (const _teaInfo of teaInfo) {
  235. // 查询该班主任已分配的数据中的班级信息
  236. const _class = await this.cmodel.findById(_teaInfo.classid);
  237. // 根据班级的批次id查出该班级的开始时间和结束时间
  238. const batchInfo = _.filter(batchList, item => item.id === _class.batchid);
  239. // 如果该班级的开始时间和结束时间于当前遍历批次的开始时间和结束时间有重合部分,将该班主任加入不能上课的班主任数组中
  240. if (await this.isrepeat(batchInfo.startdate, batchInfo.enddate, _batch.startdate, _batch.enddate)) {
  241. _.remove(headteacherList, item => item.id === headteacher.id);
  242. }
  243. }
  244. }
  245. }
  246. // 将数组去重
  247. noteaList = _.uniqWith(noteaList, _.isEqual);
  248. // 将班主任全年计划表过滤,去除在不能上课的班主任数组中已存在的数据
  249. let teaList = _.difference(teaplanList, noteaList);
  250. teaList = _.concat(teaList, headteacherList);
  251. // 将不能上课的班主任数组清空
  252. noteaList = [];
  253. const departmentList = await this.departmentcount(teaList);
  254. for (const department of departmentList) {
  255. // 如果部门中人员的数量减去能部门中上课的班主任数量小于一,那么将能上课的班主任数组中部门为该部门的班主任移除一个
  256. if ((department.dcount - department.dnum) < 1) {
  257. let _noteaList = _.filter(teaList, item => item.deparmentid === department.deparmentid);
  258. _noteaList = _.orderBy(_noteaList, [ 'teacount' ], [ 'desc' ]);
  259. teaList = _.difference(teaList, _noteaList[0]);
  260. }
  261. }
  262. // 将该批次最终筛选出的能上课的班主任数组按每个班主任担任过班主任的次数倒序排序
  263. teaList = _.orderBy(teaList, [ 'teacount' ], [ 'asc' ]);
  264. console.log(teaList);
  265. // 查出该批次下所有的班级
  266. const classList = await this.cmodel.find({ batchid: _batch.id });
  267. // 循环该批次下所有的班级
  268. let index = 0;
  269. for (const _class of classList) {
  270. if (teaList[index]) {
  271. data.push({ classid: _class.id, headteacherid: teaList[index].headteacherid });
  272. } else {
  273. data.push({ classid: _class.id, headteacherid: undefined });
  274. }
  275. index = index + 1;
  276. }
  277. }
  278. return data;
  279. }
  280. // 判断两个时间段是否有重合部分
  281. async isrepeat(startdate1, enddate1, startdate2, enddate2) {
  282. let result = true;
  283. if (moment(enddate1).isSameOrBefore(startdate2)) {
  284. result = false;
  285. }
  286. if (moment(startdate1).isSameOrAfter(enddate2)) {
  287. result = false;
  288. }
  289. return result;
  290. }
  291. // 查询传递的班主任列表中每个部门的人数以及每个部门的总人数
  292. // teaList 传递的教师列表 dnum 传递的教师列表中各部门的人数 dcount 各部门的总人数
  293. async departmentcount(teaList) {
  294. let departmentList = [];
  295. for (const teaplan of teaList) {
  296. departmentList.push({ deparmentid: teaplan.department });
  297. }
  298. // 将数组去重
  299. departmentList = _.uniqWith(departmentList, _.isEqual);
  300. for (const department of departmentList) {
  301. // 计算出能该批次能上课的班主任中各部门有多少人
  302. const dnum = _.filter(teaList, item => item.department === department.deparmentid).length;
  303. department.dnum = dnum;
  304. // 计算出各部门有多少人
  305. const dcount = await this.hmodel.count({ department: department.deparmentid });
  306. department.dcount = dcount;
  307. }
  308. return departmentList;
  309. }
  310. }
  311. module.exports = TeaplanService;