trainplan.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 TrainplanService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'trainplan');
  10. this.model = this.ctx.model.Trainplan;
  11. this.clamodel = this.ctx.model.Class;
  12. this.umodel = this.ctx.model.User;
  13. this.smodel = this.ctx.model.School;
  14. this.tmodel = this.ctx.model.Teacher;
  15. }
  16. // async create(data) {
  17. // const terminfo = await data.termnum;
  18. // console.log(terminfo);
  19. // const { batchnum: { type, name, number }, term } = terminfo;
  20. // console.log(type);
  21. // if (type === 1) {
  22. // const classdata = { name, number, term, type };
  23. // await this.clamodel.create(classdata);
  24. // }
  25. // if (type === 0) {
  26. // for (let i = 0; i < class; i++) {
  27. // const name = '第' + term + '期' + batch + '批次' + i + '班';
  28. // const classdate = { name, number, term, type, newbatch };
  29. // await this.clamodel.create(classdate);
  30. // }
  31. // }
  32. // return this.tpmodel.create(data);
  33. // }
  34. // }
  35. async update({ id }, data) {
  36. const trainplan = await this.model.findById(id);
  37. // 保存原数据
  38. const trainplanold = _.cloneDeep(trainplan);
  39. const { year, title, termnum, festivals, status } = data;
  40. if (year) {
  41. trainplan.year = year;
  42. }
  43. if (title) {
  44. trainplan.title = title;
  45. }
  46. if (termnum) {
  47. trainplan.termnum = termnum;
  48. }
  49. if (festivals) {
  50. trainplan.festivals = festivals;
  51. }
  52. if (status === '1') {
  53. trainplan.status = status;
  54. }
  55. // 如果培训计划状态改为发布,发送培训计划信息,并自动生成班级
  56. const res = await trainplan.save();
  57. if (res) {
  58. if (status === '1') {
  59. // 自动生成班级
  60. await this.autoclass(res, trainplanold);
  61. // 发送培训计划信息通知给相应人员
  62. // 查询所有入库的教师
  63. const teachers = await this.tmodel.find({ status: '4' });
  64. for (const teacher of teachers) {
  65. const teacherid = teacher._id;
  66. const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
  67. const openid = _teacher.openid;
  68. const detail = trainplan.title + '已发布,请注意查收!';
  69. const date = await this.ctx.service.util.updatedate();
  70. const remark = '感谢您的使用';
  71. if (openid) {
  72. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  73. }
  74. }
  75. // 查询所有学校用户
  76. const schools = await this.umodel.find({ type: '2' });
  77. for (const school of schools) {
  78. const openid = school.openid;
  79. const detail = trainplan.title + '已发布,请注意查收!';
  80. const date = await this.ctx.service.util.updatedate();
  81. const remark = '感谢您的使用';
  82. if (openid) {
  83. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  84. }
  85. }
  86. }
  87. }
  88. return res;
  89. }
  90. // 自动生成班级私有方法
  91. async autoclass(res, trainplanold) {
  92. // 首先比较当前数据和原数据的值是否有不同
  93. // 保存后所有期id
  94. const tremid_res = _.map(res.termnum, 'id');
  95. // 保存前所有期id
  96. const tremid_old = _.map(trainplanold.termnum, 'id');
  97. // 取得要删除的期id,进行班级中删除已删除期的班级
  98. const deltrem = _.difference(tremid_old, tremid_res);
  99. // 循环删除已经删除期的所有班级
  100. for (const elm of deltrem) {
  101. await this.clamodel.deleteMany({ termid: elm });
  102. }
  103. // 取得所有新加期id
  104. const addtrem = _.difference(tremid_res, tremid_old);
  105. // 清空后循环取得所有期进行批次操作
  106. const terms = res.termnum;
  107. for (const el of terms) {
  108. // 判断是否新加期
  109. if (_.indexOf(addtrem, el.id) !== -1) {
  110. // 循环当前新加期的批次列表,根据批次id和班级数生成班级信息
  111. const batchnums = el.batchnum;
  112. for (const batchnum of batchnums) {
  113. // 取得当前批次的班级数
  114. const classnum = batchnum.class;
  115. for (let i = 1; i <= classnum; i++) {
  116. const newdata = { name: i + '班', number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  117. await this.clamodel.create(newdata);
  118. }
  119. }
  120. } else {
  121. // 不是新加期,更新期信息
  122. // 保存后所有期id
  123. const batchid_res = _.map(el.batchnum, 'id');
  124. // 保存前所有期id
  125. const batchid_old = _.map(trainplanold.termnum.id(el.id).batchnum, 'id');
  126. // 取得要删除的期id,进行班级中删除已删除期的班级
  127. const delbatchs = _.difference(batchid_old, batchid_res);
  128. // 循环删除已经删除期的所有班级
  129. for (const delba of delbatchs) {
  130. await this.clamodel.deleteMany({ termid: el.id, batchid: delba });
  131. }
  132. // 取得所有新加期id
  133. const addbatch = _.difference(batchid_res, batchid_old);
  134. const batchnums = el.batchnum;
  135. for (const batchnum of batchnums) {
  136. // 取得当前批次是否有删除
  137. // 判断是否新加期
  138. if (_.indexOf(addbatch, batchnum.id) !== -1) {
  139. // 取得当前批次的班级数
  140. const classnum = batchnum.class;
  141. for (let i = 1; i <= classnum; i++) {
  142. const newdata = { name: i + '班', number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  143. await this.clamodel.create(newdata);
  144. }
  145. } else {
  146. if (batchnum.class === trainplanold.termnum.id(el.id).batchnum.id(batchnum.id).class) {
  147. // 编辑只会针对班级人数进行修改。
  148. const _class = await this.clamodel.find({ termid: el.id, batchid: batchnum.id });
  149. if (_class.length !== 0) {
  150. for (const ee of _class) {
  151. ee.number = batchnum.number;
  152. await ee.save();
  153. }
  154. } else {
  155. const classnum = batchnum.class;
  156. for (let i = 1; i <= classnum; i++) {
  157. const newdata = { name: i + '班', number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  158. await this.clamodel.create(newdata);
  159. }
  160. }
  161. } else {
  162. // 当班级数有更改时
  163. // 删除所有班级 并重新生成班级
  164. await this.clamodel.deleteMany({ termid: el.id, batchid: batchnum.id });
  165. const classnum = batchnum.class;
  166. for (let i = 1; i <= classnum; i++) {
  167. const newdata = { name: i + '班', number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  168. await this.clamodel.create(newdata);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. module.exports = TrainplanService;