class.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class ClassService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'class');
  10. this.model = this.ctx.model.Class;
  11. this.stumodel = this.ctx.model.Student;
  12. this.lessmodel = this.ctx.model.Lesson;
  13. this.umodel = this.ctx.model.User;
  14. this.tmodel = this.ctx.model.Trainplan;
  15. this.gmodel = this.ctx.model.Group;
  16. this.heamodel = this.ctx.model.Headteacher;
  17. this.teamodel = this.ctx.model.Teacher;
  18. this.locamodel = this.ctx.model.Location;
  19. }
  20. async divide(data) {
  21. // 21-04-27重做
  22. const { planid, termid } = data;
  23. assert(planid, '计划id为必填项');
  24. assert(termid, '期id为必填项');
  25. // 先自动生成班级 TODO:之后放开
  26. await this.autoclass(planid, termid);
  27. // 根据计划id与期id查询所有批次下的班级
  28. const newclass = await this.model.find({ planid, termid });
  29. if (!newclass) {
  30. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  31. }
  32. // 根据计划和期查询所有上报的学生 并按照学校排序
  33. const newstudent = await this.stumodel.find({ termid }).sort({ schid: 1 });
  34. if (!newstudent) {
  35. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  36. }
  37. // 按批次分组,每个批次处理自己的
  38. const claGroup = _.groupBy(newclass, 'batchid');
  39. const keys = Object.keys(claGroup);
  40. const result = {}; // key:班级id;value:学生id数组
  41. const pList = []; // 补人结果
  42. // 循环批次
  43. for (const bkey of keys) {
  44. const classList = claGroup[bkey]; // 该批次下的班级列表
  45. const batchStudentList = _.shuffle(newstudent.filter(f => f.batchid === bkey)); // shuffle:打乱顺序 该批次下的学生列表
  46. // 分为多个学校 男/女 数组
  47. // 按学校分组
  48. const sgroup = _.groupBy(batchStudentList, 'schid');
  49. // 学校代码key
  50. const schKeys = Object.keys(sgroup);
  51. let solve = {};
  52. const studentGroup = {}; // key:学校id-boy/girl;value:对应学校,性别的学生列表
  53. const classnum = classList.length; // 班级数
  54. const minClass = _.minBy(classList, i => parseInt(i.number)); // 该批次人数最少的班级
  55. let minNumber = 0;
  56. if (minClass) minNumber = parseInt(minClass.number);
  57. for (const skey of schKeys) {
  58. const schstus = sgroup[skey];
  59. // 获得每个学校的男/女人数
  60. const boys = schstus.filter(f => f.gender.includes('男'));
  61. const girls = schstus.filter(f => f.gender.includes('女'));
  62. studentGroup[`${skey}-boy`] = boys;
  63. studentGroup[`${skey}-girl`] = girls;
  64. // 算出平均分配解:每个 学校 固定向 每个班 派多少 男/女生
  65. // 需要验证最少的班级人数:因为平均后,可能造成人多了,要吐出来的
  66. const br = this.numDivide(boys.length, classnum);
  67. const gr = this.numDivide(girls.length, classnum);
  68. // 存入最佳计算结果,但是需要验证,看看这批次的计算结果是否<=班级最少人数;多了可是要吐人的.吐人的写法代码更多
  69. solve[`${skey}-boy`] = br;
  70. solve[`${skey}-girl`] = gr;
  71. }
  72. // 验证最佳结果是否超出班级的最少人数,不是,则处理
  73. solve = this.checkSolve(solve, minNumber, classnum);
  74. console.log(solve);
  75. // 塞人
  76. for (const c of classList) {
  77. const { _id } = c;
  78. if (!(result[_id] && _.isArray(result[_id]))) result[_id] = [];
  79. for (const key in solve) {
  80. const { res } = solve[key];
  81. let sList = studentGroup[key];
  82. // 取出指定人数
  83. const inputs = _.take(sList, res);
  84. // 放进结果里
  85. // .map(i => i._id)
  86. result[_id].push(...inputs);
  87. // 删除指定人数
  88. sList = _.drop(sList, res);
  89. // 赋值回去
  90. studentGroup[key] = sList;
  91. }
  92. }
  93. // 补人
  94. for (const c of classList) {
  95. const { _id, number, name } = c;
  96. let nowNum = result[_id].length;
  97. const soldup = _.cloneDeep(solve);
  98. while (nowNum < number) {
  99. // 补人,根据solve,获取补人的选择列表
  100. const res = this.getSelects(soldup);
  101. for (const r of res) {
  102. const { schid, gender } = r;
  103. // 获取学生列表
  104. const sList = studentGroup[`${schid}-${gender}`];
  105. const head = _.head(sList);
  106. if (head) {
  107. // 有学生,往班级里推,备选中删除,重新计算当前分配的班级人数,修改solve副本
  108. // result[_id].push(head._id);
  109. result[_id].push(head);
  110. studentGroup[`${schid}-${gender}`] = _.drop(sList);
  111. nowNum = result[_id].length;
  112. soldup[`${schid}-${gender}`].res++;
  113. pList.push(`${name}班用${schid}-${gender}补人:当前人数为${nowNum};要求${number}`);
  114. break;
  115. } else continue;
  116. }
  117. }
  118. }
  119. }
  120. // console.log(pList);
  121. // for (const key in result) {
  122. // console.group(key);
  123. // const list = result[key];
  124. // const b = list.filter(f => f.gender === '男');
  125. // const g = list.filter(f => f.gender === '女');
  126. // console.log(b.length, g.length);
  127. // console.groupEnd();
  128. // }
  129. // 更新学生的班级
  130. const ckeys = Object.keys(result);
  131. for (const classid of ckeys) {
  132. await this.stumodel.updateMany({ _id: result[classid] }, { classid });
  133. }
  134. // 新添,给学生排序号
  135. const claList = await this.model.find({ termid });
  136. for (const cla of claList) {
  137. await this.ctx.service.student.arrangeNumber({ classid: cla._id });
  138. }
  139. }
  140. /**
  141. * 算整除和取余
  142. * @param {Any} num1 性别人数
  143. * @param {Any} num2 班级人数
  144. * @property {Number} res 整除结果
  145. * @property {Number} el 余数
  146. */
  147. numDivide(num1, num2) {
  148. num1 = _.isNaN(parseInt(num1)) ? 0 : parseInt(num1);
  149. num2 = _.isNaN(parseInt(num2)) ? 0 : parseInt(num2);
  150. const res = _.floor(num1 / num2, 0);
  151. const el = num1 % num2;
  152. return { res, el };
  153. }
  154. /**
  155. * 验证计算结果是否 不超过 最少人数班级的人数
  156. * @param {Object} solve 计算结果:key:${schid}-${gender}
  157. * @param {Number} number 最少人数班级的人数
  158. * @param {Number} classnum 班级数量,如果需要减人数的话,是要将每班的人数减1,-1就意味着要 - 班级数 *1;余数+班级数*1
  159. */
  160. checkSolve(solve, number, classnum) {
  161. let countClassNum = 0;
  162. let ns = [];
  163. for (const key in solve) {
  164. const { res = 0, el = 0 } = _.get(solve, key, {});
  165. countClassNum += res;
  166. ns.push({ res, el, key });
  167. }
  168. if (countClassNum <= number) return solve;
  169. do {
  170. ns = _.orderBy(ns, [ 'res' ], [ 'desc' ]);
  171. const head = _.head(ns);
  172. head.res--;
  173. head.el += classnum;
  174. ns[0] = head;
  175. countClassNum = ns.reduce((p, n) => p + n.res, 0);
  176. } while (countClassNum > number);
  177. for (const i of ns) {
  178. const { key, ...others } = i;
  179. solve[key] = { ...others };
  180. }
  181. return solve;
  182. }
  183. // 获取选择结果
  184. getSelects(solve) {
  185. // 做出补人选项的集合:按学校人数,性别人数来看
  186. const keys = Object.keys(solve);
  187. let schids = keys.map(i => {
  188. const arr = i.split('-');
  189. let res;
  190. if (arr[0] && parseInt(arr[0])) res = arr[0];
  191. return res;
  192. });
  193. schids = _.compact(_.uniq(schids));
  194. let genders = keys.map(i => {
  195. const arr = i.split('-');
  196. return arr[1];
  197. });
  198. genders = _.compact(_.uniq(genders));
  199. let schArr = [];
  200. for (const schid of schids) {
  201. const midKeys = keys.filter(f => f.includes(schid));
  202. let count = 0;
  203. for (const k of midKeys) {
  204. count += solve[k].res;
  205. }
  206. const obj = { schid, count };
  207. schArr.push(obj);
  208. }
  209. let genArr = [];
  210. for (const gen of genders) {
  211. const midKeys = keys.filter(f => f.includes(gen));
  212. let count = 0;
  213. for (const k of midKeys) {
  214. count += solve[k].res;
  215. }
  216. const obj = { gender: gen, count };
  217. genArr.push(obj);
  218. }
  219. // 2个数组升序排序
  220. schArr = _.orderBy(schArr, [ 'count' ], [ 'asc' ]);
  221. genArr = _.orderBy(genArr, [ 'count' ], [ 'asc' ]);
  222. const arr = [];
  223. // 优先性别
  224. // for (const gen of genArr) {
  225. // for (const sch of schArr) {
  226. // const { schid } = sch;
  227. // const { gender } = gen;
  228. // arr.push({ schid, gender });
  229. // }
  230. // }
  231. // 优先学校
  232. for (const sch of schArr) {
  233. for (const gen of genArr) {
  234. const { schid } = sch;
  235. const { gender } = gen;
  236. arr.push({ schid, gender });
  237. }
  238. }
  239. return arr;
  240. }
  241. // 取得同样类型的学生
  242. async getstutype(_students, type) {
  243. const data = [];
  244. for (const stuid of _students) {
  245. const student = await this.stumodel.findById(stuid);
  246. if (student && student.type === type) {
  247. data.push(stuid);
  248. }
  249. }
  250. return data;
  251. }
  252. // 自动生成班级私有方法
  253. async autoclass(planid, termid) {
  254. // 删除所有计划下的班级
  255. await this.model.deleteMany({ planid, termid });
  256. // 根据批次id取得当前批次具体信息
  257. const res = await this.tmodel.findById(planid);
  258. if (!res) {
  259. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  260. }
  261. // 循环出所有班级进行添加操作
  262. const term = await res.termnum.id(termid);
  263. for (const batch of term.batchnum) {
  264. const classs = await batch.class;
  265. for (const cla of classs) {
  266. const newdata = { name: cla.name, number: cla.number, batchid: batch.id, termid: term.id, planid: res.id, type: cla.type };
  267. const rescla = await this.model.create(newdata);
  268. await this.toSetClassSetting({ classid: rescla._id });
  269. }
  270. }
  271. }
  272. // 根据传入的学生列表和班级id更新学生信息
  273. async studentup(classid, batchid, beforestu) {
  274. // 循环学生id
  275. for (const stuid of beforestu) {
  276. const student = await this.stumodel.findById(stuid);
  277. if (student) {
  278. student.classid = classid;
  279. student.batchid = batchid;
  280. await student.save();
  281. }
  282. }
  283. }
  284. // 自动分组
  285. async groupcreate(termid, batchid, classid) {
  286. const group = await this.gmodel.find({ termid, batchid, classid });
  287. if (group.length === 0) {
  288. for (let i = 1; i < 8; i++) {
  289. const name = i + '组';
  290. const newdata = { name, termid, batchid, classid };
  291. await this.gmodel.create(newdata);
  292. }
  293. }
  294. }
  295. // 根据传入的学生列表和班级id更新学生信息
  296. async studentupclass({ id }, data) {
  297. assert(id, '班级id为必填项');
  298. // 根据全年计划表id查出对应的全年计划详细信息
  299. const trainplan = await this.tmodel.findOne({ 'termnum.batchnum.class._id': ObjectId(id) });
  300. if (!trainplan) {
  301. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  302. }
  303. // 取得计划期批次信息
  304. let termid = '';
  305. let batchid = '';
  306. let classname = '';
  307. let class_ = {};
  308. for (const term of trainplan.termnum) {
  309. for (const batch of term.batchnum) {
  310. const _class = await batch.class.id(id);
  311. if (_class) {
  312. termid = term.id;
  313. batchid = batch.id;
  314. classname = _class.name;
  315. class_ = _class;
  316. break;
  317. }
  318. }
  319. }
  320. if (!class_) {
  321. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  322. }
  323. let classid_ = '';
  324. if (classname) {
  325. const cla_ = await this.model.findOne({ termid, batchid, name: classname });
  326. if (cla_) {
  327. classid_ = cla_.id;
  328. } else {
  329. const newdata = {
  330. name: class_.name,
  331. number: class_.number,
  332. batchid,
  333. termid,
  334. planid: trainplan.id,
  335. type: class_.type,
  336. headteacherid: class_.headteacherid,
  337. };
  338. const rescla = await this.model.create(newdata);
  339. if (rescla) {
  340. classid_ = rescla.id;
  341. }
  342. }
  343. }
  344. if (classid_) {
  345. // 循环学生id
  346. for (const stuid of data) {
  347. const student = await this.stumodel.findById(stuid);
  348. if (student) {
  349. student.classid = classid_;
  350. await student.save();
  351. }
  352. }
  353. }
  354. // 添加,给学生排序号
  355. await this.ctx.service.student.arrangeNumber({ classid: classid_ });
  356. // TODO 根据模板复制班级信息
  357. await this.toSetClassSetting({ classid: classid_ });
  358. }
  359. async notice(data) {
  360. for (const classid of data.classids) {
  361. // 根据班级id找到需要通知的班级
  362. const _class = await this.model.findById(classid);
  363. const { headteacherid } = _class;
  364. // 根据班级id找到对应的课程表
  365. const lesson = await this.lessmodel.findOne({ classid });
  366. if (lesson) {
  367. const lessons = lesson.lessons;
  368. const remark = '感谢您的使用';
  369. const date = await this.ctx.service.util.updatedate();
  370. const detail = '班级各项信息已确认,请注意查收';
  371. // 遍历班级授课教师发送通知
  372. for (const lessoninfo of lessons) {
  373. const teaid = lessoninfo.teaid;
  374. const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
  375. if (_teacher) {
  376. const teaopenid = _teacher.openid;
  377. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  378. }
  379. }
  380. // 给班主任发送通知
  381. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  382. if (_headteacher) {
  383. const headteaopenid = _headteacher.openid;
  384. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  385. }
  386. // 根据班级的期id查询对应的培训计划
  387. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  388. const term = await trainplan.termnum.id(_class.termid);
  389. const batch = await term.batchnum.id(_class.batchid);
  390. const startdate = batch.startdate;
  391. const classname = _class.name;
  392. // 给班级所有学生发送邮件通知
  393. const students = await this.stumodel.find({ classid });
  394. for (const student of students) {
  395. const { email, name } = student;
  396. const subject = '吉林省高等学校毕业生就业指导中心通知';
  397. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  398. this.ctx.service.util.sendMail(email, subject, text);
  399. }
  400. }
  401. }
  402. }
  403. async uptea(data) {
  404. for (const _data of data) {
  405. const classInfo = await this.model.findById(_data.id);
  406. classInfo.headteacherid = _data.headteacherid;
  407. await classInfo.save();
  408. }
  409. }
  410. async query({ skip, limit, ...info }) {
  411. const classes = await this.model
  412. .find(info)
  413. .populate([
  414. {
  415. path: 'yclocationid',
  416. model: 'Location',
  417. select: 'name',
  418. },
  419. {
  420. path: 'kzjhlocationid',
  421. model: 'Location',
  422. select: 'name',
  423. },
  424. {
  425. path: 'kbyslocationid',
  426. model: 'Location',
  427. select: 'name',
  428. },
  429. {
  430. path: 'jslocationid',
  431. model: 'Location',
  432. select: 'name',
  433. },
  434. {
  435. path: 'headteacherid',
  436. model: 'Headteacher',
  437. select: 'name',
  438. },
  439. ])
  440. .skip(Number(skip))
  441. .limit(Number(limit));
  442. const data = [];
  443. let planids = classes.map(i => i.planid);
  444. planids = _.uniq(planids);
  445. const trainplan = await this.tmodel.find({ _id: { $in: planids } });
  446. for (const _class of classes) {
  447. let res = await this.setClassData(_class, trainplan);
  448. if (res) {
  449. res = this.setData(res);
  450. data.push(res);
  451. } else {
  452. data.push(_class);
  453. }
  454. }
  455. return data;
  456. }
  457. async fetch({ id }) {
  458. let classInfo = await this.model.findById(id).populate([
  459. {
  460. path: 'yclocationid',
  461. model: 'Location',
  462. select: 'name',
  463. },
  464. {
  465. path: 'kzjhlocationid',
  466. model: 'Location',
  467. select: 'name',
  468. },
  469. {
  470. path: 'kbyslocationid',
  471. model: 'Location',
  472. select: 'name',
  473. },
  474. {
  475. path: 'jslocationid',
  476. model: 'Location',
  477. select: 'name',
  478. },
  479. {
  480. path: 'headteacherid',
  481. model: 'Headteacher',
  482. select: 'name',
  483. },
  484. ]);
  485. const trainplan = await this.tmodel.findById(classInfo.planid);
  486. classInfo = await this.setClassData(classInfo, [ trainplan ]);
  487. classInfo = this.setData(classInfo);
  488. return classInfo;
  489. }
  490. // 整理数据,找礼仪教师
  491. async setClassData(cla, trainplan) {
  492. const { planid, termid, batchid } = cla;
  493. cla = JSON.parse(JSON.stringify(cla));
  494. const tpRes = trainplan.find(f => ObjectId(planid).equals(f._id));
  495. if (!tpRes) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的计划信息');
  496. const t = tpRes.termnum.id(termid);
  497. if (!t) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的期信息');
  498. const { term, batchnum } = t;
  499. if (!term) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的期信息');
  500. else cla.term = term;
  501. if (!batchnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的批次信息');
  502. const b = batchnum.id(batchid);
  503. if (!b) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的批次信息');
  504. const { batch, startdate, enddate } = b;
  505. if (batch)cla.batch = batch;
  506. if (startdate) cla.startdate = startdate;
  507. if (enddate) cla.enddate = enddate;
  508. // 礼仪教师
  509. if (cla.lyteacherid) {
  510. let res = await this.teamodel.findById(cla.lyteacherid);
  511. if (!res) res = await this.heamodel.findById(cla.lyteacherid);
  512. if (res) cla.lyteacher = res.name;
  513. }
  514. return cla;
  515. }
  516. // 整理数据
  517. setData(cla) {
  518. const { headteacherid, yclocationid, kzjhlocationid, kbyslocationid, jslocationid } = cla;
  519. const arr = [];
  520. if (headteacherid && _.isObject(headteacherid)) arr.push({ headteacherid });
  521. if (yclocationid && _.isObject(yclocationid)) arr.push({ yclocationid });
  522. if (kzjhlocationid && _.isObject(kzjhlocationid)) arr.push({ kzjhlocationid });
  523. if (kbyslocationid && _.isObject(kbyslocationid)) arr.push({ kbyslocationid });
  524. if (jslocationid && _.isObject(jslocationid)) arr.push({ jslocationid });
  525. for (const kid of arr) {
  526. for (const key in kid) {
  527. if (kid.hasOwnProperty(key)) {
  528. const obj = kid[key];
  529. const { _id, name } = obj;
  530. const keynoids = key.split('id');
  531. cla[key] = _id;
  532. cla[_.get(keynoids, 0)] = name;
  533. }
  534. }
  535. }
  536. return cla;
  537. }
  538. async upclasses(data) {
  539. for (const _data of data) {
  540. await this.model.findByIdAndUpdate(_data.id, _data);
  541. }
  542. }
  543. async classinfo({ id: classid }) {
  544. const _classes = await this.model.findById(classid);
  545. // 班级信息
  546. const classes = _.cloneDeep(JSON.parse(JSON.stringify(_classes)));
  547. // 学生信息
  548. const students = await this.stumodel.find({ classid });
  549. // 所有用户信息
  550. const users = await this.umodel.find();
  551. if (students) {
  552. for (const stu of students) {
  553. const user = users.find(item => item.uid === stu.id);
  554. if (user && user.openid) {
  555. const _stu = _.cloneDeep(JSON.parse(JSON.stringify(stu)));
  556. _stu.hasuserinfo = '1';
  557. _.remove(students, stu);
  558. students.push(_stu);
  559. }
  560. }
  561. classes.students = students;
  562. }
  563. // 班主任信息
  564. let headteacher;
  565. if (classes.headteacherid) {
  566. headteacher = await this.heamodel.findById(classes.headteacherid);
  567. }
  568. // 礼仪课老师信息
  569. let lyteacher;
  570. if (classes.lyteacherid) {
  571. lyteacher = await this.heamodel.findById(classes.lyteacherid);
  572. if (!lyteacher) {
  573. lyteacher = await this.teamodel.findById(classes.lyteacherid);
  574. }
  575. }
  576. // 教课老师信息
  577. let teachers = [];
  578. const lessones = await this.lessmodel.findOne({ classid });
  579. if (lessones) {
  580. for (const lesson of lessones.lessons) {
  581. if (lesson.teaid) {
  582. const teacher = await this.teamodel.findById(lesson.teaid);
  583. teachers.push(teacher);
  584. }
  585. }
  586. }
  587. teachers.push(lyteacher);
  588. teachers.push(headteacher);
  589. teachers = _.uniq(_.compact(teachers));
  590. for (const tea of teachers) {
  591. const user = users.find(item => item.uid === tea.id);
  592. if (user && user.openid) {
  593. const _tea = _.cloneDeep(JSON.parse(JSON.stringify(tea)));
  594. _tea.hasuserinfo = '1';
  595. _.remove(teachers, tea);
  596. teachers.push(_tea);
  597. }
  598. }
  599. classes.teachers = teachers;
  600. return classes;
  601. }
  602. // 根据模板设置班级信息
  603. async toSetClassSetting({ classid }) {
  604. const setting = await this.ctx.model.Setting.findOne();
  605. if (!setting) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到系统设置');
  606. const { template_term } = setting;
  607. if (!template_term) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级模板设置');
  608. const templateList = await this.query({ termid: template_term });
  609. const tClass = await this.model.findById(classid);
  610. if (!tClass) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级不存在,无法复制设定的班级设置');
  611. const { name, termid, batchid } = tClass;
  612. const r = templateList.find(f => f.name === name);
  613. // 找到班主任全年计划
  614. const trainPlan = await this.tmodel.findById(tClass.planid);
  615. if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该班所在的年度计划信息');
  616. const tpt = trainPlan.termnum.id(tClass.termid);
  617. if (!tpt) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该班所在年度计划的期信息');
  618. const tpb = tpt.batchnum.id(tClass.batchid);
  619. if (!tpb) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该班所在年度计划的批次信息');
  620. if (!tpb.class) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该班所在年度计划批次下的班级');
  621. const tpc = tpb.class.find(f => f.name === tClass.name);
  622. if (!tpc) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该班所在年度计划的班级信息');
  623. const { headteacherid } = tpc;
  624. if (r) {
  625. // 说明这个是正常班,且从模板中找得到; 除了礼仪教师外,都复制过来
  626. const { jslocationid, kbyslocationid, kzjhlocationid, yclocationid } = r;
  627. if (!tClass.jslocation && jslocationid) tClass.jslocationid = jslocationid;
  628. if (!tClass.kbyslocationid && kbyslocationid) tClass.kbyslocationid = kbyslocationid;
  629. if (!tClass.kzjhlocationid && kzjhlocationid) tClass.kzjhlocationid = kzjhlocationid;
  630. if (!tClass.yclocationid && yclocationid) tClass.yclocationid = yclocationid;
  631. if (!tClass.headteacherid && headteacherid) {
  632. tClass.headteacherid = headteacherid;
  633. // 默认班主任为礼仪教师
  634. tClass.lyteacherid = headteacherid;
  635. }
  636. await tClass.save();
  637. } else {
  638. // 没找到,有可能是普通班,也有可能是非普通班
  639. // 找这个班级的同批次
  640. const tClassBatch = await this.query({ termid, batchid });
  641. const r = tClassBatch.find(f => ObjectId(tClass._id).equals(f._id));
  642. const ri = tClassBatch.findIndex(f => ObjectId(tClass._id).equals(f._id));
  643. // TODO 特殊班需要判断,如果没有就没有
  644. // if (r) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '无法确定班级批次排序');
  645. if (!r) {
  646. const { batch: tAllClassBatch } = r;
  647. const templateBatchList = templateList.filter(f => f.batch === tAllClassBatch);
  648. // 根据该班级所在批次的顺序,找到对应模板,然后复制
  649. const copyTemplate = templateBatchList[ri];
  650. const { jslocationid, kbyslocationid, kzjhlocationid, yclocationid } = copyTemplate;
  651. if (!tClass.jslocation && jslocationid) tClass.jslocationid = jslocationid;
  652. if (!tClass.kbyslocationid && kbyslocationid) tClass.kbyslocationid = kbyslocationid;
  653. if (!tClass.kzjhlocationid && kzjhlocationid) tClass.kzjhlocationid = kzjhlocationid;
  654. if (!tClass.yclocationid && yclocationid) tClass.yclocationid = yclocationid;
  655. if (!tClass.headteacherid && headteacherid) tClass.headteacherid = headteacherid;
  656. await tClass.save();
  657. }
  658. }
  659. }
  660. }
  661. module.exports = ClassService;