trainplan.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 moment = require('moment');
  61. let date = new Date();
  62. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  63. const remark = '感谢您的使用';
  64. if (openid) {
  65. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  66. }
  67. }
  68. // 查询所有学校用户
  69. const schools = await this.umodel.find({ type: '2' });
  70. for (const school of schools) {
  71. const openid = school.openid;
  72. const detail = trainplan.title + '已发布,请注意查收!';
  73. const moment = require('moment');
  74. let date = new Date();
  75. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  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. return await trainplan.save();
  83. }
  84. }
  85. module.exports = TrainplanService;