trainplan.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // 查询所有入库的教师
  53. const teachers = await this.tmodel.find({ status: '4' });
  54. for (const teacher of teachers) {
  55. const teacherid = teacher._id;
  56. const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
  57. const openid = _teacher.openid;
  58. const detail = trainplan.title + '已发布,请注意查收!';
  59. const moment = require('moment');
  60. let date = new Date();
  61. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  62. const remark = '感谢您的使用';
  63. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  64. }
  65. // 查询所有学校用户
  66. const schools = await this.umodel.find({ type: '2' });
  67. for (const school of schools) {
  68. const openid = school.openid;
  69. const detail = trainplan.title + '已发布,请注意查收!';
  70. const moment = require('moment');
  71. let date = new Date();
  72. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  73. const remark = '感谢您的使用';
  74. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  75. }
  76. }
  77. return await trainplan.save();
  78. }
  79. }
  80. module.exports = TrainplanService;