teaplan.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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(
  31. ErrorCode.DATA_NOT_EXIST,
  32. '全年计划内期信息不存在'
  33. );
  34. }
  35. const batch = await term.batchnum.id(batchid);
  36. if (!batch) {
  37. throw new BusinessError(
  38. ErrorCode.DATA_NOT_EXIST,
  39. '全年计划内批次信息不存在'
  40. );
  41. }
  42. const newheadteachers = [];
  43. // 遍历班主任信息
  44. for (const headteacher of headteachers) {
  45. // 查询某班主任对应的班主任全年计划表
  46. const teaplan = await this.model.findOne({
  47. headteacherid: headteacher.id,
  48. });
  49. if (teaplan) {
  50. // 取得所有不能排班的日期列表
  51. const nodates = teaplan.nodate;
  52. const iswork = await this.teacheriswork(
  53. nodates,
  54. batch.startdate,
  55. batch.enddate
  56. );
  57. if (iswork) {
  58. newheadteachers.push(headteacher);
  59. } else {
  60. newheadteachers.push({
  61. ...JSON.parse(JSON.stringify(headteacher)),
  62. disabled: true,
  63. });
  64. }
  65. } else {
  66. newheadteachers.push(headteacher);
  67. }
  68. }
  69. return newheadteachers;
  70. }
  71. // 判断当前日期是否在两个日期之间
  72. async betweendate(curdate, startdate, enddate) {
  73. // 比较开始日期,如果小于0 为不在此区间
  74. // 比较结束日期 如果大于0 为不在此区间
  75. const sres = moment(curdate).diff(moment(startdate), 'days');
  76. const eres = moment(curdate).diff(moment(enddate), 'days');
  77. let result = true;
  78. if (sres < 0 || eres > 0) {
  79. result = false;
  80. }
  81. return result;
  82. }
  83. // 判断班主任不能带班日期是否在批次日期里
  84. async teacheriswork(nodates, startdate, enddate) {
  85. let result = true;
  86. for (const nodate of nodates) {
  87. const res = await this.betweendate(nodate, startdate, enddate);
  88. if (res) {
  89. result = false;
  90. break;
  91. }
  92. }
  93. return result;
  94. }
  95. async divide({ trainplanid }) {
  96. const data = [];
  97. // 根据全年计划表id查出对应的全年计划详细信息
  98. const trainplan = await this.tmodel.findById(trainplanid);
  99. // 查询本培训计划中班主任全年计划表的全部信息
  100. const teaplanList = await this.model.find({ trainplanid });
  101. // 查询所有班主任信息
  102. const headteacherList = await this.hmodel.find();
  103. // for (const teaplan of teaplanList) {
  104. // // 过滤出班主任信息表中id为此班主任全年计划信息班主任id的数据
  105. // const headteacherInfo = _.filter(headteacherList, item => item.id === teaplan.headteacherid);
  106. // // 如果班主任信息列表中班主任上报了班主任全年计划,将其移除
  107. // if (headteacherInfo.length === 0) {
  108. // _.remove(headteacherList, item => item.id === teaplan.headteacherid);
  109. // }
  110. // }
  111. // 遍历所有批次信息
  112. // 将全年计划中的批次信息取出放在一个数组中
  113. for (const term of trainplan.termnum) {
  114. for (const batch of term.batchnum) {
  115. const teaListAll = _.cloneDeep(
  116. JSON.parse(JSON.stringify(headteacherList))
  117. );
  118. for (const teaplan of teaplanList) {
  119. if (teaplan.nodate && teaplan.nodate.length > 0) {
  120. // 遍历班主任全年计划表中的不能上课的日期
  121. for (const nodate of teaplan.nodate) {
  122. // 如果不能上课的日期在该批次的开始时间和结束时间中,将该班主任从班主任列表中删除
  123. if (
  124. moment(nodate).isBetween(
  125. batch.startdate,
  126. batch.enddate,
  127. null,
  128. '[]'
  129. )
  130. ) {
  131. // const headteacher_ = await this.hmodel.findById(teaplan.headteacherid);
  132. // if (headteacher_) {
  133. // teaListAll.push(headteacher_);
  134. // }
  135. _.remove(
  136. teaListAll,
  137. item => item.id === teaplan.headteacherid
  138. );
  139. }
  140. }
  141. }
  142. }
  143. // 将班主任排班次数填入数组中
  144. let teaListAll_ = [];
  145. for (const teac of teaListAll) {
  146. // 计算出每个班主任担任过班主任的次数
  147. const teacount = await this.cmodel.count({ headteacherid: teac.id });
  148. const newdata = { ...JSON.parse(JSON.stringify(teac)), teacount };
  149. teaListAll_.push(newdata);
  150. }
  151. // 将班主任按排次数排序
  152. teaListAll_ = _.orderBy(teaListAll_, [ 'teacount' ], [ 'asc' ]);
  153. // 查出该批次下所有的班级
  154. const classList = await batch.class;
  155. console.log(classList);
  156. for (const _class of classList) {
  157. // 取出所有部门和部门可用人数
  158. const departmentList = await this.departmentcount(teaListAll_);
  159. let index = 0;
  160. for (const _tea of teaListAll_) {
  161. // 过滤出当前教师的部门和部门可用人数
  162. const deptinfo = _.find(departmentList, function(o) {
  163. return o.deparmentid === _tea.department;
  164. });
  165. if (deptinfo.dcount - index > 1) {
  166. _class.headteacherid = _tea.id;
  167. _.remove(teaListAll_, item => item.id === _tea.id);
  168. _.remove(teaListAll, item => item.id === _tea.id);
  169. index = index + 1;
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. return await trainplan.save();
  177. }
  178. // 判断两个时间段是否有重合部分
  179. async isrepeat(startdate1, enddate1, startdate2, enddate2) {
  180. let result = true;
  181. if (moment(enddate1).isSameOrBefore(startdate2)) {
  182. result = false;
  183. }
  184. if (moment(startdate1).isSameOrAfter(enddate2)) {
  185. result = false;
  186. }
  187. return result;
  188. }
  189. // 查询传递的班主任列表中每个部门的人数以及每个部门的总人数
  190. // teaList 传递的教师列表 dnum 传递的教师列表中各部门的人数 dcount 各部门的总人数
  191. async departmentcount(teaList) {
  192. let departmentList = [];
  193. for (const teaplan of teaList) {
  194. departmentList.push({ deparmentid: teaplan.department });
  195. }
  196. // 将数组去重
  197. departmentList = _.uniqWith(departmentList, _.isEqual);
  198. for (const department of departmentList) {
  199. // 计算出能该批次能上课的班主任中各部门有多少人
  200. const dnum = _.filter(
  201. teaList,
  202. item => item.department === department.deparmentid
  203. ).length;
  204. department.dnum = dnum;
  205. // 计算出各部门有多少人
  206. const dcount = await this.hmodel.count({
  207. department: department.deparmentid,
  208. });
  209. department.dcount = dcount;
  210. }
  211. return departmentList;
  212. }
  213. async arrangeteacher({ planid }) {
  214. const trainplan = await this.tmodel.findOne({ id: planid });
  215. if (!trainplan) { throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在'); }
  216. // 查找所有教师列表
  217. const teacherList = await this.teamodel.find();
  218. // 查找所有教师上报列表
  219. const teaplanList = await this.model.find();
  220. // 课程
  221. const subjectList = await this.submodel.find();
  222. const termList = _.cloneDeep(trainplan.termnum);
  223. const { termnum } = termList;
  224. if (!termnum) return;
  225. // 整理出课表
  226. const arr = [];
  227. for (const t of termnum) {
  228. const { batchnum, term, _id: termid } = t;
  229. // 班级和课程一一匹配显示在列表上
  230. for (const b of batchnum) {
  231. const { class: classes, lessons, startdate, enddate, _id: batchid } = b;
  232. const claslesList = this.setList(term, termid, batchid, classes, lessons);
  233. arr.push(...claslesList);
  234. }
  235. }
  236. // 安排后的课表
  237. const afterList = [];
  238. // 排课
  239. for (const l of arr) {
  240. const { termid, subid, day: date, teaid } = l;
  241. if (teaid) { afterList.push(l); continue; }
  242. const subject = subjectList.find(f => ObjectId(subid).equals(f._id));
  243. if (subject.need_teacher !== '0') continue;
  244. // 申请该天,该科目的教师,并查出教师的名字,分数;并按分数排序
  245. let applyList = teaplanList.filter(f => f.termid === termid && f.subid === subid && f.date === date);
  246. applyList = applyList.map(i => {
  247. const r = this.teacherList.find(f => ObjectId(i.teacherid).equals(f._id));
  248. if (r) {
  249. const { name: teaname, xsscore: score } = r;
  250. i.teaname = teaname;
  251. i.score = score * 1;
  252. }
  253. return i;
  254. });
  255. applyList = _.orderBy(applyList, [ 'score' ], [ 'desc' ]);
  256. // 依次循环申请的教师列表,往这个课程安排中放教师
  257. for (const atea of applyList) {
  258. // 先查询,该教师,是否在今天有安排
  259. const tr = afterList.find(f => f.teaid === atea.teacherid && f.day === atea.date);
  260. if (tr) continue;
  261. else {
  262. l.teaid = tr.teacherid;
  263. l.teaname = tr.teaname;
  264. break;
  265. }
  266. }
  267. afterList.push(l);
  268. }
  269. // 将afterList还原回正常的termnum
  270. let newTermnum = [];
  271. for (const l of afterList) {
  272. const { termid, batchid, classid, ...info } = l;
  273. const updata = _.pick(info, [ 'day', 'subid', 'subname', 'teaid', 'teaname', 'time' ]);
  274. newTermnum = trainplan.termnum.map(t => {
  275. // 找到期
  276. if (ObjectId(termid).equals(t._id)) {
  277. t.batchnum = t.batchnum.map(b => {
  278. if (ObjectId(batchid).equals(b._id)) {
  279. // 找到批次
  280. b.class = b.class.map(c => {
  281. if (ObjectId(classid).equals(c._id)) {
  282. if (c.lessons) {
  283. // 说明有课程安排,找有没有重复的,没有就推进去,有就更改,subid查
  284. const r = c.lessons.find(f => f.subid === updata.subid);
  285. if (r) {
  286. const rindex = c.lessons.findIndex(
  287. f => f.subid === updata.subid
  288. );
  289. c.lessons[rindex] = updata;
  290. } else {
  291. c.lessons.push(updata);
  292. }
  293. } else {
  294. // 说明没有课程安排,放进去一条保存
  295. c.lessons = [ updata ];
  296. }
  297. }
  298. return c;
  299. });
  300. }
  301. return b;
  302. });
  303. }
  304. return t;
  305. });
  306. }
  307. // 保存至计划
  308. trainplan.termnum = newTermnum;
  309. return await trainplan.save();
  310. }
  311. setList(term, termid, batchid, classes, lessonTemplate) {
  312. const arr = [];
  313. // 班级和课程匹配
  314. for (const cla of classes) {
  315. cla.term = term;
  316. cla.termid = termid;
  317. cla.batchid = batchid;
  318. const { lessons } = cla;
  319. if (!lessons) cla.lessons = lessonTemplate;
  320. cla.lessons.map(i => {
  321. let obj = _.omit(cla, [ 'lessons' ]);
  322. obj = { ...obj, ...i, classid: _.clone(cla._id) };
  323. arr.push(obj);
  324. });
  325. }
  326. return arr;
  327. }
  328. }
  329. module.exports = TeaplanService;