lesson.js 24 KB

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