trainplan.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. const { year, title, termnum, festivals, status } = data;
  38. if (year) {
  39. trainplan.year = year;
  40. }
  41. if (title) {
  42. trainplan.title = title;
  43. }
  44. if (termnum) {
  45. trainplan.termnum = termnum;
  46. }
  47. if (festivals) {
  48. trainplan.festivals = festivals;
  49. }
  50. // 如果培训计划状态改为发布
  51. if (status === '1') {
  52. trainplan.status = status;
  53. // 查询所有入库的教师
  54. const teachers = await this.tmodel.find({ status: '4' });
  55. for (const teacher of teachers) {
  56. const teacherid = teacher._id;
  57. const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
  58. const openid = _teacher.openid;
  59. const detail = trainplan.title + '已发布,请注意查收!';
  60. const date = await this.ctx.service.util.updatedate();
  61. const remark = '感谢您的使用';
  62. if (openid) {
  63. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  64. }
  65. }
  66. // 查询所有学校用户
  67. const schools = await this.umodel.find({ type: '2' });
  68. for (const school of schools) {
  69. const openid = school.openid;
  70. const detail = trainplan.title + '已发布,请注意查收!';
  71. const date = await this.ctx.service.util.updatedate();
  72. const remark = '感谢您的使用';
  73. if (openid) {
  74. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  75. }
  76. }
  77. }
  78. return await trainplan.save();
  79. }
  80. }
  81. module.exports = TrainplanService;