class.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. const { planid, termid } = data;
  22. assert(planid, '计划id为必填项');
  23. assert(termid, '期id为必填项');
  24. // 先自动生成班级
  25. await this.autoclass(planid, termid);
  26. // 根据计划id与期id查询所有批次下的班级
  27. const newclass = await this.model.find({ planid, termid });
  28. if (!newclass) {
  29. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  30. }
  31. // 根据计划和期查询所有上报的学生 并按照学校排序
  32. const newstudent = await this.stumodel.find({ termid }).sort({ schid: 1 });
  33. if (!newstudent) {
  34. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
  35. }
  36. let _students = _.map(newstudent, 'id');
  37. for (const _class of newclass) {
  38. // 取得班级人数
  39. const stunum = _class.number;
  40. // 取出相同类型的学生
  41. const studentids = await this.getstutype(_students, _class.type);
  42. // 取出班级人数下的学生id
  43. const beforestu = _.take(studentids, stunum);
  44. // 将取出的数据调用更新学生的班级信息方法
  45. await this.studentup(_class.id, _class.batchid, beforestu);
  46. // 将班级自动分为组
  47. await this.groupcreate(termid, _class.batchid, _class.id);
  48. // 取出的学生数据从原数据中删除
  49. _students = _.difference(_students, beforestu);
  50. }
  51. // 判断学生是否有剩余
  52. const stulen_ = _students.length;
  53. if (stulen_ > 0) {
  54. // 判断共有多少班级
  55. // 判断学生数与班级数,当小于或等于班级数 将班级直接分配给学生
  56. const classlength = newclass.length;
  57. // 循环取得学生id并分配班级
  58. let cindex_ = 0;
  59. for (const stuid of _students) {
  60. const student = await this.stumodel.findById(stuid);
  61. if (student) {
  62. const cla_ = newclass[cindex_];
  63. student.classid = cla_.classid;
  64. student.batchid = cla_.batchid;
  65. await student.save();
  66. cindex_ = cindex_ + 1;
  67. if (cindex_ === classlength - 1) {
  68. cindex_ = 0;
  69. }
  70. }
  71. }
  72. }
  73. // 新添,给学生排序号
  74. const claList = await this.model.find({ termid });
  75. for (const cla of claList) {
  76. await this.ctx.service.student.arrangeNumber({ classid: cla._id });
  77. }
  78. }
  79. // 取得同样类型的学生
  80. async getstutype(_students, type) {
  81. const data = [];
  82. for (const stuid of _students) {
  83. const student = await this.stumodel.findById(stuid);
  84. if (student && student.type === type) {
  85. data.push(stuid);
  86. }
  87. }
  88. return data;
  89. }
  90. // 自动生成班级私有方法
  91. async autoclass(planid, termid) {
  92. // 删除所有计划下的班级
  93. await this.model.deleteMany({ planid, termid });
  94. // 根据批次id取得当前批次具体信息
  95. const res = await this.tmodel.findById(planid);
  96. if (!res) {
  97. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  98. }
  99. // 循环出所有班级进行添加操作
  100. const term = await res.termnum.id(termid);
  101. for (const batch of term.batchnum) {
  102. const classs = await batch.class;
  103. for (const cla of classs) {
  104. const newdata = { name: cla.name, number: cla.number, batchid: batch.id, termid: term.id, planid: res.id, type: cla.type };
  105. const rescla = await this.model.create(newdata);
  106. await this.toSetClassSetting({ classid: rescla._id });
  107. }
  108. }
  109. }
  110. // 根据传入的学生列表和班级id更新学生信息
  111. async studentup(classid, batchid, beforestu) {
  112. // 循环学生id
  113. for (const stuid of beforestu) {
  114. const student = await this.stumodel.findById(stuid);
  115. if (student) {
  116. student.classid = classid;
  117. student.batchid = batchid;
  118. await student.save();
  119. }
  120. }
  121. }
  122. // 自动分组
  123. async groupcreate(termid, batchid, classid) {
  124. const group = await this.gmodel.find({ termid, batchid, classid });
  125. if (group.length === 0) {
  126. for (let i = 1; i < 8; i++) {
  127. const name = i + '组';
  128. const newdata = { name, termid, batchid, classid };
  129. await this.gmodel.create(newdata);
  130. }
  131. }
  132. }
  133. // 根据传入的学生列表和班级id更新学生信息
  134. async studentupclass({ id }, data) {
  135. assert(id, '班级id为必填项');
  136. // 根据全年计划表id查出对应的全年计划详细信息
  137. const trainplan = await this.tmodel.findOne({ 'termnum.batchnum.class._id': ObjectId(id) });
  138. if (!trainplan) {
  139. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  140. }
  141. // 取得计划期批次信息
  142. let termid = '';
  143. let batchid = '';
  144. let classname = '';
  145. let class_ = {};
  146. for (const term of trainplan.termnum) {
  147. for (const batch of term.batchnum) {
  148. const _class = await batch.class.id(id);
  149. if (_class) {
  150. termid = term.id;
  151. batchid = batch.id;
  152. classname = _class.name;
  153. class_ = _class;
  154. break;
  155. }
  156. }
  157. }
  158. if (!class_) {
  159. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  160. }
  161. let classid_ = '';
  162. if (classname) {
  163. const cla_ = await this.model.findOne({ termid, batchid, name: classname });
  164. if (cla_) {
  165. classid_ = cla_.id;
  166. } else {
  167. const newdata = {
  168. name: class_.name,
  169. number: class_.number,
  170. batchid,
  171. termid,
  172. planid: trainplan.id,
  173. type: class_.type,
  174. headteacherid: class_.headteacherid,
  175. };
  176. const rescla = await this.model.create(newdata);
  177. if (rescla) {
  178. classid_ = rescla.id;
  179. }
  180. }
  181. }
  182. if (classid_) {
  183. // 循环学生id
  184. for (const stuid of data) {
  185. const student = await this.stumodel.findById(stuid);
  186. if (student) {
  187. student.classid = classid_;
  188. await student.save();
  189. }
  190. }
  191. }
  192. // 添加,给学生排序号
  193. await this.ctx.service.student.arrangeNumber({ classid: classid_ });
  194. // TODO 根据模板复制班级信息
  195. await this.toSetClassSetting({ classid: classid_ });
  196. }
  197. async notice(data) {
  198. for (const classid of data.classids) {
  199. // 根据班级id找到需要通知的班级
  200. const _class = await this.model.findById(classid);
  201. const { headteacherid } = _class;
  202. // 根据班级id找到对应的课程表
  203. const lesson = await this.lessmodel.findOne({ classid });
  204. if (lesson) {
  205. const lessons = lesson.lessons;
  206. const remark = '感谢您的使用';
  207. const date = await this.ctx.service.util.updatedate();
  208. const detail = '班级各项信息已确认,请注意查收';
  209. // 遍历班级授课教师发送通知
  210. for (const lessoninfo of lessons) {
  211. const teaid = lessoninfo.teaid;
  212. const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
  213. if (_teacher) {
  214. const teaopenid = _teacher.openid;
  215. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  216. }
  217. }
  218. // 给班主任发送通知
  219. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  220. if (_headteacher) {
  221. const headteaopenid = _headteacher.openid;
  222. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  223. }
  224. // 根据班级的期id查询对应的培训计划
  225. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  226. const term = await trainplan.termnum.id(_class.termid);
  227. const batch = await term.batchnum.id(_class.batchid);
  228. const startdate = batch.startdate;
  229. const classname = _class.name;
  230. // 给班级所有学生发送邮件通知
  231. const students = await this.stumodel.find({ classid });
  232. for (const student of students) {
  233. const { email, name } = student;
  234. const subject = '吉林省高等学校毕业生就业指导中心通知';
  235. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  236. this.ctx.service.util.sendMail(email, subject, text);
  237. }
  238. }
  239. }
  240. }
  241. async uptea(data) {
  242. for (const _data of data) {
  243. const classInfo = await this.model.findById(_data.id);
  244. classInfo.headteacherid = _data.headteacherid;
  245. await classInfo.save();
  246. }
  247. }
  248. async query({ skip, limit, ...info }) {
  249. const classes = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  250. const data = [];
  251. for (const _class of classes) {
  252. const classInfo = await this.fetch({ id: _class.id });
  253. data.push(classInfo);
  254. }
  255. return data;
  256. }
  257. async fetch({ id }) {
  258. const classInfo = _.cloneDeep(JSON.parse(JSON.stringify(await this.model.findById(id))));
  259. const trainplan = await this.tmodel.findById(classInfo.planid);
  260. if (trainplan) {
  261. const term = _.filter(trainplan.termnum, item => item.id === classInfo.termid);
  262. if (term.length > 0) {
  263. classInfo.term = term[0].term;
  264. const batch = _.filter(term[0].batchnum, item => item.id === classInfo.batchid);
  265. if (batch.length > 0) {
  266. classInfo.batch = batch[0].batch;
  267. classInfo.startdate = batch[0].startdate;
  268. classInfo.enddate = batch[0].enddate;
  269. }
  270. }
  271. }
  272. if (classInfo.yclocationid) {
  273. classInfo.yclocation = (await this.locamodel.findById(classInfo.yclocationid)).name;
  274. }
  275. if (classInfo.kzjhlocationid) {
  276. classInfo.kzjhlocation = (await this.locamodel.findById(classInfo.kzjhlocationid)).name;
  277. }
  278. if (classInfo.kbyslocationid) {
  279. classInfo.kbyslocation = (await this.locamodel.findById(classInfo.kbyslocationid)).name;
  280. }
  281. if (classInfo.jslocationid) {
  282. classInfo.jslocation = (await this.locamodel.findById(classInfo.jslocationid)).name;
  283. }
  284. if (classInfo.headteacherid) {
  285. classInfo.headteacher = (await this.heamodel.findById(classInfo.headteacherid)).name;
  286. }
  287. if (classInfo.lyteacherid) {
  288. let res = await this.teamodel.findById(classInfo.lyteacherid);
  289. if (!res) res = await this.heamodel.findById(classInfo.lyteacherid);
  290. if (res)classInfo.lyteacher = res.name;
  291. }
  292. return classInfo;
  293. }
  294. async upclasses(data) {
  295. for (const _data of data) {
  296. await this.model.findByIdAndUpdate(_data.id, _data);
  297. }
  298. }
  299. async classinfo({ id: classid }) {
  300. const _classes = await this.model.findById(classid);
  301. // 班级信息
  302. const classes = _.cloneDeep(JSON.parse(JSON.stringify(_classes)));
  303. // 学生信息
  304. const students = await this.stumodel.find({ classid });
  305. // 所有用户信息
  306. const users = await this.umodel.find();
  307. if (students) {
  308. for (const stu of students) {
  309. const user = users.find(item => item.uid === stu.id);
  310. if (user && user.openid) {
  311. const _stu = _.cloneDeep(JSON.parse(JSON.stringify(stu)));
  312. _stu.hasuserinfo = '1';
  313. _.remove(students, stu);
  314. students.push(_stu);
  315. }
  316. }
  317. classes.students = students;
  318. }
  319. // 班主任信息
  320. let headteacher;
  321. if (classes.headteacherid) {
  322. headteacher = await this.heamodel.findById(classes.headteacherid);
  323. }
  324. // 礼仪课老师信息
  325. let lyteacher;
  326. if (classes.lyteacherid) {
  327. lyteacher = await this.heamodel.findById(classes.lyteacherid);
  328. if (!lyteacher) {
  329. lyteacher = await this.teamodel.findById(classes.lyteacherid);
  330. }
  331. }
  332. // 教课老师信息
  333. let teachers = [];
  334. const lessones = await this.lessmodel.findOne({ classid });
  335. if (lessones) {
  336. for (const lesson of lessones.lessons) {
  337. if (lesson.teaid) {
  338. const teacher = await this.teamodel.findById(lesson.teaid);
  339. teachers.push(teacher);
  340. }
  341. }
  342. }
  343. teachers.push(lyteacher);
  344. teachers.push(headteacher);
  345. teachers = _.uniq(_.compact(teachers));
  346. for (const tea of teachers) {
  347. const user = users.find(item => item.uid === tea.id);
  348. console.log(user);
  349. if (user && user.openid) {
  350. const _tea = _.cloneDeep(JSON.parse(JSON.stringify(tea)));
  351. _tea.hasuserinfo = '1';
  352. _.remove(teachers, tea);
  353. teachers.push(_tea);
  354. }
  355. }
  356. classes.teachers = teachers;
  357. return classes;
  358. }
  359. // 根据模板设置班级信息
  360. async toSetClassSetting({ classid }) {
  361. const setting = await this.ctx.model.Setting.findOne();
  362. if (!setting) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到系统设置');
  363. const { template_term } = setting;
  364. if (!template_term) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级模板设置');
  365. const templateList = await this.query({ termid: template_term });
  366. const tClass = await this.model.findById(classid);
  367. if (!tClass) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级不存在,无法复制设定的班级设置');
  368. const { name, termid, batchid } = tClass;
  369. const r = templateList.find(f => f.name === name);
  370. if (r) {
  371. // 说明这个是正常班,且从模板中找得到; 除了礼仪教师外,都复制过来
  372. const { jslocationid, kbyslocationid, kzjhlocationid, yclocationid } = r;
  373. if (!tClass.jslocation && jslocationid) tClass.jslocationid = jslocationid;
  374. if (!tClass.kbyslocationid && kbyslocationid) tClass.kbyslocationid = kbyslocationid;
  375. if (!tClass.kzjhlocationid && kzjhlocationid) tClass.kzjhlocationid = kzjhlocationid;
  376. if (!tClass.yclocationid && yclocationid) tClass.yclocationid = yclocationid;
  377. await tClass.save();
  378. } else {
  379. // 没找到,有可能是普通班,也有可能是非普通班
  380. // 找这个班级的同批次
  381. const tClassBatch = await this.query({ termid, batchid });
  382. const r = tClassBatch.find(f => ObjectId(tClass._id).equals(f._id));
  383. const ri = tClassBatch.findIndex(f => ObjectId(tClass._id).equals(f._id));
  384. if (r) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '无法确定班级批次排序');
  385. else {
  386. const { batch: tAllClassBatch } = r;
  387. const templateBatchList = templateList.filter(f => f.batch === tAllClassBatch);
  388. // 根据该班级所在批次的顺序,找到对应模板,然后复制
  389. const copyTemplate = templateBatchList[ri];
  390. const { jslocationid, kbyslocationid, kzjhlocationid, yclocationid } = copyTemplate;
  391. if (!tClass.jslocation && jslocationid) tClass.jslocationid = jslocationid;
  392. if (!tClass.kbyslocationid && kbyslocationid) tClass.kbyslocationid = kbyslocationid;
  393. if (!tClass.kzjhlocationid && kzjhlocationid) tClass.kzjhlocationid = kzjhlocationid;
  394. if (!tClass.yclocationid && yclocationid) tClass.yclocationid = yclocationid;
  395. await tClass.save();
  396. }
  397. }
  398. }
  399. }
  400. module.exports = ClassService;