'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class TrainplanService extends CrudService { constructor(ctx) { super(ctx, 'trainplan'); this.model = this.ctx.model.Trainplan; this.clamodel = this.ctx.model.Class; this.umodel = this.ctx.model.User; this.smodel = this.ctx.model.School; this.tmodel = this.ctx.model.Teacher; } // async create(data) { // const terminfo = await data.termnum; // console.log(terminfo); // const { batchnum: { type, name, number }, term } = terminfo; // console.log(type); // if (type === 1) { // const classdata = { name, number, term, type }; // await this.clamodel.create(classdata); // } // if (type === 0) { // for (let i = 0; i < class; i++) { // const name = '第' + term + '期' + batch + '批次' + i + '班'; // const classdate = { name, number, term, type, newbatch }; // await this.clamodel.create(classdate); // } // } // return this.tpmodel.create(data); // } // } async update({ id }, data) { const trainplan = await this.model.findById(id); const { year, title, termnum, festivals, status } = data; if (year) { trainplan.year = year; } if (title) { trainplan.title = title; } if (termnum) { trainplan.termnum = termnum; } if (festivals) { trainplan.festivals = festivals; } // 如果培训计划状态改为发布 if (status === '1') { // 查询所有入库的教师 const teachers = await this.tmodel.find({ status: '4' }); for (const teacher of teachers) { const teacherid = teacher._id; const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' }); const openid = _teacher.openid; const detail = trainplan.title + '已发布,请注意查收!'; const moment = require('moment'); let date = new Date(); date = moment(date).format('YYYY-MM-DD HH:mm:ss'); const remark = '感谢您的使用'; this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark); } // 查询所有学校用户 const schools = await this.umodel.find({ type: '2' }); for (const school of schools) { const openid = school.openid; const detail = trainplan.title + '已发布,请注意查收!'; const moment = require('moment'); let date = new Date(); date = moment(date).format('YYYY-MM-DD HH:mm:ss'); const remark = '感谢您的使用'; this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark); } } return await trainplan.save(); } } module.exports = TrainplanService;