trainplan.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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, school } = 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 (school) {
  50. trainplan.school = school;
  51. }
  52. if (festivals) {
  53. trainplan.festivals = festivals;
  54. }
  55. if (status === '1') {
  56. trainplan.status = status;
  57. }
  58. // 如果培训计划状态改为发布,发送培训计划信息,并自动生成班级
  59. const res = await trainplan.save();
  60. if (res) {
  61. if (status === '1') {
  62. // 自动生成班级
  63. await this.autoclass(res, trainplanold);
  64. // 将生成的班级重新将班级排班名
  65. await this.autoclassname(res);
  66. // 发送培训计划信息通知给相应人员
  67. // 查询所有入库的教师
  68. const teachers = await this.tmodel.find({ status: '4' });
  69. for (const teacher of teachers) {
  70. const teacherid = teacher._id;
  71. const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
  72. const openid = _teacher.openid;
  73. const detail = trainplan.title + '已发布,请注意查收!';
  74. const date = await this.ctx.service.util.updatedate();
  75. const remark = '感谢您的使用';
  76. if (openid) {
  77. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  78. }
  79. }
  80. // 查询所有学校用户
  81. const schools = await this.umodel.find({ type: '2' });
  82. for (const school of schools) {
  83. const openid = school.openid;
  84. const detail = trainplan.title + '已发布,请注意查收!';
  85. const date = await this.ctx.service.util.updatedate();
  86. const remark = '感谢您的使用';
  87. if (openid) {
  88. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  89. }
  90. }
  91. }
  92. }
  93. return res;
  94. }
  95. // 自动生成班级私有方法
  96. async autoclass(res, trainplanold) {
  97. // 首先比较当前数据和原数据的值是否有不同
  98. // 保存后所有期id
  99. const tremid_res = _.map(res.termnum, 'id');
  100. // 保存前所有期id
  101. const tremid_old = _.map(trainplanold.termnum, 'id');
  102. // 取得要删除的期id,进行班级中删除已删除期的班级
  103. const deltrem = _.difference(tremid_old, tremid_res);
  104. // 循环删除已经删除期的所有班级
  105. for (const elm of deltrem) {
  106. await this.clamodel.deleteMany({ termid: elm });
  107. }
  108. // 取得所有新加期id
  109. const addtrem = _.difference(tremid_res, tremid_old);
  110. // 清空后循环取得所有期进行批次操作
  111. const terms = res.termnum;
  112. for (const el of terms) {
  113. // 判断是否新加期
  114. if (_.indexOf(addtrem, el.id) !== -1) {
  115. // 循环当前新加期的批次列表,根据批次id和班级数生成班级信息
  116. const batchnums = el.batchnum;
  117. for (const batchnum of batchnums) {
  118. // 取得当前批次的班级数
  119. const classnum = batchnum.class;
  120. for (let i = 1; i <= classnum; i++) {
  121. const newdata = { name: i, number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  122. await this.clamodel.create(newdata);
  123. }
  124. }
  125. } else {
  126. // 不是新加期,更新期信息
  127. // 保存后所有期id
  128. const batchid_res = _.map(el.batchnum, 'id');
  129. // 保存前所有期id
  130. const batchid_old = _.map(trainplanold.termnum.id(el.id).batchnum, 'id');
  131. // 取得要删除的期id,进行班级中删除已删除期的班级
  132. const delbatchs = _.difference(batchid_old, batchid_res);
  133. // 循环删除已经删除期的所有班级
  134. for (const delba of delbatchs) {
  135. await this.clamodel.deleteMany({ termid: el.id, batchid: delba });
  136. }
  137. // 取得所有新加期id
  138. const addbatch = _.difference(batchid_res, batchid_old);
  139. const batchnums = el.batchnum;
  140. for (const batchnum of batchnums) {
  141. // 取得当前批次是否有删除
  142. // 判断是否新加期
  143. if (_.indexOf(addbatch, batchnum.id) !== -1) {
  144. // 取得当前批次的班级数
  145. const classnum = batchnum.class;
  146. for (let i = 1; i <= classnum; i++) {
  147. const newdata = { name: i, number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  148. await this.clamodel.create(newdata);
  149. }
  150. } else {
  151. if (batchnum.class === trainplanold.termnum.id(el.id).batchnum.id(batchnum.id).class) {
  152. // 编辑只会针对班级人数进行修改。
  153. const _class = await this.clamodel.find({ termid: el.id, batchid: batchnum.id });
  154. if (_class.length !== 0) {
  155. for (const ee of _class) {
  156. ee.number = batchnum.number;
  157. await ee.save();
  158. }
  159. } else {
  160. const classnum = batchnum.class;
  161. for (let i = 1; i <= classnum; i++) {
  162. const newdata = { name: i, number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  163. await this.clamodel.create(newdata);
  164. }
  165. }
  166. } else {
  167. // 当班级数有更改时
  168. // 删除所有班级 并重新生成班级
  169. await this.clamodel.deleteMany({ termid: el.id, batchid: batchnum.id });
  170. const classnum = batchnum.class;
  171. for (let i = 1; i <= classnum; i++) {
  172. const newdata = { name: i, number: batchnum.number, batchid: batchnum.id, termid: el.id, planid: res.id };
  173. await this.clamodel.create(newdata);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. // 将分好的班级重新编排名字
  182. async autoclassname(res) {
  183. // 取得所有期id
  184. const tremid_res = _.map(res.termnum, 'id');
  185. for (const termid of tremid_res) {
  186. const classs = await this.clamodel.find({ planid: res.id, termid });
  187. let i = 0;
  188. for (const cla of classs) {
  189. i = i + 1;
  190. cla.name = i;
  191. await cla.save();
  192. }
  193. }
  194. }
  195. }
  196. module.exports = TrainplanService;