trainplan.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. await this.autoclassNew(res, trainplanold);
  65. // 将生成的班级重新将班级排班名
  66. // await this.autoclassname(res);
  67. // 发送培训计划信息通知给相应人员
  68. // 查询所有入库的教师
  69. const teachers = await this.tmodel.find({ status: '4' });
  70. for (const teacher of teachers) {
  71. const teacherid = teacher._id;
  72. const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
  73. const openid = _teacher.openid;
  74. const detail = trainplan.title + '已发布,请注意查收!';
  75. const date = await this.ctx.service.util.updatedate();
  76. const remark = '感谢您的使用';
  77. if (openid) {
  78. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  79. }
  80. }
  81. // 查询所有学校用户
  82. const schools = await this.umodel.find({ type: '2' });
  83. for (const school of schools) {
  84. const openid = school.openid;
  85. const detail = trainplan.title + '已发布,请注意查收!';
  86. const date = await this.ctx.service.util.updatedate();
  87. const remark = '感谢您的使用';
  88. if (openid) {
  89. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  90. }
  91. }
  92. }
  93. }
  94. return res;
  95. }
  96. // 自动生成班级私有方法
  97. async autoclassNew(res) {
  98. // 删除所有计划下的班级
  99. await this.clamodel.deleteMany({ planid: res.id });
  100. // 循环出所有班级进行添加操作
  101. for (const term of res.termnum) {
  102. for (const batch of term.batchnum) {
  103. const classs = await batch.class;
  104. for (const cla of classs) {
  105. const newdata = { name: cla.name, number: cla.number, batchid: batch.id, termid: term.id, planid: res.id, type: cla.type, headteacherid: cla.headteacherid };
  106. await this.clamodel.create(newdata);
  107. }
  108. }
  109. }
  110. }
  111. // 自动生成班级私有方法
  112. async autoclass(res, trainplanold) {
  113. // 首先比较当前数据和原数据的值是否有不同
  114. // 保存后所有期id
  115. const tremid_res = _.map(res.termnum, 'id');
  116. // 保存前所有期id
  117. const tremid_old = _.map(trainplanold.termnum, 'id');
  118. // 取得要删除的期id,进行班级中删除已删除期的班级
  119. const deltrem = _.difference(tremid_old, tremid_res);
  120. // 循环删除已经删除期的所有班级
  121. for (const elm of deltrem) {
  122. await this.clamodel.deleteMany({ termid: elm });
  123. }
  124. // 取得所有新加期id
  125. const addtrem = _.difference(tremid_res, tremid_old);
  126. // 清空后循环取得所有期进行批次操作
  127. const terms = res.termnum;
  128. for (const el of terms) {
  129. // 判断是否新加期
  130. if (_.indexOf(addtrem, el.id) !== -1) {
  131. // 循环当前新加期的批次列表,根据批次id和班级数生成班级信息
  132. const batchnums = el.batchnum;
  133. for (const batchnum of batchnums) {
  134. // 取得当前批次的班级数
  135. const classnum = batchnum.class;
  136. for (const cla of classnum) {
  137. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  138. await this.clamodel.create(newdata);
  139. }
  140. }
  141. } else {
  142. // 不是新加期,更新期信息
  143. // 保存后所有期id
  144. const batchid_res = _.map(el.batchnum, 'id');
  145. // 保存前所有期id
  146. const batchid_old = _.map(trainplanold.termnum.id(el.id).batchnum, 'id');
  147. // 取得要删除的期id,进行班级中删除已删除期的班级
  148. const delbatchs = _.difference(batchid_old, batchid_res);
  149. // 循环删除已经删除期的所有班级
  150. for (const delba of delbatchs) {
  151. await this.clamodel.deleteMany({ termid: el.id, batchid: delba });
  152. }
  153. // 取得所有新加期id
  154. const addbatch = _.difference(batchid_res, batchid_old);
  155. const batchnums = el.batchnum;
  156. for (const batchnum of batchnums) {
  157. // 取得当前批次是否有删除
  158. // 判断是否新加期
  159. if (_.indexOf(addbatch, batchnum.id) !== -1) {
  160. // 取得当前批次的班级数
  161. const classnum = batchnum.class;
  162. for (const cla of classnum) {
  163. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  164. await this.clamodel.create(newdata);
  165. }
  166. } else {
  167. if (batchnum.class === trainplanold.termnum.id(el.id).batchnum.id(batchnum.id).class) {
  168. // 编辑只会针对班级人数进行修改。
  169. const _class = await this.clamodel.find({ termid: el.id, batchid: batchnum.id });
  170. if (_class.length !== 0) {
  171. for (const ee of _class) {
  172. ee.number = batchnum.number;
  173. await ee.save();
  174. }
  175. } else {
  176. const classnum = batchnum.class;
  177. for (const cla of classnum) {
  178. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  179. await this.clamodel.create(newdata);
  180. }
  181. }
  182. } else {
  183. // 当班级数有更改时
  184. // 删除所有班级 并重新生成班级
  185. await this.clamodel.deleteMany({ termid: el.id, batchid: batchnum.id });
  186. const classnum = batchnum.class;
  187. for (const cla of classnum) {
  188. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  189. await this.clamodel.create(newdata);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. // // 将分好的班级重新编排名字
  198. // async autoclassname(res) {
  199. // // 取得所有期id
  200. // const tremid_res = _.map(res.termnum, 'id');
  201. // for (const termid of tremid_res) {
  202. // const classs = await this.clamodel.find({ planid: res.id, termid });
  203. // let i = 0;
  204. // for (const cla of classs) {
  205. // i = i + 1;
  206. // cla.name = i;
  207. // await cla.save();
  208. // }
  209. // }
  210. // }
  211. async updateclass({ trainplanid, classid, rightHeader }) {
  212. assert(trainplanid && classid && rightHeader, '缺少参数项');
  213. // 根据全年计划表id查出对应的全年计划详细信息
  214. const trainplan = await this.model.findById(trainplanid);
  215. if (!trainplan) {
  216. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  217. }
  218. for (const term of trainplan.termnum) {
  219. for (const batch of term.batchnum) {
  220. const class_ = await batch.class.id(classid);
  221. if (class_) {
  222. class_.headteacherid = rightHeader;
  223. }
  224. }
  225. }
  226. return await trainplan.save();
  227. }
  228. async updatereteacher({ trainplanid, termid, reteacher }) {
  229. assert(trainplanid && termid && reteacher, '缺少参数项');
  230. // 根据全年计划表id查出对应的全年计划详细信息
  231. const trainplan = await this.model.findById(trainplanid);
  232. if (!trainplan) {
  233. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '全年计划信息不存在');
  234. }
  235. const term = await trainplan.termnum.id(termid);
  236. if (term) {
  237. term.reteacher = reteacher;
  238. }
  239. return await trainplan.save();
  240. }
  241. }
  242. module.exports = TrainplanService;