teaplan.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 teaplanList = await this.model.find({ trainplanid });
  84. // 查询所有班主任信息
  85. const headteacherList = await this.hmodel.find();
  86. // for (const teaplan of teaplanList) {
  87. // // 过滤出班主任信息表中id为此班主任全年计划信息班主任id的数据
  88. // const headteacherInfo = _.filter(headteacherList, item => item.id === teaplan.headteacherid);
  89. // // 如果班主任信息列表中班主任上报了班主任全年计划,将其移除
  90. // if (headteacherInfo.length === 0) {
  91. // _.remove(headteacherList, item => item.id === teaplan.headteacherid);
  92. // }
  93. // }
  94. // 遍历所有批次信息
  95. // 将全年计划中的批次信息取出放在一个数组中
  96. for (const term of trainplan.termnum) {
  97. for (const batch of term.batchnum) {
  98. const teaListAll = _.cloneDeep(JSON.parse(JSON.stringify(headteacherList)));
  99. for (const teaplan of teaplanList) {
  100. if (teaplan.nodate && (teaplan.nodate).length > 0) {
  101. // 遍历班主任全年计划表中的不能上课的日期
  102. for (const nodate of teaplan.nodate) {
  103. // 如果不能上课的日期在该批次的开始时间和结束时间中,将该班主任从班主任列表中删除
  104. if (moment(nodate).isBetween(batch.startdate, batch.enddate, null, '[]')) {
  105. // const headteacher_ = await this.hmodel.findById(teaplan.headteacherid);
  106. // if (headteacher_) {
  107. // teaListAll.push(headteacher_);
  108. // }
  109. _.remove(teaListAll, item => item.id === teaplan.headteacherid);
  110. }
  111. }
  112. }
  113. }
  114. // 将班主任排班次数填入数组中
  115. let teaListAll_ = [];
  116. for (const teac of teaListAll) {
  117. // 计算出每个班主任担任过班主任的次数
  118. const teacount = await this.cmodel.count({ headteacherid: teac.id });
  119. const newdata = { ...JSON.parse(JSON.stringify(teac)), teacount };
  120. teaListAll_.push(newdata);
  121. }
  122. // 将班主任按排次数排序
  123. teaListAll_ = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
  124. // 查出该批次下所有的班级
  125. const classList = await batch.class;
  126. console.log(classList);
  127. for (const _class of classList) {
  128. // 取出所有部门和部门可用人数
  129. const departmentList = await this.departmentcount(teaListAll_);
  130. let index = 0;
  131. for (const _tea of teaListAll_) {
  132. // 过滤出当前教师的部门和部门可用人数
  133. const deptinfo = _.find(departmentList, function(o) { return o.deparmentid === _tea.department; });
  134. if (deptinfo.dcount - index > 1) {
  135. _class.headteacherid = _tea.id;
  136. _.remove(teaListAll_, item => item.id === _tea.id);
  137. _.remove(teaListAll, item => item.id === _tea.id);
  138. index = index + 1;
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. return await trainplan.save();
  146. }
  147. // 判断两个时间段是否有重合部分
  148. async isrepeat(startdate1, enddate1, startdate2, enddate2) {
  149. let result = true;
  150. if (moment(enddate1).isSameOrBefore(startdate2)) {
  151. result = false;
  152. }
  153. if (moment(startdate1).isSameOrAfter(enddate2)) {
  154. result = false;
  155. }
  156. return result;
  157. }
  158. // 查询传递的班主任列表中每个部门的人数以及每个部门的总人数
  159. // teaList 传递的教师列表 dnum 传递的教师列表中各部门的人数 dcount 各部门的总人数
  160. async departmentcount(teaList) {
  161. let departmentList = [];
  162. for (const teaplan of teaList) {
  163. departmentList.push({ deparmentid: teaplan.department });
  164. }
  165. // 将数组去重
  166. departmentList = _.uniqWith(departmentList, _.isEqual);
  167. for (const department of departmentList) {
  168. // 计算出能该批次能上课的班主任中各部门有多少人
  169. const dnum = _.filter(teaList, item => item.department === department.deparmentid).length;
  170. department.dnum = dnum;
  171. // 计算出各部门有多少人
  172. const dcount = await this.hmodel.count({ department: department.deparmentid });
  173. department.dcount = dcount;
  174. }
  175. return departmentList;
  176. }
  177. }
  178. module.exports = TeaplanService;