Ver Fonte

加入判断机制

liuyu há 5 anos atrás
pai
commit
bbf25fcccd
1 ficheiros alterados com 35 adições e 28 exclusões
  1. 35 28
      app/service/class.js

+ 35 - 28
app/service/class.js

@@ -65,34 +65,41 @@ class ClassService extends CrudService {
       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);
+      if (lesson) {
+        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' });
+          if (_teacher) {
+            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' });
+        if (_headteacher) {
+          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);
+        }
       }
     }
   }