lrf402788946 há 4 anos atrás
pai
commit
0cfdf07975
3 ficheiros alterados com 213 adições e 121 exclusões
  1. 2 1
      app/model/notice.js
  2. 211 118
      app/service/lesson.js
  3. 0 2
      app/service/util.js

+ 2 - 1
app/model/notice.js

@@ -8,6 +8,7 @@ const notifiedInfo = new Schema({
   username: { type: String, required: false, maxLength: 200 }, // 通知姓名
   status: { type: String, required: false, maxLength: 200, default: '0' }, // 状态,0-未读,1-已读
   readtime: { type: String, required: false, maxLength: 200 }, // 读取时间
+  content: { type: String, required: false }, // 如果属于自己的信息,就放这里,上面放信息标题
 });
 
 // 通知表
@@ -18,7 +19,7 @@ const NoticeSchema = {
   classid: { type: String, required: false, maxLength: 200 }, // 班级id
   noticeid: { type: String, required: true, maxLength: 200 }, // 通知人id
   content: { type: String, required: true }, // 通知内容
-  type: { type: String, required: false, maxLength: 200, default: '0' }, // 状态,0-培训自定义通知;1-日历通知;2-班主任计划通知;3-学校计划通知;4-课表通知;5-分寝通知
+  type: { type: String, required: false, maxLength: 200, default: '0' }, // 状态,0-培训自定义通知;1-日历通知;2-班主任计划通知;3-学校计划通知;4-课表通知;5-分寝通知;6-预安排课表通知
   notified: { type: [ notifiedInfo ], select: true }, // 被通知信息
 };
 

+ 211 - 118
app/service/lesson.js

@@ -2,6 +2,7 @@
 
 
 const assert = require('assert');
+const { groupEnd } = require('console');
 const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
 const moment = require('moment');
@@ -21,6 +22,7 @@ class LessonService extends CrudService {
     this.headteamodel = this.ctx.model.Headteacher;
     this.umodel = this.ctx.model.User;
     this.nmodel = this.ctx.model.Notice;
+    this.weekList = [ '日', '一', '二', '三', '四', '五', '六' ];
   }
 
   // 自动排课私有方法
