class.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. let yclocationid = ''; // 用餐地点
  102. let kzjhlocationid = ''; // 拓展计划地点
  103. let kbyslocationid = ''; // 开班仪式地点
  104. let jslocationid = ''; // 教室位置
  105. const locations = await this.locamodel.find();
  106. const jslocationids = _.filter(locations, { type: '0' });
  107. const kbyslocationids = _.filter(locations, { type: '1' });
  108. const kzjhlocationids = _.filter(locations, { type: '2' });
  109. const yclocationids = _.filter(locations, { type: '3' });
  110. for (const batch of term.batchnum) {
  111. const classs = await batch.class;
  112. for (const cla of classs) {
  113. // 取得班级各个地点
  114. // 同一地点同期只能使用一次
  115. if (jslocationids.length > 0) {
  116. jslocationid = jslocationids[0].id;
  117. _.remove(jslocationids, { id: jslocationid });
  118. }
  119. if (kbyslocationids.length > 0) {
  120. kbyslocationid = kbyslocationids[0].id;
  121. _.remove(kbyslocationids, { id: kbyslocationid });
  122. }
  123. if (kzjhlocationids.length > 0) {
  124. kzjhlocationid = kzjhlocationids[0].id;
  125. _.remove(kzjhlocationids, { id: kzjhlocationid });
  126. }
  127. if (yclocationids.length > 0) {
  128. yclocationid = yclocationids[0].id;
  129. _.remove(yclocationids, { id: yclocationid });
  130. }
  131. const newdata = { name: cla.name, number: cla.number, batchid: batch.id, termid: term.id, planid: res.id, type: cla.type, headteacherid: cla.headteacherid, jslocationid, kbyslocationid, kzjhlocationid, yclocationid };
  132. await this.model.create(newdata);
  133. }
  134. }
  135. }
  136. // 根据传入的学生列表和班级id更新学生信息
  137. async studentup(classid, batchid, beforestu) {
  138. // 循环学生id
  139. for (const stuid of beforestu) {
  140. const student = await this.stumodel.findById(stuid);
  141. if (student) {
  142. student.classid = classid;
  143. student.batchid = batchid;
  144. await student.save();
  145. }
  146. }
  147. }
  148. // 自动分组
  149. async groupcreate(termid, batchid, classid) {
  150. const group = await this.gmodel.find({ termid, batchid, classid });
  151. if (group.length === 0) {
  152. for (let i = 1; i < 8; i++) {
  153. const name = i + '组';
  154. const newdata = { name, termid, batchid, classid };
  155. await this.gmodel.create(newdata);
  156. }
  157. }
  158. }
  159. // 根据传入的学生列表和班级id更新学生信息
  160. async studentupclass({ id }, data) {
  161. assert(id, '班级id为必填项');
  162. // 根据全年计划表id查出对应的全年计划详细信息
  163. const trainplan = await this.tmodel.findOne({ 'termnum.batchnum.class._id': ObjectId(id) });
  164. if (!trainplan) {
  165. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  166. }
  167. // 取得计划期批次信息
  168. let termid = '';
  169. let batchid = '';
  170. let classname = '';
  171. let class_ = {};
  172. for (const term of trainplan.termnum) {
  173. for (const batch of term.batchnum) {
  174. const _class = await batch.class.id(id);
  175. if (_class) {
  176. termid = term.id;
  177. batchid = batch.id;
  178. classname = _class.name;
  179. class_ = _class;
  180. break;
  181. }
  182. }
  183. }
  184. if (!class_) {
  185. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
  186. }
  187. let classid_ = '';
  188. if (classname) {
  189. const cla_ = await this.model.findOne({ termid, batchid, name: classname });
  190. if (cla_) {
  191. classid_ = cla_.id;
  192. } else {
  193. const newdata = {
  194. name: class_.name,
  195. number: class_.number,
  196. batchid,
  197. termid,
  198. planid: trainplan.id,
  199. type: class_.type,
  200. };
  201. const rescla = await this.model.create(newdata);
  202. if (rescla) {
  203. classid_ = rescla.id;
  204. }
  205. }
  206. }
  207. if (classid_) {
  208. // 循环学生id
  209. for (const stuid of data) {
  210. const student = await this.stumodel.findById(stuid);
  211. if (student) {
  212. student.classid = classid_;
  213. await student.save();
  214. }
  215. }
  216. }
  217. // 添加,给学生排序号
  218. await this.ctx.service.student.arrangeNumber({ classid: classid_ });
  219. }
  220. async notice(data) {
  221. for (const classid of data.classids) {
  222. // 根据班级id找到需要通知的班级
  223. const _class = await this.model.findById(classid);
  224. const { headteacherid } = _class;
  225. // 根据班级id找到对应的课程表
  226. const lesson = await this.lessmodel.findOne({ classid });
  227. if (lesson) {
  228. const lessons = lesson.lessons;
  229. const remark = '感谢您的使用';
  230. const date = await this.ctx.service.util.updatedate();
  231. const detail = '班级各项信息已确认,请注意查收';
  232. // 遍历班级授课教师发送通知
  233. for (const lessoninfo of lessons) {
  234. const teaid = lessoninfo.teaid;
  235. const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
  236. if (_teacher) {
  237. const teaopenid = _teacher.openid;
  238. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
  239. }
  240. }
  241. // 给班主任发送通知
  242. const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  243. if (_headteacher) {
  244. const headteaopenid = _headteacher.openid;
  245. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
  246. }
  247. // 根据班级的期id查询对应的培训计划
  248. const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
  249. const term = await trainplan.termnum.id(_class.termid);
  250. const batch = await term.batchnum.id(_class.batchid);
  251. const startdate = batch.startdate;
  252. const classname = _class.name;
  253. // 给班级所有学生发送邮件通知
  254. const students = await this.stumodel.find({ classid });
  255. for (const student of students) {
  256. const { email, name } = student;
  257. const subject = '吉林省高等学校毕业生就业指导中心通知';
  258. const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
  259. this.ctx.service.util.sendMail(email, subject, text);
  260. }
  261. }
  262. }
  263. }
  264. async uptea(data) {
  265. for (const _data of data) {
  266. const classInfo = await this.model.findById(_data.id);
  267. classInfo.headteacherid = _data.headteacherid;
  268. await classInfo.save();
  269. }
  270. }
  271. async query({ skip, limit, ...info }) {
  272. const classes = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  273. const data = [];
  274. for (const _class of classes) {
  275. const classInfo = await this.fetch({ id: _class.id });
  276. data.push(classInfo);
  277. }
  278. return data;
  279. }
  280. async fetch({ id }) {
  281. const classInfo = _.cloneDeep(JSON.parse(JSON.stringify(await this.model.findById(id))));
  282. const trainplan = await this.tmodel.findById(classInfo.planid);
  283. if (trainplan) {
  284. const term = _.filter(trainplan.termnum, item => item.id === classInfo.termid);
  285. if (term.length > 0) {
  286. classInfo.term = term[0].term;
  287. const batch = _.filter(term[0].batchnum, item => item.id === classInfo.batchid);
  288. if (batch.length > 0) {
  289. classInfo.batch = batch[0].batch;
  290. classInfo.startdate = batch[0].startdate;
  291. classInfo.enddate = batch[0].enddate;
  292. }
  293. }
  294. }
  295. if (classInfo.yclocationid) {
  296. classInfo.yclocation = (await this.locamodel.findById(classInfo.yclocationid)).name;
  297. }
  298. if (classInfo.kzjhlocationid) {
  299. classInfo.kzjhlocation = (await this.locamodel.findById(classInfo.kzjhlocationid)).name;
  300. }
  301. if (classInfo.kbyslocationid) {
  302. classInfo.kbyslocation = (await this.locamodel.findById(classInfo.kbyslocationid)).name;
  303. }
  304. if (classInfo.jslocationid) {
  305. classInfo.jslocation = (await this.locamodel.findById(classInfo.jslocationid)).name;
  306. }
  307. if (classInfo.headteacherid) {
  308. classInfo.headteacher = (await this.heamodel.findById(classInfo.headteacherid)).name;
  309. }
  310. if (classInfo.lyteacherid) {
  311. let res = await this.teamodel.findById(classInfo.lyteacherid);
  312. if (!res) res = await this.heamodel.findById(classInfo.lyteacherid);
  313. if (res)classInfo.lyteacher = res.name;
  314. }
  315. return classInfo;
  316. }
  317. async upclasses(data) {
  318. for (const _data of data) {
  319. await this.model.findByIdAndUpdate(_data.id, _data);
  320. }
  321. }
  322. async classinfo({ id: classid }) {
  323. const _classes = await this.model.findById(classid);
  324. // 班级信息
  325. const classes = _.cloneDeep(JSON.parse(JSON.stringify(_classes)));
  326. // 学生信息
  327. const students = await this.stumodel.find({ classid });
  328. // 所有用户信息
  329. const users = await this.umodel.find();
  330. if (students) {
  331. for (const stu of students) {
  332. const user = users.find(item => item.uid === stu.id);
  333. if (user && user.openid) {
  334. const _stu = _.cloneDeep(JSON.parse(JSON.stringify(stu)));
  335. _stu.hasuserinfo = '1';
  336. _.remove(students, stu);
  337. students.push(_stu);
  338. }
  339. }
  340. classes.students = students;
  341. }
  342. // 班主任信息
  343. let headteacher;
  344. if (classes.headteacherid) {
  345. headteacher = await this.heamodel.findById(classes.headteacherid);
  346. }
  347. // 礼仪课老师信息
  348. let lyteacher;
  349. if (classes.lyteacherid) {
  350. lyteacher = await this.heamodel.findById(classes.lyteacherid);
  351. if (!lyteacher) {
  352. lyteacher = await this.teamodel.findById(classes.lyteacherid);
  353. }
  354. }
  355. // 教课老师信息
  356. let teachers = [];
  357. const lessones = await this.lessmodel.findOne({ classid });
  358. if (lessones) {
  359. for (const lesson of lessones.lessons) {
  360. if (lesson.teaid) {
  361. const teacher = await this.teamodel.findById(lesson.teaid);
  362. teachers.push(teacher);
  363. }
  364. }
  365. }
  366. teachers.push(lyteacher);
  367. teachers.push(headteacher);
  368. teachers = _.uniq(_.compact(teachers));
  369. for (const tea of teachers) {
  370. const user = users.find(item => item.uid === tea.id);
  371. console.log(user);
  372. if (user && user.openid) {
  373. const _tea = _.cloneDeep(JSON.parse(JSON.stringify(tea)));
  374. _tea.hasuserinfo = '1';
  375. _.remove(teachers, tea);
  376. teachers.push(_tea);
  377. }
  378. }
  379. classes.teachers = teachers;
  380. return classes;
  381. }
  382. }
  383. module.exports = ClassService;