apply.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. 'use strict';
  2. const assert = require('assert');
  3. const { after } = require('lodash');
  4. const _ = require('lodash');
  5. const moment = require('moment');
  6. const { ObjectId } = require('mongoose').Types;
  7. const { CrudService } = require('naf-framework-mongoose/lib/service');
  8. const { BusinessError, ErrorCode } = require('naf-core').Error;
  9. class ApplyService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'apply');
  12. this.model = this.ctx.model.Apply;
  13. this.tmodel = this.ctx.model.Teacher;
  14. this.submodel = this.ctx.model.Subject;
  15. this.trainmodel = this.ctx.model.Trainplan;
  16. this.umodel = this.ctx.model.User;
  17. this.dayList = [ '日', '一', '二', '三', '四', '五', '六' ];
  18. }
  19. // 查询
  20. async queryteacher(query) {
  21. const { termid, subid, date } = query;
  22. const data = await this.model
  23. .find({ termid, subid, date })
  24. .sort({ msscore: -1 });
  25. const teachers = [];
  26. for (const _data of data) {
  27. const teacherid = _data.teacherid;
  28. const teacher = await this.tmodel.findById(teacherid);
  29. teachers.push(teacher);
  30. }
  31. return teachers;
  32. }
  33. // 教师计划初步课表安排,可反复使用
  34. async arrangeteacher({ planid }) {
  35. const trainplan = await this.trainmodel.findById(planid);
  36. if (!trainplan) {
  37. throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在');
  38. }
  39. // trainplan = JSON.parse(JSON.stringify(trainplan));
  40. // 查找所有教师列表
  41. let teacherList = await this.tmodel.find({ xsscore: { $exists: true } });
  42. teacherList = JSON.parse(JSON.stringify(teacherList));
  43. // 查找所有教师上报列表
  44. let teaplanList = await this.model.find();
  45. teaplanList = JSON.parse(JSON.stringify(teaplanList));
  46. // 课程
  47. let subjectList = await this.submodel.find();
  48. subjectList = JSON.parse(JSON.stringify(subjectList));
  49. const termList = _.cloneDeep(trainplan);
  50. let { termnum } = termList;
  51. if (!termnum) return;
  52. termnum = JSON.parse(JSON.stringify(termnum));
  53. // 整理出课表
  54. const arr = this.setLessonList(termnum);
  55. // 安排后的课表
  56. const afterList = [];
  57. // 排课
  58. for (const l of arr) {
  59. const { termid, subid, day: date, teaid, status, batchid } = l;
  60. // 本期超过2次的教师列表,如果没有人就用这里分最高的排
  61. let outTwoTimesList = [];
  62. if (status && `${status}` === '1') {
  63. afterList.push(l);
  64. continue;
  65. }
  66. const subject = subjectList.find(f => ObjectId(subid).equals(f._id));
  67. if (subject.need_teacher !== '0') {
  68. afterList.push(l);
  69. continue;
  70. }
  71. // 申请该天,该科目的教师,并查出教师的名字,分数;并按分数排序
  72. let applyList = teaplanList.filter(
  73. f => f.date === date && f.subid === subid
  74. );
  75. applyList = applyList.map(i => {
  76. let obj = { ...JSON.parse(JSON.stringify(i)) };
  77. const r = teacherList.find(f => i.teacherid === f._id);
  78. if (r) {
  79. const { name: teaname, xsscore: score } = r;
  80. i.teaname = teaname;
  81. i.score = score * 1;
  82. obj = { ...obj, teaname, score };
  83. }
  84. return obj;
  85. });
  86. // 过滤出没有分数的,不排
  87. applyList = applyList.filter(f => f.score);
  88. // 按成绩排序
  89. applyList = _.orderBy(applyList, [ 'score' ], [ 'desc' ]);
  90. // 依次循环申请的教师列表,往这个课程安排中放教师
  91. for (const atea of applyList) {
  92. // 先查询,该教师,是否在今天有安排
  93. const tr = afterList.find(
  94. f => f.teaid === atea.teacherid && f.day === atea.date
  95. );
  96. if (tr) continue;
  97. // 查看这期内,每个申请上课的教师时候超过2天(2条记录),如果超过,则不排,但是如果最后没有人了,就得硬排了
  98. const r = afterList.filter(
  99. f => f.termid === termid && f.teaid === atea.teacherid
  100. );
  101. if (r.length >= 2) {
  102. outTwoTimesList = [ ...outTwoTimesList, atea ];
  103. continue;
  104. } else {
  105. l.teaid = atea.teacherid;
  106. l.teaname = atea.teaname;
  107. break;
  108. }
  109. }
  110. // 检查,该天,该科的课是否有教师
  111. const has_teaid = _.get(l, 'teaid');
  112. if (!has_teaid) {
  113. // // 如果没有教师,就需要在outTowTimesList列表中找分最高的教师
  114. const list = _.orderBy(outTwoTimesList, [ 'score' ], [ 'desc' ]);
  115. for (const i of list) {
  116. const tr = afterList.find(
  117. f => f.teaid === i.teacherid && f.day === i.date
  118. );
  119. if (tr) continue;
  120. else {
  121. l.teaid = i.teacherid;
  122. l.teaname = i.teaname;
  123. break;
  124. }
  125. }
  126. }
  127. afterList.push(l);
  128. }
  129. // 将afterList还原回正常的termnum;
  130. const newTermnum = this.returnTermnum(afterList, termnum);
  131. // 保存至计划
  132. trainplan.termnum = newTermnum;
  133. await trainplan.save();
  134. }
  135. // 确认计划安排
  136. async arrangeConfirm({ planid, ids }) {
  137. console.log(planid, ids);
  138. const trainplan = await this.trainmodel.findById(planid);
  139. if (!trainplan) {
  140. throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在');
  141. }
  142. const plan = _.cloneDeep(trainplan);
  143. let { termnum } = plan;
  144. if (!termnum) return;
  145. termnum = JSON.parse(JSON.stringify(termnum));
  146. // 过滤出确认的期,TODO:没有做通知
  147. // termnum = termnum.filter(f => );
  148. // 找到每个教师的位置,然后把状态(status)改成1=>已确认
  149. for (const t of termnum) {
  150. if (!ids.includes(t._id)) continue;
  151. const { term } = t;
  152. if (!(t.batchnum && _.isArray(t.batchnum))) continue;
  153. for (const b of t.batchnum) {
  154. const { batch } = b;
  155. if (!(b.class && _.isArray(b.class))) continue;
  156. for (const c of b.class) {
  157. if (!(c.lessons && _.isArray(c.lessons))) continue;
  158. for (const l of c.lessons) {
  159. l.status = '1';
  160. }
  161. }
  162. }
  163. }
  164. trainplan.termnum = termnum;
  165. await trainplan.save();
  166. }
  167. /**
  168. * 拍平了的课表=>termnum
  169. * @param {Array} list 拍平了的课表,详情参考页面的初步课表的数据
  170. * @param {Array} termnum 原termnum
  171. */
  172. returnTermnum(list, termnum) {
  173. let newTermnum = [];
  174. for (const l of list) {
  175. const { termid, batchid, classid, ...info } = l;
  176. const updata = _.pick(info, [
  177. 'day',
  178. 'subid',
  179. 'subname',
  180. 'teaid',
  181. 'teaname',
  182. 'time',
  183. 'status',
  184. ]);
  185. newTermnum = termnum.map(t => {
  186. // 找到期
  187. if (termid === t._id) {
  188. t.batchnum = t.batchnum.map(b => {
  189. if (batchid === b._id) {
  190. // 找到批次
  191. b.class = b.class.map(c => {
  192. if (classid === c._id) {
  193. if (c.lessons) {
  194. // 说明有课程安排,找有没有重复的,没有就推进去,有就更改,subid查
  195. const r = c.lessons.find(f => f.subid === updata.subid);
  196. if (r) {
  197. const rindex = c.lessons.findIndex(
  198. f => f.subid === updata.subid
  199. );
  200. c.lessons[rindex] = updata;
  201. } else {
  202. c.lessons.push(updata);
  203. }
  204. } else {
  205. // 说明没有课程安排,放进去一条保存
  206. c.lessons = [ updata ];
  207. }
  208. }
  209. return c;
  210. });
  211. }
  212. return b;
  213. });
  214. }
  215. return t;
  216. });
  217. }
  218. return newTermnum;
  219. }
  220. /**
  221. * 将课表拍平了,从多维=>一维
  222. * @param {Array} termnum 计划的termnum
  223. */
  224. setLessonList(termnum) {
  225. let arr = [];
  226. for (const t of termnum) {
  227. const { batchnum, term, _id: termid } = t;
  228. // 班级和课程一一匹
  229. for (const b of batchnum) {
  230. const { class: classes, lessons, _id: batchid } = b;
  231. const claslesList = this.setList(
  232. term * 1,
  233. termid,
  234. batchid,
  235. classes,
  236. lessons
  237. );
  238. arr.push(...claslesList);
  239. }
  240. }
  241. arr = _.orderBy(arr, [ 'term', 'day' ], [ 'asc', 'asc' ]);
  242. return arr;
  243. }
  244. /**
  245. * 将课表模板和班级整理成一维数组
  246. * @param {String} term 期数
  247. * @param {String} termid 期id
  248. * @param {String} batchid 批id
  249. * @param {Array} classes 班级列表
  250. * @param {Array} lessonTemplate 课表模板
  251. */
  252. setList(term, termid, batchid, classes, lessonTemplate) {
  253. const arr = [];
  254. // 班级和课程匹配
  255. for (const cla of classes) {
  256. let { lessons } = cla;
  257. if (!lessons) lessons = lessonTemplate;
  258. for (const i of lessons) {
  259. let nobj = {};
  260. nobj.term = term;
  261. nobj.termid = termid;
  262. nobj.batchid = batchid;
  263. const obj = _.omit(cla, [ 'lessons' ]);
  264. nobj.classid = _.clone(cla._id);
  265. nobj = _.assign(nobj, obj);
  266. nobj = _.assign(nobj, i);
  267. arr.push(nobj);
  268. }
  269. }
  270. return arr;
  271. }
  272. /**
  273. * 发送消息
  274. * @param {Object} param planid:年度计划id,ids,发送的期列表;classtype:发送班级类型 undefined 都发,有的话就找指定班级类型发
  275. */
  276. async arrangeSendMsg({ planid, ids, classtype }) {
  277. const trainplan = await this.trainmodel.findById(planid);
  278. if (!trainplan) {
  279. throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在');
  280. }
  281. const plan = _.cloneDeep(trainplan);
  282. let { termnum } = plan;
  283. if (!termnum) return;
  284. termnum = JSON.parse(JSON.stringify(termnum));
  285. // 整理出课表
  286. let arr = this.setLessonList(termnum);
  287. // 过滤出需要发送的教师
  288. arr = arr.filter(f => ids.find(id => f.termid === id) && f.teaid);
  289. // && f.status !== '1'
  290. // 整理出要发送的教师列表
  291. let teaids = arr.map(i => i.teaid);
  292. teaids = _.uniq(teaids);
  293. // 找到教师信息
  294. let teaList = await this.tmodel.find({ _id: teaids });
  295. // 找到教师用户信息
  296. let teauserList = await this.umodel.find({ uid: teaids });
  297. if (teaList) teaList = JSON.parse(JSON.stringify(teaList));
  298. if (teauserList)teauserList = JSON.parse(JSON.stringify(teauserList));
  299. // 发送,此处是根据安排,给教师发.还有一种方案是根据教师,整理安排一起发送
  300. for (const l of arr) {
  301. // 教师id,期数,班级名,上课的日期,课程名
  302. const { teaid, term, name, day, subname } = l;
  303. const tea = teaList.find(f => f._id === teaid);
  304. const teauser = teauserList.find(f => f.uid === teaid);
  305. // 文案
  306. let msg = `${_.get(tea, 'name', '')}老师您好:
  307. 吉林省高等学校毕业生就业指导中心-双困生培训系统提醒您:
  308. ${term}期-${name.includes('班') ? name : `${name}班`}
  309. ${day}(星期${this.dayList[moment(day).days()]})
  310. 有您的课程安排:${subname}`;
  311. msg = `${msg}\n 如果您无法进行授课,请及时联系中心负责人`;
  312. // 邮箱与微信都发送
  313. const { email } = tea;
  314. if (email) {
  315. this.toSendEmail(email, msg, tea.name);
  316. }
  317. const { openid } = teauser;
  318. if (openid) {
  319. this.toSendWxMsg(openid, msg, tea.name);
  320. }
  321. }
  322. }
  323. /**
  324. * 计划-教师初步课表发送邮件
  325. * @param {String} email 邮件
  326. * @param {String} content 内容
  327. * @param {String} teaname 教师姓名
  328. */
  329. async toSendEmail(email, content, teaname) {
  330. if (!email) {
  331. console.error(`计划教师发送通知:${teaname}没有email`);
  332. return;
  333. }
  334. const subject = '吉林省高等学校毕业生就业指导中心通知(系统邮件,请勿回复)'; //
  335. this.ctx.service.util.sendMail(email, subject, content);
  336. }
  337. /**
  338. * 计划-教师初步课表发送微信推送
  339. * @param {String} openid 微信公众号的openid
  340. * @param {String} content 内容
  341. * @param {String} teaname 教师姓名
  342. */
  343. async toSendWxMsg(openid, content, teaname) {
  344. if (!openid) {
  345. console.error(`计划教师发送微信推送:${teaname}没有openid`);
  346. return;
  347. }
  348. // TODO or notTODO 发送微信推送记录
  349. // const tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + teacherUser.uid + '&noticeid=' + nresid;
  350. await this.ctx.service.weixin.sendTemplateDesign(
  351. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  352. openid,
  353. '您有一个新的通知',
  354. '您有新的安排',
  355. content,
  356. '感谢您的使用'
  357. );
  358. }
  359. }
  360. module.exports = ApplyService;