lesson.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. 'use strict';
  2. const assert = require('assert');
  3. const { groupEnd } = require('console');
  4. const _ = require('lodash');
  5. const { ObjectId } = require('mongoose').Types;
  6. const moment = require('moment');
  7. const { CrudService } = require('naf-framework-mongoose/lib/service');
  8. const { BusinessError, ErrorCode } = require('naf-core').Error;
  9. class LessonService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'lesson');
  12. this.model = this.ctx.model.Lesson;
  13. this.tmodel = this.ctx.model.Trainplan;
  14. this.clamodel = this.ctx.model.Class;
  15. this.lmodel = this.ctx.model.Lessonmode;
  16. this.teamodel = this.ctx.model.Teacher;
  17. this.stumodel = this.ctx.model.Student;
  18. this.schmodel = this.ctx.model.School;
  19. this.headteamodel = this.ctx.model.Headteacher;
  20. this.umodel = this.ctx.model.User;
  21. this.nmodel = this.ctx.model.Notice;
  22. this.weekList = [ '日', '一', '二', '三', '四', '五', '六' ];
  23. }
  24. // 自动排课私有方法
  25. async autolesson({ id }) {
  26. // 首先将课程表清空
  27. const res = await this.tmodel.findById(id);
  28. if (!res) {
  29. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  30. }
  31. const terms = res.termnum;
  32. const _lessonmode = await this.lmodel.find();
  33. // 循环取得所有期
  34. for (const elm of terms) {
  35. // 根据期id清空课程表
  36. await this.model.deleteMany({ termid: elm.id });
  37. // 清空成功后,循环取得当前期下所有批次信息
  38. const batchs = elm.batchnum;
  39. for (const batch of batchs) {
  40. // 取得当前批次开始结束日期,并根据日期取得所有天数
  41. const sedays = await this.getAllDays(batch.startdate, batch.enddate);
  42. // 根据批次取得当前批次下所有班级
  43. const _classs = await this.clamodel.find({
  44. planid: id,
  45. termid: elm.id,
  46. batchid: batch.id,
  47. });
  48. // 循环班级
  49. const teachids = [];
  50. for (const cla of _classs) {
  51. // 取得课程模板信息
  52. let lessonmode_ = _.find(_lessonmode, { type: cla.type });
  53. if (!lessonmode_) {
  54. lessonmode_ = _lessonmode[0];
  55. if (!lessonmode_) {
  56. throw new BusinessError(
  57. ErrorCode.DATA_NOT_EXIST,
  58. '课程模板信息不存在'
  59. );
  60. }
  61. }
  62. // 取得模板内容并转化成json
  63. const lessons_ = JSON.parse(lessonmode_.lessons);
  64. // 记录天数
  65. let i = 1;
  66. // 循环天数
  67. const newlesson = [];
  68. for (const day of sedays) {
  69. // 循环课程模板,将模板信息排入班级课程表中
  70. for (const lessm of lessons_) {
  71. // 循环插入模板信息
  72. if (lessm['day' + i] !== '--') {
  73. let _subid = '';
  74. if (lessm['day' + i + 'type'] === '课程') {
  75. _subid = lessm['day' + i + 'subid'];
  76. } else {
  77. _subid = '';
  78. }
  79. let allday = 0;
  80. if (i === 6) {
  81. // 判断是否有外市的学生有的时候 将其设置为半天
  82. const ishalfday = await this.ishalfday(cla.id);
  83. if (ishalfday) {
  84. allday = 1;
  85. }
  86. }
  87. const data = {
  88. subid: _subid,
  89. subname: lessm['day' + i],
  90. date: day,
  91. time: lessm.time,
  92. day: allday,
  93. };
  94. // 将教师按照分数的综合成绩排序,上报时间,安排教师.
  95. const teacher_ = await this.autoteacher(_subid, teachids);
  96. if (teacher_) {
  97. data.teaid = teacher_.id;
  98. data.teaname = teacher_.name;
  99. teachids.push(teacher_.id);
  100. }
  101. newlesson.push(data);
  102. }
  103. }
  104. i = i + 1;
  105. }
  106. const newdata = {
  107. termid: elm.id,
  108. batchid: batch.id,
  109. classid: cla.id,
  110. lessons: newlesson,
  111. };
  112. // 将课程信息填入课程表
  113. await this.model.create(newdata);
  114. }
  115. }
  116. }
  117. }
  118. // 自动排教师,按照分数的综合成绩排序,上报时间,安排教师
  119. async autoteacher(subid, teachids) {
  120. // 按照上报时间取得所有老师,进行正序排列
  121. const teachers = await this.teamodel
  122. .find({ subid, status: '4' })
  123. .sort({ zlscore: '-1', msscore: '-1', xsscore: '-1' });
  124. for (const teaid of teachids) {
  125. _.remove(teachers, item => item.id === teaid);
  126. }
  127. let teacher = {};
  128. if (teachers.length > 0) {
  129. teacher = teachers[0];
  130. }
  131. return teacher;
  132. }
  133. // 判断是否为半天
  134. async ishalfday(classid) {
  135. // 通过班级id取得所有学生
  136. const students = await this.stumodel.find({ classid });
  137. let res = false;
  138. for (const stu of students) {
  139. const sch = await this.schmodel.findOne({ code: stu.schid });
  140. if (sch && sch.hascar === '0') {
  141. res = true;
  142. break;
  143. }
  144. }
  145. return res;
  146. }
  147. // 取得日期间所有日期
  148. async getAllDays(begin_date, end_date) {
  149. const errArr = [],
  150. resultArr = [],
  151. dateReg = /^[2]\d{3}-[01]\d-[0123]\d$/;
  152. if (
  153. typeof begin_date !== 'string' ||
  154. begin_date === '' ||
  155. !dateReg.test(begin_date)
  156. ) {
  157. return errArr;
  158. }
  159. if (
  160. typeof end_date !== 'string' ||
  161. end_date === '' ||
  162. !dateReg.test(end_date)
  163. ) {
  164. return errArr;
  165. }
  166. try {
  167. const beginTimestamp = Date.parse(new Date(begin_date)),
  168. endTimestamp = Date.parse(new Date(end_date));
  169. // 开始日期小于结束日期
  170. if (beginTimestamp > endTimestamp) {
  171. return errArr;
  172. }
  173. // 开始日期等于结束日期
  174. if (beginTimestamp === endTimestamp) {
  175. resultArr.push(begin_date);
  176. return resultArr;
  177. }
  178. let tempTimestamp = beginTimestamp,
  179. tempDate = begin_date;
  180. // 新增日期是否和结束日期相等, 相等跳出循环
  181. while (tempTimestamp !== endTimestamp) {
  182. resultArr.push(tempDate);
  183. // 增加一天
  184. tempDate = moment(tempTimestamp).add(1, 'd').format('YYYY-MM-DD');
  185. // 将增加时间变为时间戳
  186. tempTimestamp = Date.parse(new Date(tempDate));
  187. }
  188. // 将最后一天放入数组
  189. resultArr.push(end_date);
  190. return resultArr;
  191. } catch (err) {
  192. return errArr;
  193. }
  194. }
  195. // 根据计划id、教师id查询所有班级信息
  196. async classbyteaid({ planid, teaid }) {
  197. // 取得传入的计划id与教师id
  198. // 根据计划id取得所有期
  199. const plan = await this.tmodel.findById(planid);
  200. if (!plan) {
  201. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  202. }
  203. const terms = await plan.termnum;
  204. // 循环取得所有期信息
  205. const data = [];
  206. for (const term of terms) {
  207. // 根据期id与教师id查出课程班级信息
  208. const lessons = await this.model.find({
  209. termid: term.id,
  210. 'lessons.teaid': teaid,
  211. });
  212. const batchs = await term.batchnum;
  213. for (const elm of lessons) {
  214. const newdata = {};
  215. const batch = await batchs.id(elm.batchid);
  216. newdata.planid = planid;
  217. newdata.title = plan.title;
  218. newdata.termid = elm.termid;
  219. newdata.term = term.term;
  220. newdata.batchid = elm.batchid;
  221. newdata.batch = batch.batch;
  222. newdata.classid = elm.classid;
  223. if (elm.classid) {
  224. const cla = await this.clamodel.findById(elm.classid);
  225. if (cla) {
  226. newdata.classname = cla.name;
  227. }
  228. }
  229. const _lessons = elm.lessons.filter(item => item.teaid === teaid);
  230. newdata.lessons = _lessons;
  231. data.push(newdata);
  232. }
  233. }
  234. return data;
  235. }
  236. // 根据计划id、教师id查询所有班级信息
  237. async teaclass({ termid, teaid }) {
  238. // 取得传入的计划id与教师id
  239. // 根据计划id取得所有期
  240. const lessons = await this.model.find({
  241. termid,
  242. 'lessons.teaid': teaid,
  243. });
  244. const classids = lessons.map(i => ObjectId(i.classid));
  245. const data = await this.ctx.model.Class.find({ _id: { $in: classids } });
  246. return data;
  247. }
  248. async uplessones(data) {
  249. for (const _data of data) {
  250. await this.model.findByIdAndUpdate(_data.id, _data);
  251. }
  252. }
  253. /**
  254. * 修改课表的状态,并发送信息
  255. * @param {Array} ids 要修改的课表
  256. * @property {Array} classids 班级表ids
  257. */
  258. async check({ ids, classids }) {
  259. // 1,修改课表状态; TODO 2,拿到所有的班级,获取所有人员;3,然后发送信息
  260. const list = await this.model.find({ _id: { $in: ids }, classid: classids });
  261. const res = await this.model.updateMany({ _id: { $in: ids }, classid: classids }, { status: '1' });
  262. const noticeList = [];
  263. const defaults = {
  264. noticeid: 'system',
  265. type: '4',
  266. };
  267. // 循环课表
  268. for (const l of list) {
  269. const planRes = await this.tmodel.findOne({ termnum: { $elemMatch: { _id: l.termid } } });
  270. const { planyearid, _id: planid } = planRes;
  271. if (!planRes) continue;
  272. // 教师 需要知道 期,批,班 日期 星期 科目 上课地点 班主任 班主任电话
  273. // 先找到这个课表是哪个班的
  274. const { classid } = l;
  275. if (!classid) {
  276. console.error(`不存在班级id为=>${classid}`);
  277. continue;
  278. }
  279. const classInfo = await this.ctx.service.class.fetch({ id: classid });
  280. if (!classInfo) {
  281. console.error(`没有id为=>${classid} 的班级信息`);
  282. continue;
  283. }
  284. const { termid } = classInfo;
  285. // 班主任信息
  286. let data = await this.getHeadTeacherMsg(classInfo);
  287. // 礼仪教师信息
  288. let lydata = await this.getLyTeacherMsg(classInfo);
  289. // 整理信息
  290. data = { ...data, ...defaults, termid, classid, planyearid, planid };
  291. if (lydata) {
  292. lydata = { ...lydata, ...defaults, termid, classid, planyearid, planid };
  293. noticeList.push(lydata);
  294. }
  295. // const dirIsBefore = moment(moment().format('YYYY-MM-DD')).isSameOrBefore(classInfo.startdate); // isSameOrBefore
  296. // if (dirIsBefore) {
  297. // 班主任,礼仪教师,不限制时间问题,只要未确认,就发送
  298. noticeList.push(data);
  299. // }
  300. const { lessons } = l;
  301. let have_teacherLesson = lessons.filter(f => f.teaid);
  302. have_teacherLesson = JSON.parse(JSON.stringify(have_teacherLesson));
  303. const newArr = [];
  304. for (const l of have_teacherLesson) {
  305. // 整理时间 TODO,不需要合并时间了,先留着,之后真不需要就删掉
  306. const { subid, teaid, date } = l;
  307. // 超过日期了就不发了
  308. const isBefore = moment(moment().format('YYYY-MM-DD')).isSameOrBefore(date);
  309. if (!isBefore) continue;
  310. if (!subid && teaid) continue;
  311. const r = newArr.find(f => f.subid === subid && f.teaid === teaid);
  312. const ri = newArr.findIndex(
  313. f => f.subid === subid && f.teaid === teaid
  314. );
  315. // 如果找到了,就要把这个时间和上一个整合
  316. if (r) {
  317. newArr[ri].timeList.push(l.time);
  318. } else {
  319. let obj = _.cloneDeep(l);
  320. // 没找到,就放进去
  321. const timeList = [ l.time ];
  322. obj = { ...obj, timeList };
  323. newArr.push(obj);
  324. }
  325. }
  326. for (const l of newArr) {
  327. let teamsg = await this.getTeacherMsg(classInfo, l);
  328. teamsg = { ...teamsg, ...defaults, termid, classid, planyearid, planid };
  329. noticeList.push(teamsg);
  330. }
  331. }
  332. await this.toSendMsg(noticeList);
  333. }
  334. // 给班主任发信息
  335. async getHeadTeacherMsg(classInfo) {
  336. // 班主任 需要知道 期,批,班,时间段,星期段,礼仪课教师,用餐地点,拓展训练地点,开班仪式地点,上课地点
  337. const { term, batch, headteacher, name, headteacherid, startdate, enddate } = classInfo;
  338. if (!headteacherid) return;
  339. let msg = `班主任-${headteacher},中心已经安排您为: ${term}期-${name.includes('班') ? name : `${name}班`} 班主任`;
  340. if (startdate && enddate) msg = `${msg} \n 时间为:${startdate}(星期${this.weekList[moment(startdate).day()]}) 至 ${enddate} (星期${this.weekList[moment(enddate).day()]})`;
  341. if (_.get(classInfo, 'kbyslocationid'))msg = `${msg} \n 开班地点为:${_.get(classInfo, 'kbyslocation')}`;
  342. if (_.get(classInfo, 'kzjhlocationid'))msg = `${msg} \n 拓展训练地点为:${_.get(classInfo, 'kzjhlocation')}`;
  343. if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课地点为:${_.get(classInfo, 'jslocation')}`;
  344. if (_.get(classInfo, 'yclocationid'))msg = `${msg} \n 用餐地点为:${_.get(classInfo, 'yclocation')}`;
  345. // 礼仪教师需要查询,然后带上电话
  346. if (_.get(classInfo, 'lyteacherid')) {
  347. msg = `${msg} \n 礼仪课教师为: \n ${_.get(classInfo, 'lyteacher')}`;
  348. const { lyteacherid } = classInfo;
  349. const r = await this.umodel.findOne({ uid: lyteacherid });
  350. if (r) {
  351. const { mobile } = r;
  352. if (mobile) msg = `${msg} 电话:${mobile}`;
  353. }
  354. }
  355. // 查出openid,email
  356. const info = await this.getSendInfo(headteacherid);
  357. const obj = { notifiedid: headteacherid, username: headteacher, content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
  358. if (info) {
  359. const { openid, email } = info;
  360. obj.openid = openid;
  361. obj.email = email;
  362. }
  363. return obj;
  364. }
  365. // 给教师发送信息
  366. async getTeacherMsg(classInfo, lessonInfo) {
  367. if (!classInfo || !lessonInfo) return;
  368. const { term, batch, name, headteacherid } = classInfo;
  369. const { date, subname, timeList } = lessonInfo;
  370. let msg = `教师-${lessonInfo.teaname}您好,中心已经为您安排了 \n ${term}期-${name.includes('班') ? name : `${name}班`}`;
  371. if (date) msg = `${msg} \n 上课日期:${date}(星期${this.weekList[moment(date).day()]})`;
  372. // if (timeList && _.isArray(timeList)) {
  373. // msg = `${msg} \n 上课时间:`;
  374. // for (const time of timeList) {
  375. // msg = `${msg} ${time}`;
  376. // }
  377. // }
  378. if (subname) msg = `${msg} \n 课程:${subname}`;
  379. if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课教室地点为:${_.get(classInfo, 'jslocation')}`;
  380. if (_.get(classInfo, 'headteacherid')) {
  381. msg = `${msg} \n 班主任为: \n ${_.get(classInfo, 'headteacher')}`;
  382. if (headteacherid) {
  383. const r = await this.umodel.findOne({ uid: headteacherid });
  384. if (r) {
  385. const { mobile } = r;
  386. if (mobile) msg = `${msg} 电话:${mobile}`;
  387. }
  388. }
  389. }
  390. const info = await this.getSendInfo(lessonInfo.teaid);
  391. const obj = { notifiedid: _.get(lessonInfo, 'teaid'), username: _.get(lessonInfo, 'teaname'), content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
  392. if (info) {
  393. const { openid, email } = info;
  394. obj.openid = openid;
  395. obj.email = email;
  396. }
  397. return obj;
  398. }
  399. // 给礼仪教师发送信息
  400. async getLyTeacherMsg(classInfo) {
  401. const { term, batch, name, headteacherid, lyteacherid, lyteacher } = classInfo;
  402. if (headteacherid === lyteacherid) return;
  403. let msg = `${lyteacher}您好,中心为您安排了\n ${term}期-${name.includes('班') ? name : `${name}班`}的礼仪课`;
  404. if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课教室地点为:${_.get(classInfo, 'jslocation')}`;
  405. if (_.get(classInfo, 'headteacherid')) {
  406. msg = `${msg} \n 班主任为: \n ${_.get(classInfo, 'headteacher')}`;
  407. if (headteacherid) {
  408. const r = await this.umodel.findOne({ uid: headteacherid });
  409. if (r) {
  410. const { mobile } = r;
  411. if (mobile) msg = `${msg} 电话:${mobile}`;
  412. }
  413. }
  414. }
  415. const info = await this.getSendInfo(lyteacherid);
  416. const obj = { notifiedid: lyteacherid, username: lyteacher, content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
  417. if (info) {
  418. const { openid, email } = info;
  419. obj.openid = openid;
  420. obj.email = email;
  421. }
  422. return obj;
  423. }
  424. // 查找openid和emaiil
  425. async getSendInfo(uid) {
  426. const user = await this.umodel.findOne({ uid });
  427. if (!user) return;
  428. const { type, openid } = user;
  429. let email;
  430. // type =1班主任,type = 3 教师
  431. if (type === '1') {
  432. const info = await this.headteamodel.findOne({ _id: ObjectId(uid) });
  433. if (info) {
  434. const { qq } = info;
  435. if (qq) email = `${qq}@qq.com`;
  436. }
  437. } else if (type === '3') {
  438. const info = await this.teamodel.findOne({ _id: ObjectId(uid) });
  439. if (info) email = info.email;
  440. }
  441. return { openid, email };
  442. }
  443. async toSendMsg(noticeList) {
  444. for (const notice of noticeList) {
  445. // 先找信息notice,然后查看有没有这个人的信息,有就发送,没有就添加,再发送
  446. // 课表通知,1个班的信息放一起,按班级来看
  447. const { planyearid, planid, termid, classid, noticeid, type, ncontent, content, username, notifiedid, email, openid } = notice;
  448. // await this.nmodel.deleteMany({ termid, planid, classid, type });
  449. let nres = await this.nmodel.findOne({ termid, planid, classid, type });
  450. if (openid) {
  451. // 排除重复,没有的填进对应的班级中
  452. if (!nres) {
  453. // 组织数据,存起来
  454. const notified = [{ content, username, notifiedid }];
  455. const nobj = { planyearid, planid, termid, classid, noticeid, type, content: ncontent, notified };
  456. await this.nmodel.create(nobj);
  457. nres = await this.nmodel.findOne({ termid, planid, classid, type });
  458. } else {
  459. // 有该班的通知,然后查看是否有这个人,没有,加进去,有这个人,不管
  460. if (nres.notified && _.isArray(nres.notified)) {
  461. const r = nres.notified.find(f => f.notifiedid === notifiedid);
  462. // 没有人,加进去,保存
  463. if (!r) {
  464. nres.notified.push({ content, username, notifiedid });
  465. await nres.save();
  466. } else {
  467. // 有这个人,需要查这个人是否接收到信息,接收到信息,就不需要再次发送了
  468. const { status } = r;
  469. if (status !== '0') continue;
  470. }
  471. }
  472. }
  473. }
  474. // 邮件
  475. if (email) {
  476. const subject = '吉林省高等学校毕业生就业指导中心通知';
  477. this.ctx.service.util.sendMail(email, subject, content);
  478. }
  479. if (openid) {
  480. const tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + notifiedid + '&noticeid=' + nres._id;
  481. // TODO 推送
  482. this.ctx.service.weixin.sendTemplateDesign(
  483. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  484. openid,
  485. '您有一个新的通知,请点击信息,确认您已收到信息!',
  486. '您的安排',
  487. content,
  488. '感谢您的使用',
  489. tourl
  490. );
  491. }
  492. }
  493. }
  494. // 新排课,从计划中拿出来对应的课表
  495. async newArrange({ planid, termid }) {
  496. const lmodelList = await this.lmodel.find();
  497. const trainplan = await this.tmodel.findById(planid);
  498. if (!trainplan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '计划不存在');
  499. let { termnum } = trainplan;
  500. // 指定期的数据
  501. termnum = JSON.parse(JSON.stringify(termnum));
  502. if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该期不存在');
  503. // 该期的课表,没有就添加
  504. const has_lesson = await this.model.find({ termid });
  505. const classids = has_lesson.map(i => ObjectId(i.classid));
  506. const has_classList = await this.clamodel.find({ _id: { $in: classids } }, { name: 1 });
  507. const sterm = termnum.find(f => f._id === termid);
  508. const { batchnum } = sterm;
  509. // 确保batchnum是数组
  510. if (!_.isArray(batchnum)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该期下批次数据错误');
  511. for (const b of batchnum) {
  512. const { _id: batchid, class: classArrange, startdate, enddate } = b;
  513. const classList = await this.clamodel.find({ planid, termid, batchid });
  514. // 确保list是数组
  515. if (!classList || classList.length === 0) continue;
  516. const clist = JSON.parse(JSON.stringify(classList));
  517. for (const c of clist) {
  518. // 排指定班
  519. const { name, type, ...lessinfo } = await this.partsOfClass(c);
  520. // 查看该班级是否已经有课表
  521. const is_has = has_classList.find(f => f.name === name);
  522. if (is_has) continue;
  523. // 找到指定班级的安排 这里目前只能用name找,这个地方很不稳定,需要找到其他的具有唯一性的判断来执行这个find
  524. const clalr = classArrange.find(f => f.name === name);
  525. // 没找到指定班级的安排
  526. if (!clalr) {
  527. console.error('没有找到指定安排');
  528. continue;
  529. }
  530. const { lessons: ntemplate } = clalr;
  531. // 如果没有模板
  532. if (!ntemplate) {
  533. console.error('没有找到指定班级安排模板');
  534. continue;
  535. }
  536. // 根据班级类型找到原课表模板
  537. let lessonModel = lmodelList.find(f => f.type === type);
  538. lessonModel = JSON.parse(JSON.stringify(lessonModel));
  539. // 原模板
  540. let { lessons: otemplate } = lessonModel;
  541. otemplate = JSON.parse(otemplate);
  542. // 日期列表,感谢裕哥能给我留个可用的东西
  543. const dayList = await this.getAllDays(startdate, enddate);
  544. const lessons = [];
  545. for (let i = 0; i < dayList.length; i++) {
  546. for (const ot of otemplate) {
  547. const date = dayList[i];
  548. const { time } = ot;
  549. const keys = Object.keys(ot).filter(f => f.includes(`day${i + 1}`));
  550. const kvs = {};
  551. for (const key of keys) {
  552. // 将原课表的每日,每个时间段的安排整理成object
  553. if (key.startsWith('day') && key.endsWith('type')) {
  554. // kvs.type = ot[key];
  555. } else if (key.startsWith('day') && key.endsWith('subid')) {
  556. kvs.subid = ot[key];
  557. } else {
  558. kvs.subname = ot[key];
  559. }
  560. }
  561. // 整理完的object,如果有subid,就是课程,找教师
  562. const tsubid = _.get(kvs, 'subid');
  563. if (tsubid) {
  564. const r = ntemplate.find(f => f.subid === tsubid);
  565. if (r) {
  566. const { teaid, teaname } = r;
  567. if (teaid && teaname) {
  568. kvs.teaid = teaid;
  569. kvs.teaname = teaname;
  570. }
  571. }
  572. }
  573. const obj = { date, time, ...kvs, day: '0' };
  574. lessons.push(obj);
  575. }
  576. }
  577. // 最后整合
  578. const classLesson = { ...lessinfo, lessons };
  579. // 保存
  580. await this.model.create(classLesson);
  581. }
  582. }
  583. }
  584. // 新排课,将班级层面处理抽出来
  585. async partsOfClass(classinfo) {
  586. classinfo = JSON.parse(JSON.stringify(classinfo));
  587. const { termid, batchid, _id: classid, name, type } = classinfo;
  588. const obj = { termid, batchid, classid, name, type };
  589. return obj;
  590. }
  591. async teaIndex({ planid, teaid }) {
  592. const trainplan = await this.tmodel.findById(planid);
  593. if (!trainplan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定年度计划');
  594. const { termnum } = trainplan;
  595. const termids = termnum.map(i => i._id);
  596. let lessons = await this.model.find({ termid: termids, status: '1', lessons: { $elemMatch: { teaid } } });
  597. if (lessons.length > 0)lessons = JSON.parse(JSON.stringify(lessons));
  598. else return [];
  599. const classids = lessons.map(i => i.classid);
  600. // 找期,班级信息
  601. const classes = await this.clamodel.find({ _id: classids });
  602. let res = [];
  603. for (const i of lessons) {
  604. const { lessons: ls, classid, termid } = i;
  605. let term,
  606. cla;
  607. const list = ls.filter(f => f.teaid === teaid);
  608. const t = termnum.find(f => ObjectId(f._id).equals(termid));
  609. if (t) term = t.term;
  610. const c = classes.find(f => ObjectId(f._id).equals(classid));
  611. if (c) cla = c.name;
  612. for (const l of list) {
  613. const { subname, date } = l;
  614. if (cla && subname && date && term) { res.push({ subname, date, term, class: cla }); }
  615. }
  616. }
  617. res = _.uniqWith(res, _.isEqual);
  618. return res;
  619. }
  620. }
  621. module.exports = LessonService;