@@ -290,146 +292,237 @@ class LessonService extends CrudService {
   async check({ ids }) {
     // 1,修改课表状态; TODO 2,拿到所有的班级,获取所有人员;3,然后发送信息
     const list = await this.model.find({ _id: { $in: ids } });
-    const res = await this.model.updateMany({ _id: { $in: ids } }, { status: '1' });
+    // const res = await this.model.updateMany({ _id: { $in: ids } }, { status: '1' });
+    const noticeList = [];
+    const defaults = {
+      noticeid: 'system',
+      type: '4',
+    };
     // 循环课表
-    for (const lessonInfo of list) {
-      // 获取期数
-      const { termid, classid, lessons } = lessonInfo;
-      const planRes = await this.tmodel.findOne({ termnum: { $elemMatch: { _id: termid } } });
+    for (const l of list) {
+      const planRes = await this.tmodel.findOne({ termnum: { $elemMatch: { _id: l.termid } } });
+      const { planyearid, _id: planid } = planRes;
       if (!planRes) continue;
-      const term = planRes.termnum.find(f => ObjectId(termid).equals(f._id));
-      if (!term) continue;
-      const { term: termnum, batchnum } = term;
-      let start;
-      let end;
-      if (batchnum) {
-        const { startdate, enddate } = batchnum;
-        if (startdate) start = startdate;
-        if (enddate) end = enddate;
+      // 教师 需要知道 期,批,班 日期 星期 科目 上课地点 班主任 班主任电话
+      // 先找到这个课表是哪个班的
+      const { classid } = l;
+      if (!classid) {
+        console.error(`不存在班级id为=>${classid}`);
+        continue;
       }
-      const classInfo = await this.clamodel.findById(classid);
-      // 先查找信息,如果有,不需要创建,修改创建信息,获取数据
-      const content = `欢迎参加由吉林省高等学校毕业生就业指导中心举办的"双困生培训会"第${termnum}期-${classInfo.name}${classInfo.name.includes('班') ? '' : '班'}${start ? `(${start}至${end})` : ''}`;
-      let is_update = false;
-      let nres = await this.nmodel.findOne({
-        planyearid: planRes.planyearid,
-        planid: planRes._id,
-        termid,
-        classid,
-        noticeid: 'system',
-        type: '4',
-      });
-      console.log(nres);
-      let alreadyList = [];
-      if (nres) {
-        const { notified } = nres;
-        alreadyList = notified.filter(i => i.status === '1');
-        is_update = true;
+      const classInfo = await this.ctx.service.class.fetch({ id: classid });
+      if (!classInfo) {
+        console.error(`没有id为=>${classid} 的班级信息`);
+        continue;
       }
-      if (!is_update) {
-        nres = await this.nmodel.create({
-          planyearid: planRes.planyearid,
-          planid: planRes._id,
-          termid,
-          classid,
-          noticeid: 'system',
-          type: '4',
-          content,
-        });
+      const { termid } = classInfo;
+      // 班主任信息
+      let data = await this.getHeadTeacherMsg(classInfo);
+      // 整理信息
+      data = { ...data, ...defaults, termid, classid, planyearid, planid };
+      noticeList.push(data);
+      const { lessons } = l;
+      let have_teacherLesson = lessons.filter(f => f.teaid);
+      have_teacherLesson = JSON.parse(JSON.stringify(have_teacherLesson));
+      const newArr = [];
+      for (const l of have_teacherLesson) {
+        // 整理时间
+        const { subid, teaid, date } = l;
+        const isBefore = moment().isBefore(date);
+        if (!isBefore) false;
+        if (!subid && teaid) continue;
+        const r = newArr.find(f => f.subid === subid && f.teaid === teaid);
+        const ri = newArr.findIndex(
+          f => f.subid === subid && f.teaid === teaid
+        );
+        // 如果找到了,就要把这个时间和上一个整合
+        if (r) {
+          // console.log(newArr[ri].timeList);
+          newArr[ri].timeList.push(l.time);
+        } else {
+          let obj = _.cloneDeep(l);
+          // 没找到,就放进去
+          const timeList = [ l.time ];
+          obj = { ...obj, timeList };
+          newArr.push(obj);
+        }
+      }
+      for (const l of newArr) {
+        let teamsg = await this.getTeacherMsg(classInfo, l);
+        teamsg = { ...teamsg, ...defaults, termid, classid, planyearid, planid };
+        noticeList.push(teamsg);
       }
 
-      const { headteacherid, lyteacherid } = classInfo;
-      const headteacher = await this.headteamodel.findById(headteacherid);
-      if (headteacher) {
-        const r = await this.toSendMsg(headteacher, 'headteacher', termnum, nres._id, content, alreadyList);
-        if (r && !is_update) nres.notified.push(r);
-        else if (r && is_update) {
-          const dr = nres.notified.find(f => f.notifiedid === r.notifiedid);
-          if (!dr) nres.notified.push(r);
-        }
+    }
+    await this.toSendMsg(noticeList);
+  }
+  // 给班主任发信息
+  async getHeadTeacherMsg(classInfo) {
+    // 班主任 需要知道 期,批,班,时间段,星期段,礼仪课教师,用餐地点,拓展计划地点,开班仪式地点,上课地点
+    const { term, batch, headteacher, name, headteacherid, startdate, enddate } = classInfo;
+    if (!headteacherid) return;
+    let msg = `班主任-${headteacher},中心已经安排您为: ${term}期-${batch}批-${name.includes('班') ? name : `${name}班`} 班主任`;
+    if (startdate && enddate) msg = `${msg} \n 本班时间为:${startdate}(星期${this.weekList[moment(startdate).day()]}) 至 ${enddate} (星期${this.weekList[moment(enddate).day()]})`;
+    if (_.get(classInfo, 'kbyslocationid'))msg = `${msg} \n 开班仪式地点为:${_.get(classInfo, 'kbyslocation')}`;
+    if (_.get(classInfo, 'kzjhlocationid'))msg = `${msg} \n 拓展训练地点为:${_.get(classInfo, 'kzjhlocation')}`;
+    if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课教室地点为:${_.get(classInfo, 'jslocation')}`;
+    if (_.get(classInfo, 'yclocationid'))msg = `${msg} \n 用餐地点为:${_.get(classInfo, 'yclocation')}`;
+    // 礼仪教师需要查询,然后带上电话
+    if (_.get(classInfo, 'lyteacherid')) {
+      msg = `${msg} \n 礼仪课教师为: \n ${_.get(classInfo, 'lyteacher')}`;
+      const { lyteacherid } = classInfo;
+      const r = await this.umodel.findOne({ uid: lyteacherid });
+      if (r) {
+        const { mobile } = r;
+        if (mobile) msg = `${msg} 电话:${mobile}`;
       }
-      // 礼仪教师和班主任不是一个人时查礼仪教师是班主任还是任课教师,然后发消息
-      if (lyteacherid !== headteacherid) {
-        let lyTeacher = await this.headteamodel.findById(lyteacherid);
-        if (lyTeacher) {
-          const r = await this.toSendMsg(lyTeacher, 'headteacher', termnum, nres._id, content, alreadyList);
-          if (r && !is_update) nres.notified.push(r);
-          else if (r && is_update) {
-            const dr = nres.notified.find(f => f.notifiedid === r.notifiedid);
-            if (!dr) nres.notified.push(r);
-          }
-        } else {
-          lyTeacher = await this.teamodel.findById(lyteacherid);
-          if (lyTeacher) {
-            const r = await this.toSendMsg(lyTeacher, 'teacher', termnum, nres._id, content, alreadyList);
-            if (r && !is_update) nres.notified.push(r);
-            else if (r && is_update) {
-              const dr = nres.notified.find(f => f.notifiedid === r.notifiedid);
-              if (!dr) nres.notified.push(r);
-            }
-          }
-        }
+    }
+    // 查出openid,email
+    const { openid, email } = await this.getSendInfo(headteacherid);
+    return { openid, email, notifiedid: headteacherid, username: headteacher, content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
+  }
+
+  async getTeacherMsg(classInfo, lessonInfo) {
+    if (!classInfo || !lessonInfo) return;
+    const { term, batch, name, headteacherid } = classInfo;
+    const { date, subname, timeList } = lessonInfo;
+    let msg = `教师-${lessonInfo.teaname}您好,中心已经为您安排了 \n ${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}`;
+    if (date) msg = `${msg} \n 上课日期:${date}(星期${this.weekList[moment(date).day()]})`;
+    if (timeList && _.isArray(timeList)) {
+      msg = `${msg} \n 上课时间:`;
+      for (const time of timeList) {
+        msg = `${msg} ${time}`;
       }
-      // 获取所有任课教师ids
-      const teacherList = _.compact(_.uniq(lessons.map(i => i.teaid)));
-      if (teacherList) {
-        for (const tea of teacherList) {
-          const teacher = await this.teamodel.findById(tea);
-          if (!teacher) continue;
-          const r = await this.toSendMsg(teacher, 'teacher', termnum, nres._id, content, alreadyList);
-          if (r && !is_update) nres.notified.push(r);
-          else if (r && is_update) {
-            const dr = nres.notified.find(f => f.notifiedid === r.notifiedid);
-            if (!dr) nres.notified.push(r);
-          }
+    }
+    if (subname) msg = `${msg} \n 课程:${subname}`;
+    if (_.get(classInfo, 'jslocationid'))msg = `${msg} \n 上课教室地点为:${_.get(classInfo, 'jslocation')}`;
+    if (_.get(classInfo, 'headteacherid')) {
+      msg = `${msg} \n 班主任为: \n ${_.get(classInfo, 'headteacher')}`;
+      if (headteacherid) {
+        const r = await this.umodel.findOne({ uid: headteacherid });
+        if (r) {
+          const { mobile } = r;
+          if (mobile) msg = `${msg} 电话:${mobile}`;
         }
       }
-      nres.save();
     }
+    const { openid, email } = await this.getSendInfo(lessonInfo.teaid);
+    return { openid, email, notifiedid: _.get(lessonInfo, 'teaid'), username: _.get(lessonInfo, 'teaname'), content: msg, ncontent: `${term}期-${batch}批-${name.includes('班') ? name : `${name}班`}课表确认` };
   }
 
-  async toSendMsg(teacherInfo, type, term, nresid, content, alreadyList) {
-    const r = alreadyList.find(f => ObjectId(f.notifiedid).equals(teacherInfo._id));
-    if (r) return;
-    let person = null;
-    if (teacherInfo) {
-      let email;
-      if (type === 'headteacher') {
-        const { qq } = teacherInfo;
+  // 查找openid和emaiil
+  async getSendInfo(uid) {
+    const user = await this.umodel.findOne({ uid });
+    if (!user) return;
+    const { type, openid } = user;
+    let email;
+    // type =1班主任,type = 3 教师
+    if (type === '1') {
+      const info = await this.headteamodel.findOne({ _id: ObjectId(uid) });
+      if (info) {
+        const { qq } = info;
         if (qq) email = `${qq}@qq.com`;
+      }
+    } else if (type === '3') {
+      const info = await this.teamodel.findOne({ _id: ObjectId(uid) });
+      if (info) email = info.email;
+    }
+    return { openid, email };
+  }
+
+
+  async toSendMsg(noticeList) {
+    // console.log(noticeList);
+    for (const notice of noticeList) {
+      // 先找信息notice,然后查看有没有这个人的信息,有就发送,没有就添加,再发送
+      // 课表通知,1个班的信息放一起,按班级来看
+      const { planyearid, planid, termid, classid, noticeid, type, ncontent, content, username, notifiedid, email, openid } = notice;
+      // await this.nmodel.deleteMany({ termid, planid, classid, type });
+      console.log(`${username}-${email}-${openid}`);
+      // 排除重复,没有的填进对应的班级中
+      let nres = await this.nmodel.findOne({ termid, planid, classid, type });
+      if (!nres) {
+        // 组织数据,存起来
+        const notified = [{ content, username, notifiedid }];
+        const nobj = { planyearid, planid, termid, classid, noticeid, type, content: ncontent, notified };
+        await this.nmodel.create(nobj);
+        nres = await this.nmodel.findOne({ termid, planid, classid, type });
       } else {
-        email = teacherInfo.email;
+        // 有该班的通知,然后查看是否有这个人,没有,加进去,有这个人,不管
+        if (nres.notified && _.isArray(nres.notified)) {
+          const r = nres.notified.find(f => f.notifiedid === notifiedid);
+          // 没有人,加进去,保存
+          if (!r) {
+            nres.notified.push({ content, username, notifiedid });
+            await nres.save();
+          }
+        }
       }
-      // 发邮件
+
+      // 邮件
       if (email) {
-        // console.error(`${teacherInfo.name}-email:${email}`);
         const subject = '吉林省高等学校毕业生就业指导中心通知';
-        const text = teacherInfo.name + `${content},请您尽快登陆双困生培训系统查看您的安排,系统邮件,请勿回复!`;
-        this.ctx.service.util.sendMail(email, subject, text);
+        this.ctx.service.util.sendMail('2938260457@qq.com', subject, content);
+      }
+      if (openid) {
+        const tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + notifiedid + '&noticeid=' + nres._id;
+        // TODO 推送
+        await this.ctx.service.weixin.sendTemplateDesign(
+          this.ctx.app.config.REVIEW_TEMPLATE_ID,
+          'ocPqjswkUejZHq2ANriNrFFC7A3I',
+          '您有一个新的通知,请点击信息,确认您已收到信息!',
+          '您的安排',
+          content,
+          '感谢您的使用',
+          tourl
+        );
       }
 
 
-      // 获取openid,推送
-      const teacherUser = await this.umodel.findOne({ uid: teacherInfo._id });
-      if (teacherUser) {
-        const { openid } = teacherUser;
-        if (openid) {
-          const tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + teacherUser.uid + '&noticeid=' + nresid;
-          // TODO 推送
-          await this.ctx.service.weixin.sendTemplateDesign(
-            this.ctx.app.config.REVIEW_TEMPLATE_ID,
-            openid,
-            '您有一个新的通知,请点击信息,确认您已收到信息!(若已确认,则无需理会)',
-            '您有新的安排',
-            content,
-            '感谢您的使用',
-            tourl
-          );
-          person = { notifiedid: teacherUser.uid, username: teacherUser.name };
-        }
-      }
     }
-    return person;
+
+
+    // const r = alreadyList.find(f => ObjectId(f.notifiedid).equals(teacherInfo._id));
+    // if (r) return;
+    // let person = null;
+    // if (teacherInfo) {
+    //   let email;
+    //   if (type === 'headteacher') {
+    //     const { qq } = teacherInfo;
+    //     if (qq) email = `${qq}@qq.com`;
+    //   } else {
+    //     email = teacherInfo.email;
+    //   }
+    //   // 发邮件
+    //   if (email) {
+    //     // console.error(`${teacherInfo.name}-email:${email}`);
+    //     const subject = '吉林省高等学校毕业生就业指导中心通知';
+    //     const text = teacherInfo.name + `${content},请您尽快登陆双困生培训系统查看您的安排,系统邮件,请勿回复!`;
+    //     this.ctx.service.util.sendMail('2938260457@qq.com', subject, text); // email
+    //   }
+
+
+    //   // 获取openid,推送
+    //   const teacherUser = await this.umodel.findOne({ uid: teacherInfo._id });
+    //   if (teacherUser) {
+    //     const { openid } = teacherUser;
+    //     if (openid) {
+    //       const tourl = this.ctx.app.config.baseUrl + '/msgconfirm/?userid=' + teacherUser.uid + '&noticeid=' + nresid;
+    //       // TODO 推送
+    //       await this.ctx.service.weixin.sendTemplateDesign(
+    //         this.ctx.app.config.REVIEW_TEMPLATE_ID,
+    //         'ocPqjswkUejZHq2ANriNrFFC7A3I',
+    //         '您有一个新的通知,请点击信息,确认您已收到信息!(若已确认,则无需理会)',
+    //         '您有新的安排',
+    //         content,
+    //         '感谢您的使用',
+    //         tourl
+    //       );
+    //       person = { notifiedid: teacherUser.uid, username: teacherUser.name };
+    //     }
+    //   }
+    // }
+    // return person;
   }
 
   // 新排课,从计划中拿出来对应的课表

+ 0 - 2
app/service/util.js

@@ -93,11 +93,9 @@ class UtilService extends CrudService {
     const nowDate = new Date().getTime();
     const filename = `${fn}-${nowDate}.xlsx`;
     // 取出预设存储地址
-    console.log(`cdn=>${app.config.cdn}`);
     const rootPath = `${app.config.cdn.repos_root_path}`;
     const rooturl = `${app.config.cdn.repos_root_url_excel}`;
     const path = `${rootPath}${rooturl}`;
-    console.log(`filePath=>${path}`);
     if (!path) {
       throw new BusinessError(ErrorCode.BUSINESS, '服务端没有设置存储路径');
     }