lesson.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. */
  257. async check({ ids }) {
  258. // 1,修改课表状态; TODO 2,拿到所有的班级,获取所有人员;3,然后发送信息
  259. const list = await this.model.find({ _id: { $in: ids } });
  260. const res = await this.model.updateMany({ _id: { $in: ids } }, { status: '1' });
  261. const noticeList = [];
  262. const defaults = {
  263. noticeid: 'system',
  264. type: '4',
  265. };
  266. // 循环课表
  267. for (const l of list) {
  268. const planRes = await this.tmodel.findOne({ termnum: { $elemMatch: { _id: l.termid } } });
  269. const { planyearid, _id: planid } = planRes;
  270. if (!planRes) continue;
  271. // 教师 需要知道 期,批,班 日期 星期 科目 上课地点 班主任 班主任电话
  272. // 先找到这个课表是哪个班的
  273. const { classid } = l;
  274. if (!classid) {
  275. console.error(`不存在班级id为=>${classid}`);
  276. continue;
  277. }
  278. const classInfo = await this.ctx.service.class.fetch({ id: classid });
  279. if (!classInfo) {
  280. console.error(`没有id为=>${classid} 的班级信息`);
  281. continue;
  282. }
  283. const { termid } = classInfo;
  284. // 班主任信息
  285. let data = await this.getHeadTeacherMsg(classInfo);
  286. // 礼仪教师信息
  287. let lydata = await this.getLyTeacherMsg(classInfo);
  288. // 整理信息
  289. data = { ...data, ...defaults, termid, classid, planyearid, planid };
  290. if (lydata) {
  291. lydata = { ...lydata, ...defaults, termid, classid, planyearid, planid };
  292. noticeList.push(lydata);
  293. }
  294. // const dirIsBefore = moment(moment().format('YYYY-MM-DD')).isSameOrBefore(classInfo.startdate); // isSameOrBefore
  295. // if (dirIsBefore) {
  296. // 班主任,礼仪教师,不限制时间问题,只要未确认,就发送
  297. noticeList.push(data);
  298. // }
  299. const { lessons } = l;
  300. let have_teacherLesson = lessons.filter(f => f.teaid);
  301. have_teacherLesson = JSON.parse(JSON.stringify(have_teacherLesson));
  302. const newArr = [];
  303. for (const l of have_teacherLesson) {
  304. // 整理时间 TODO,不需要合并时间了,先留着,之后真不需要就删掉
  305. const { subid, teaid, date } = l;
  306. // 超过日期了就不发了
  307. const isBefore = moment(moment().format('YYYY-MM-DD')).isSameOrBefore(date);
  308. if (!isBefore) continue;
  309. if (!subid && teaid) continue;
  310. const r = newArr.find(f => f.subid === subid && f.teaid === teaid);
  311. const ri = newArr.findIndex(
  312. f => f.subid === subid && f.teaid === teaid
  313. );
  314. // 如果找到了,就要把这个时间和上一个整合
  315. if (r) {
  316. newArr[ri].timeList.push(l.time);
  317. } else {
  318. let obj = _.cloneDeep(l);
  319. // 没找到,就放进去
  320. const timeList = [ l.time ];
  321. obj = { ...obj, timeList };
  322. newArr.push(obj);
  323. }
  324. }
  325. for (const l of newArr) {
  326. let teamsg = await this.getTeacherMsg(classInfo, l);
  327. teamsg = { ...teamsg, ...defaults, termid, classid, planyearid, planid };
  328. noticeList.push(teamsg);
  329. }
  330. }
  331. await this.toSendMsg(noticeList);
  332. }
  333. // 给班主任发信息
  334. async getHeadTeacherMsg(classInfo) {
  335. // 班主任 需要知道 期,批,班,时间段,星期段,礼仪课教师,用餐地点,拓展训练地点,开班仪式地点,上课地点
  336. const { term, batch, headteacher, name, headteacherid, startdate, enddate } = classInfo;
  337. if (!headteacherid) return;
  338. let msg = `班主任-${headteacher},中心已经安排您为: ${term}期-${name.includes('班') ? name : `${name}班`} 班主任`;
  339. if (startdate && enddate) msg = `${msg} \n 时间为:${startdate}(星期${this.weekList[moment(startdate).day()]}) 至 ${enddate} (星期${this.weekList[moment(enddate).day()]})`;
  340. if (_.get(classInfo, 'kbyslocationid'))msg = `${msg} \n 开班地点为:${_.get(classInfo, 'kbyslocation')}`;
  341. if (_.get(classInfo, 'kzjhlocationid'))msg = `${msg} \n 拓展训练地点为:${_.get(classInfo, 'kzjhlocation')}`;
  342. if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课地点为:${_.get(classInfo, 'jslocation')}`;
  343. if (_.get(classInfo, 'yclocationid'))msg = `${msg} \n 用餐地点为:${_.get(classInfo, 'yclocation')}`;
  344. // 礼仪教师需要查询,然后带上电话
  345. if (_.get(classInfo, 'lyteacherid')) {
  346. msg = `${msg} \n 礼仪课教师为: \n ${_.get(classInfo, 'lyteacher')}`;
  347. const { lyteacherid } = classInfo;
  348. const r = await this.umodel.findOne({ uid: lyteacherid });
  349. if (r) {
  350. const { mobile } = r;
  351. if (mobile) msg = `${msg} 电话:${mobile}`;
  352. }
  353. }
  354. // 查出openid,email
  355. const info = await this.getSendInfo(headteacherid);
  356. const obj = { notifiedid: headteacherid, username: headteacher, content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
  357. if (info) {
  358. const { openid, email } = info;
  359. obj.openid = openid;
  360. obj.email = email;
  361. }
  362. return obj;
  363. }
  364. // 给教师发送信息
  365. async getTeacherMsg(classInfo, lessonInfo) {
  366. if (!classInfo || !lessonInfo) return;
  367. const { term, batch, name, headteacherid } = classInfo;
  368. const { date, subname, timeList } = lessonInfo;
  369. let msg = `教师-${lessonInfo.teaname}您好,中心已经为您安排了 \n ${term}期-${name.includes('班') ? name : `${name}班`}`;
  370. if (date) msg = `${msg} \n 上课日期:${date}(星期${this.weekList[moment(date).day()]})`;
  371. // if (timeList && _.isArray(timeList)) {
  372. // msg = `${msg} \n 上课时间:`;
  373. // for (const time of timeList) {
  374. // msg = `${msg} ${time}`;
  375. // }
  376. // }
  377. if (subname) msg = `${msg} \n 课程:${subname}`;
  378. if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课教室地点为:${_.get(classInfo, 'jslocation')}`;
  379. if (_.get(classInfo, 'headteacherid')) {
  380. msg = `${msg} \n 班主任为: \n ${_.get(classInfo, 'headteacher')}`;
  381. if (headteacherid) {
  382. const r = await this.umodel.findOne({ uid: headteacherid });
  383. if (r) {
  384. const { mobile } = r;
  385. if (mobile) msg = `${msg} 电话:${mobile}`;
  386. }
  387. }
  388. }
  389. const info = await this.getSendInfo(lessonInfo.teaid);
  390. const obj = { notifiedid: _.get(lessonInfo, 'teaid'), username: _.get(lessonInfo, 'teaname'), content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
  391. if (info) {
  392. const { openid, email } = info;
  393. obj.openid = openid;
  394. obj.email = email;
  395. }
  396. return obj;
  397. }
  398. // 给礼仪教师发送信息
  399. async getLyTeacherMsg(classInfo) {
  400. const { term, batch, name, headteacherid, lyteacherid, lyteacher } = classInfo;
  401. if (headteacherid === lyteacherid) return;
  402. let msg = `${lyteacher}您好,中心为您安排了\n ${term}期-${name.includes('班') ? name : `${name}班`}的礼仪课`;
  403. if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课教室地点为:${_.get(classInfo, 'jslocation')}`;
  404. if (_.get(classInfo, 'headteacherid')) {
  405. msg = `${msg} \n 班主任为: \n ${_.get(classInfo, 'headteacher')}`;
  406. if (headteacherid) {
  407. const r = await this.umodel.findOne({ uid: headteacherid });
  408. if (r) {
  409. const { mobile } = r;
  410. if (mobile) msg = `${msg} 电话:${mobile}`;
  411. }
  412. }
  413. }
  414. const info = await this.getSendInfo(lyteacherid);
  415. const obj = { notifiedid: lyteacherid, username: lyteacher, content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
  416. if (info) {
  417. const { openid, email } = info;
  418. obj.openid = openid;
  419. obj.email = email;
  420. }
  421. return obj;
  422. }
  423. // 查找openid和emaiil
  424. async getSendInfo(uid) {
  425. const user = await this.umodel.findOne({ uid });
  426. if (!user) return;
  427. const { type, openid } = user;
  428. let email;
  429. // type =1班主任,type = 3 教师
  430. if (type === '1') {
  431. const info = await this.headteamodel.findOne({ _id: ObjectId(uid) });
  432. if (info) {
  433. const { qq } = info;
  434. if (qq) email = `${qq}@qq.com`;
  435. }
  436. } else if (type === '3') {
  437. const info = await this.teamodel.findOne({ _id: ObjectId(uid) });
  438. if (info) email = info.email;
  439. }
  440. return { openid, email };
  441. }
  442. async toSendMsg(noticeList) {
  443. for (const notice of noticeList) {
  444. // 先找信息notice,然后查看有没有这个人的信息,有就发送,没有就添加,再发送
  445. // 课表通知,1个班的信息放一起,按班级来看
  446. const { planyearid, planid, termid, classid, noticeid, type, ncontent, content, username, notifiedid, email, openid } = notice;
  447. // await this.nmodel.deleteMany({ termid, planid, classid, type });
  448. let nres = await this.nmodel.findOne({ termid, planid, classid, type });
  449. if (openid) {
  450. // 排除重复,没有的填进对应的班级中
  451. if (!nres) {
  452. // 组织数据,存起来
  453. const notified = [{ content, username, notifiedid }];
  454. const nobj = { planyearid, planid, termid, classid, noticeid, type, content: ncontent, notified };
  455. await this.nmodel.create(nobj);
  456. nres = await this.nmodel.findOne({ termid, planid, classid, type });
  457. } else {
  458. // 有该班的通知,然后查看是否有这个人,没有,加进去,有这个人,不管
  459. if (nres.notified && _.isArray(nres.notified)) {
  460. const r = nres.notified.find(f => f.notifiedid === notifiedid);
  461. // 没有人,加进去,保存
  462. if (!r) {
  463. nres.notified.push({ content, username, notifiedid });
  464. await nres.save();
  465. } else {
  466. // 有这个人,需要查这个人是否接收到信息,接收到信息,就不需要再次发送了
  467. const { status } = r;
  468. if (status !== '0') continue;
  469. }
  470. }
  471. }
  472. }
  473. // 邮件
  474. if (email) {
  475. const subject = '吉林省高等学校毕业生就业指导中心通知';
  476. this.ctx.service.util.sendMail(email, subject, content);
  477. }
  478. if (openid) {
  479. const tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + notifiedid + '&noticeid=' + nres._id;
  480. // TODO 推送
  481. this.ctx.service.weixin.sendTemplateDesign(
  482. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  483. openid,
  484. '您有一个新的通知,请点击信息,确认您已收到信息!',
  485. '您的安排',
  486. content,
  487. '感谢您的使用',
  488. tourl
  489. );
  490. }
  491. }
  492. }
  493. // 新排课,从计划中拿出来对应的课表
  494. async newArrange({ planid, termid }) {
  495. const lmodelList = await this.lmodel.find();
  496. const trainplan = await this.tmodel.findById(planid);
  497. if (!trainplan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '计划不存在');
  498. let { termnum } = trainplan;
  499. // 指定期的数据
  500. termnum = JSON.parse(JSON.stringify(termnum));
  501. if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该期不存在');
  502. // 该期的课表,没有就添加
  503. const has_lesson = await this.model.find({ termid });
  504. const classids = has_lesson.map(i => ObjectId(i.classid));
  505. const has_classList = await this.clamodel.find({ _id: { $in: classids } }, { name: 1 });
  506. const sterm = termnum.find(f => f._id === termid);
  507. const { batchnum } = sterm;
  508. // 确保batchnum是数组
  509. if (!_.isArray(batchnum)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '该期下批次数据错误');
  510. for (const b of batchnum) {
  511. const { _id: batchid, class: classArrange, startdate, enddate } = b;
  512. const classList = await this.clamodel.find({ planid, termid, batchid });
  513. // 确保list是数组
  514. if (!classList || classList.length === 0) continue;
  515. const clist = JSON.parse(JSON.stringify(classList));
  516. for (const c of clist) {
  517. // 排指定班
  518. const { name, type, ...lessinfo } = await this.partsOfClass(c);
  519. // 查看该班级是否已经有课表
  520. const is_has = has_classList.find(f => f.name === name);
  521. if (is_has) continue;
  522. // 找到指定班级的安排 这里目前只能用name找,这个地方很不稳定,需要找到其他的具有唯一性的判断来执行这个find
  523. const clalr = classArrange.find(f => f.name === name);
  524. // 没找到指定班级的安排
  525. if (!clalr) {
  526. console.error('没有找到指定安排');
  527. continue;
  528. }
  529. const { lessons: ntemplate } = clalr;
  530. // 如果没有模板
  531. if (!ntemplate) {
  532. console.error('没有找到指定班级安排模板');
  533. continue;
  534. }
  535. // 根据班级类型找到原课表模板
  536. let lessonModel = lmodelList.find(f => f.type === type);
  537. lessonModel = JSON.parse(JSON.stringify(lessonModel));
  538. // 原模板
  539. let { lessons: otemplate } = lessonModel;
  540. otemplate = JSON.parse(otemplate);
  541. // 日期列表,感谢裕哥能给我留个可用的东西
  542. const dayList = await this.getAllDays(startdate, enddate);
  543. const lessons = [];
  544. for (let i = 0; i < dayList.length; i++) {
  545. for (const ot of otemplate) {
  546. const date = dayList[i];
  547. const { time } = ot;
  548. const keys = Object.keys(ot).filter(f => f.includes(`day${i + 1}`));
  549. const kvs = {};
  550. for (const key of keys) {
  551. // 将原课表的每日,每个时间段的安排整理成object
  552. if (key.startsWith('day') && key.endsWith('type')) {
  553. // kvs.type = ot[key];
  554. } else if (key.startsWith('day') && key.endsWith('subid')) {
  555. kvs.subid = ot[key];
  556. } else {
  557. kvs.subname = ot[key];
  558. }
  559. }
  560. // 整理完的object,如果有subid,就是课程,找教师
  561. const tsubid = _.get(kvs, 'subid');
  562. if (tsubid) {
  563. const r = ntemplate.find(f => f.subid === tsubid);
  564. if (r) {
  565. const { teaid, teaname } = r;
  566. if (teaid && teaname) {
  567. kvs.teaid = teaid;
  568. kvs.teaname = teaname;
  569. }
  570. }
  571. }
  572. const obj = { date, time, ...kvs };
  573. lessons.push(obj);
  574. }
  575. }
  576. // 最后整合
  577. const classLesson = { ...lessinfo, lessons };
  578. // 保存
  579. await this.model.create(classLesson);
  580. }
  581. }
  582. }
  583. // 新排课,将班级层面处理抽出来
  584. async partsOfClass(classinfo) {
  585. classinfo = JSON.parse(JSON.stringify(classinfo));
  586. const { termid, batchid, _id: classid, name, type } = classinfo;
  587. const obj = { termid, batchid, classid, name, type };
  588. return obj;
  589. }
  590. }
  591. module.exports = LessonService;