apply.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 => ids.includes(f._id));
  148. // 找到每个教师的位置,然后把状态(status)改成1=>已确认
  149. for (const t of termnum) {
  150. const { term } = t;
  151. if (!(t.batchnum && _.isArray(t.batchnum))) continue;
  152. for (const b of t.batchnum) {
  153. const { batch } = b;
  154. if (!(b.class && _.isArray(b.class))) continue;
  155. for (const c of b.class) {
  156. if (!(c.lessons && _.isArray(c.lessons))) continue;
  157. for (const l of c.lessons) {
  158. l.status = '1';
  159. }
  160. }
  161. }
  162. }
  163. trainplan.termnum = termnum;
  164. await trainplan.save();
  165. }
  166. /**
  167. * 拍平了的课表=>termnum
  168. * @param {Array} list 拍平了的课表,详情参考页面的初步课表的数据
  169. * @param {Array} termnum 原termnum
  170. */
  171. returnTermnum(list, termnum) {
  172. let newTermnum = [];
  173. for (const l of list) {
  174. const { termid, batchid, classid, ...info } = l;
  175. const updata = _.pick(info, [
  176. 'day',
  177. 'subid',
  178. 'subname',
  179. 'teaid',
  180. 'teaname',
  181. 'time',
  182. 'status',
  183. ]);
  184. newTermnum = termnum.map(t => {
  185. // 找到期
  186. if (termid === t._id) {
  187. t.batchnum = t.batchnum.map(b => {
  188. if (batchid === b._id) {
  189. // 找到批次
  190. b.class = b.class.map(c => {
  191. if (classid === c._id) {
  192. if (c.lessons) {
  193. // 说明有课程安排,找有没有重复的,没有就推进去,有就更改,subid查
  194. const r = c.lessons.find(f => f.subid === updata.subid);
  195. if (r) {
  196. const rindex = c.lessons.findIndex(
  197. f => f.subid === updata.subid
  198. );
  199. c.lessons[rindex] = updata;
  200. } else {
  201. c.lessons.push(updata);
  202. }
  203. } else {
  204. // 说明没有课程安排,放进去一条保存
  205. c.lessons = [ updata ];
  206. }
  207. }
  208. return c;
  209. });
  210. }
  211. return b;
  212. });
  213. }
  214. return t;
  215. });
  216. }
  217. return newTermnum;
  218. }
  219. /**
  220. * 将课表拍平了,从多维=>一维
  221. * @param {Array} termnum 计划的termnum
  222. */
  223. setLessonList(termnum) {
  224. let arr = [];
  225. for (const t of termnum) {
  226. const { batchnum, term, _id: termid } = t;
  227. // 班级和课程一一匹
  228. for (const b of batchnum) {
  229. const { class: classes, lessons, _id: batchid } = b;
  230. const claslesList = this.setList(
  231. term * 1,
  232. termid,
  233. batchid,
  234. classes,
  235. lessons
  236. );
  237. arr.push(...claslesList);
  238. }
  239. }
  240. arr = _.orderBy(arr, [ 'term', 'day' ], [ 'asc', 'asc' ]);
  241. return arr;
  242. }
  243. /**
  244. * 将课表模板和班级整理成一维数组
  245. * @param {String} term 期数
  246. * @param {String} termid 期id
  247. * @param {String} batchid 批id
  248. * @param {Array} classes 班级列表
  249. * @param {Array} lessonTemplate 课表模板
  250. */
  251. setList(term, termid, batchid, classes, lessonTemplate) {
  252. const arr = [];
  253. // 班级和课程匹配
  254. for (const cla of classes) {
  255. let { lessons } = cla;
  256. if (!lessons) lessons = lessonTemplate;
  257. for (const i of lessons) {
  258. let nobj = {};
  259. nobj.term = term;
  260. nobj.termid = termid;
  261. nobj.batchid = batchid;
  262. const obj = _.omit(cla, [ 'lessons' ]);
  263. nobj.classid = _.clone(cla._id);
  264. nobj = _.assign(nobj, obj);
  265. nobj = _.assign(nobj, i);
  266. arr.push(nobj);
  267. }
  268. }
  269. return arr;
  270. }
  271. /**
  272. * 发送消息
  273. * @param {Object} param planid:年度计划id,ids,发送的期列表;classtype:发送班级类型 undefined 都发,有的话就找指定班级类型发
  274. */
  275. async arrangeSendMsg({ planid, ids, classtype }) {
  276. const trainplan = await this.trainmodel.findById(planid);
  277. if (!trainplan) {
  278. throw new BusinessError(ErrorCode.DATA_EXISTED, '年度计划不存在');
  279. }
  280. const plan = _.cloneDeep(trainplan);
  281. let { termnum } = plan;
  282. if (!termnum) return;
  283. termnum = JSON.parse(JSON.stringify(termnum));
  284. // 整理出课表
  285. let arr = this.setLessonList(termnum);
  286. // 过滤出需要发送的教师
  287. arr = arr.filter(f => ids.find(id => f.termid === id) && f.teaid);
  288. // && f.status !== '1'
  289. // 整理出要发送的教师列表
  290. let teaids = arr.map(i => i.teaid);
  291. teaids = _.uniq(teaids);
  292. // 找到教师信息
  293. let teaList = await this.tmodel.find({ _id: teaids });
  294. // 找到教师用户信息
  295. let teauserList = await this.umodel.find({ uid: teaids });
  296. if (teaList) teaList = JSON.parse(JSON.stringify(teaList));
  297. if (teauserList)teauserList = JSON.parse(JSON.stringify(teauserList));
  298. // 发送,此处是根据安排,给教师发.还有一种方案是根据教师,整理安排一起发送
  299. for (const l of arr) {
  300. // 教师id,期数,班级名,上课的日期,课程名
  301. const { teaid, term, name, day, subname } = l;
  302. const tea = teaList.find(f => f._id === teaid);
  303. const teauser = teauserList.find(f => f.uid === teaid);
  304. // 文案
  305. const msg = `${_.get(tea, 'name', '')}老师您好:
  306. 吉林省高等学校毕业生就业指导中心-双困生培训系统提醒您:
  307. ${term}期-${name.includes('班') ? name : `${name}班`}
  308. ${day}(星期${this.dayList[moment(day).days()]})
  309. 有您的课程安排:${subname}`;
  310. // 邮箱与微信都发送
  311. const { email } = tea;
  312. if (email) {
  313. this.toSendEmail(email, msg, tea.name);
  314. }
  315. const { openid } = teauser;
  316. if (openid) {
  317. this.toSendWxMsg(openid, msg, tea.name);
  318. }
  319. }
  320. }
  321. /**
  322. * 计划-教师初步课表发送邮件
  323. * @param {String} email 邮件
  324. * @param {String} content 内容
  325. * @param {String} teaname 教师姓名
  326. */
  327. async toSendEmail(email, content, teaname) {
  328. if (!email) {
  329. console.error(`计划教师发送通知:${teaname}没有email`);
  330. return;
  331. }
  332. const subject = '吉林省高等学校毕业生就业指导中心通知(系统邮件,请勿回复)'; //
  333. this.ctx.service.util.sendMail(email, subject, content);
  334. }
  335. /**
  336. * 计划-教师初步课表发送微信推送
  337. * @param {String} openid 微信公众号的openid
  338. * @param {String} content 内容
  339. * @param {String} teaname 教师姓名
  340. */
  341. async toSendWxMsg(openid, content, teaname) {
  342. if (!openid) {
  343. console.error(`计划教师发送微信推送:${teaname}没有openid`);
  344. return;
  345. }
  346. // TODO or notTODO 发送微信推送记录
  347. // const tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + teacherUser.uid + '&noticeid=' + nresid;
  348. await this.ctx.service.weixin.sendTemplateDesign(
  349. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  350. openid,
  351. '您有一个新的通知',
  352. '您有新的安排',
  353. content,
  354. '感谢您的使用'
  355. );
  356. }
  357. }
  358. module.exports = ApplyService;