lesson.js 27 KB

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