'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 ClassService extends CrudService { constructor(ctx) { super(ctx, 'class'); this.model = this.ctx.model.Class; this.stumodel = this.ctx.model.Student; this.lessmodel = this.ctx.model.Lesson; this.umodel = this.ctx.model.User; this.tmodel = this.ctx.model.Trainplan; } async divide(data) { const name = await data.classname; const number = await data.students.length; const type = await data.type; const termid = await data.termid; const batchid = await data.batchid; const classdata = { name, batchid, termid, number, type }; // 根据班级名称查询班级是否已存在 const newclass = await this.model.findOne({ name }); // 如果已经存在 if (newclass) { throw new BusinessError(ErrorCode.DATA_EXIST, '班级名称已存在'); // const classid = newclass._id; // const oldstudents = this.stumodel.find({ classid }); // for (const oldstudent of oldstudents) { // oldstudent.classid = null; // } // // 遍历学生id // for (const studentid of data.students) { // // 根据学生id找到学生数据修改其中的class字段 // const student = await this.stumodel.findById(studentid); // student.batchid = batchid; // student.classid = classid; // await student.save(); // } } else { // 如果班级不存在则创建班级 const newdata = await this.model.create(classdata); // 查询班级id const classid = newdata._id; // 遍历学生id for (const studentid of data.students) { // 根据学生id找到学生数据修改其中的class字段 const student = await this.stumodel.findById(studentid); student.classid = classid; student.batchid = batchid; student.termid = termid; await student.save(); } } } async notice(data) { for (const classid of data.classids) { const _class = await this.model.findById(classid); const { headteacherid } = _class; const lesson = await this.lessmodel.findOne({ classid }); const lessons = lesson.lessons; const remark = '感谢您的使用'; const date = await this.ctx.service.util.updatedate(); const detail = '班级各项信息已确认,请注意查收'; // 遍历班级授课教师发送通知 for (const lessoninfo of lessons) { const teaid = lessoninfo.teaid; const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' }); const teaopenid = _teacher.openid; this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid); } // 给班主任发送通知 const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' }); const headteaopenid = _headteacher.openid; this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid); // 根据班级的期id查询对应的培训计划 const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid }); const term = await trainplan.termnum.id(_class.termid); const batch = await term.batchnum.id(_class.batchid); const startdate = batch.startdate; const classname = _class.name; // 给班级所有学生发送邮件通知 const students = await this.stumodel.find({ classid }); for (const student of students) { const { email, name } = student; const subject = '吉林省高等学校毕业生就业指导中心通知'; const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate; this.ctx.service.util.sendMail(email, subject, text); } } } } module.exports = ClassService;