trainplan.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. trainplan.year = data.year;
  38. trainplan.title = data.title;
  39. trainplan.termnum = data.termnum;
  40. trainplan.festivals = data.festivals;
  41. if (data.status === '1') {
  42. const teachers = await this.tmodel.find({ status: '4' });
  43. for (const teacher of teachers) {
  44. const openid = teacher.openid;
  45. const detail = data.title + '已发布,请注意查收!';
  46. const moment = require('moment');
  47. let date = new Date();
  48. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  49. const remark = '感谢您的使用';
  50. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  51. }
  52. const schools = await this.umodel.find({ type: '2' });
  53. for (const school of schools) {
  54. const openid = school.openid;
  55. const detail = data.title + '已发布,请注意查收!';
  56. const moment = require('moment');
  57. let date = new Date();
  58. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  59. const remark = '感谢您的使用';
  60. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  61. }
  62. }
  63. }
  64. }
  65. module.exports = TrainplanService;