lesson.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const moment = require('moment');
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class LessonService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'lesson');
  11. this.model = this.ctx.model.Lesson;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. this.clamodel = this.ctx.model.Class;
  14. this.lmodel = this.ctx.model.Lessonmode;
  15. this.teamodel = this.ctx.model.Teacher;
  16. this.stumodel = this.ctx.model.Student;
  17. this.schmodel = this.ctx.model.School;
  18. }
  19. // 自动排课私有方法
  20. async autolesson({ id }) {
  21. // 首先将课程表清空
  22. const res = await this.tmodel.findById(id);
  23. if (!res) {
  24. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  25. }
  26. const terms = res.termnum;
  27. const _lessonmode = await this.lmodel.find();
  28. // 循环取得所有期
  29. for (const elm of terms) {
  30. // 根据期id清空课程表
  31. await this.model.deleteMany({ termid: elm.id });
  32. // 清空成功后,循环取得当前期下所有批次信息
  33. const batchs = elm.batchnum;
  34. for (const batch of batchs) {
  35. // 取得当前批次开始结束日期,并根据日期取得所有天数
  36. const sedays = await this.getAllDays(batch.startdate, batch.enddate);
  37. // 根据批次取得当前批次下所有班级
  38. const _classs = await this.clamodel.find({
  39. planid: id,
  40. termid: elm.id,
  41. batchid: batch.id,
  42. });
  43. // 循环班级
  44. for (const cla of _classs) {
  45. // 取得课程模板信息
  46. let lessonmode_ = _.find(_lessonmode, { type: cla.type });
  47. if (!lessonmode_) {
  48. lessonmode_ = _lessonmode[0];
  49. if (!lessonmode_) {
  50. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '课程模板信息不存在');
  51. }
  52. }
  53. // 取得模板内容并转化成json
  54. const lessons_ = JSON.parse(lessonmode_.lessons);
  55. // 记录天数
  56. let i = 1;
  57. // 循环天数
  58. const newlesson = [];
  59. for (const day of sedays) {
  60. // 循环课程模板,将模板信息排入班级课程表中
  61. const teachids = [];
  62. for (const lessm of lessons_) {
  63. // 循环插入模板信息
  64. if (lessm['day' + i] !== '--') {
  65. let _subid = '';
  66. if (lessm['day' + i + 'type'] === '课程') {
  67. _subid = lessm['day' + i + 'subid'];
  68. } else {
  69. _subid = '';
  70. }
  71. let allday = 0;
  72. if (i === 6) {
  73. // 判断是否有外市的学生有的时候 将其设置为半天
  74. const ishalfday = await this.ishalfday(cla.id);
  75. if (ishalfday) {
  76. allday = 1;
  77. }
  78. }
  79. const data = {
  80. subid: _subid,
  81. subname: lessm['day' + i],
  82. date: day,
  83. time: lessm.time,
  84. day: allday,
  85. };
  86. // 将教师按照分数的综合成绩排序,上报时间,安排教师.
  87. const teacher_ = await this.autoteacher(_subid, teachids);
  88. if (teacher_) {
  89. data.teaid = teacher_.id;
  90. data.teaname = teacher_.name;
  91. teachids.push(teacher_.id);
  92. }
  93. newlesson.push(data);
  94. }
  95. }
  96. i = i + 1;
  97. }
  98. const newdata = {
  99. termid: elm.id,
  100. batchid: batch.id,
  101. classid: cla.id,
  102. lessons: newlesson,
  103. };
  104. // 将课程信息填入课程表
  105. await this.model.create(newdata);
  106. }
  107. }
  108. }
  109. }
  110. // 自动排教师,按照分数的综合成绩排序,上报时间,安排教师
  111. async autoteacher(subid, teachids) {
  112. // 按照上报时间取得所有老师,进行正序排列
  113. const teachers = await this.teamodel.find({ subid, status: '4' }).sort({ zlscore: '-1', msscore: '-1', xsscore: '-1' });
  114. for (const teaid of teachids) {
  115. _.remove(teachers, item => item.id === teaid);
  116. }
  117. let teacher = {};
  118. if (teachers.length > 0) {
  119. teacher = teachers[0];
  120. }
  121. return teacher;
  122. }
  123. // 判断是否为半天
  124. async ishalfday(classid) {
  125. // 通过班级id取得所有学生
  126. const students = await this.stumodel.find({ classid });
  127. let res = false;
  128. for (const stu of students) {
  129. const sch = await this.schmodel.findOne({ code: stu.schid });
  130. if (sch && sch.hascar === '0') {
  131. res = true;
  132. break;
  133. }
  134. }
  135. return res;
  136. }
  137. // 取得日期间所有日期
  138. async getAllDays(begin_date, end_date) {
  139. const errArr = [],
  140. resultArr = [],
  141. dateReg = /^[2]\d{3}-[01]\d-[0123]\d$/;
  142. if (
  143. typeof begin_date !== 'string' ||
  144. begin_date === '' ||
  145. !dateReg.test(begin_date)
  146. ) {
  147. return errArr;
  148. }
  149. if (
  150. typeof end_date !== 'string' ||
  151. end_date === '' ||
  152. !dateReg.test(end_date)
  153. ) {
  154. return errArr;
  155. }
  156. try {
  157. const beginTimestamp = Date.parse(new Date(begin_date)),
  158. endTimestamp = Date.parse(new Date(end_date));
  159. // 开始日期小于结束日期
  160. if (beginTimestamp > endTimestamp) {
  161. return errArr;
  162. }
  163. // 开始日期等于结束日期
  164. if (beginTimestamp === endTimestamp) {
  165. resultArr.push(begin_date);
  166. return resultArr;
  167. }
  168. let tempTimestamp = beginTimestamp,
  169. tempDate = begin_date;
  170. // 新增日期是否和结束日期相等, 相等跳出循环
  171. while (tempTimestamp !== endTimestamp) {
  172. resultArr.push(tempDate);
  173. // 增加一天
  174. tempDate = moment(tempTimestamp).add(1, 'd').format('YYYY-MM-DD');
  175. // 将增加时间变为时间戳
  176. tempTimestamp = Date.parse(new Date(tempDate));
  177. }
  178. // 将最后一天放入数组
  179. resultArr.push(end_date);
  180. return resultArr;
  181. } catch (err) {
  182. return errArr;
  183. }
  184. }
  185. // 根据计划id、教师id查询所有班级信息
  186. async classbyteaid({ planid, teaid }) {
  187. // 取得传入的计划id与教师id
  188. // 根据计划id取得所有期
  189. const plan = await this.tmodel.findById(planid);
  190. if (!plan) {
  191. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  192. }
  193. const terms = await plan.termnum;
  194. // 循环取得所有期信息
  195. const data = [];
  196. for (const term of terms) {
  197. // 根据期id与教师id查出课程班级信息
  198. const lessons = await this.model.find({
  199. termid: term.id,
  200. 'lessons.teaid': teaid,
  201. });
  202. const batchs = await term.batchnum;
  203. for (const elm of lessons) {
  204. const newdata = {};
  205. const batch = await batchs.id(elm.batchid);
  206. newdata.planid = planid;
  207. newdata.title = plan.title;
  208. newdata.termid = elm.termid;
  209. newdata.term = term.term;
  210. newdata.batchid = elm.batchid;
  211. newdata.batch = batch.batch;
  212. newdata.classid = elm.classid;
  213. if (elm.classid) {
  214. const cla = await this.clamodel.findById(elm.classid);
  215. if (cla) {
  216. newdata.classname = cla.name;
  217. }
  218. }
  219. const _lessons = elm.lessons.filter(item => item.teaid === teaid);
  220. newdata.lessons = _lessons;
  221. data.push(newdata);
  222. }
  223. }
  224. return data;
  225. }
  226. // 根据计划id、教师id查询所有班级信息
  227. async teaclass({ planid, teaid }) {
  228. // 取得传入的计划id与教师id
  229. // 根据计划id取得所有期
  230. const plan = await this.tmodel.findById(planid);
  231. if (!plan) {
  232. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  233. }
  234. const terms = await plan.termnum;
  235. // 循环取得所有期信息
  236. const data = [];
  237. for (const term of terms) {
  238. // 根据期id与教师id查出课程班级信息
  239. const lessons = await this.model.find({
  240. termid: term.id,
  241. 'lessons.teaid': teaid,
  242. });
  243. for (const elm of lessons) {
  244. if (elm.classid) {
  245. const cla = await this.ctx.service.class.fetch({ id: elm.classid });
  246. data.push(cla);
  247. }
  248. }
  249. }
  250. return data;
  251. }
  252. async uplessones(data) {
  253. for (const _data of data) {
  254. await this.model.findByIdAndUpdate(_data.id, _data);
  255. }
  256. }
  257. }
  258. module.exports = LessonService;