Search in sources :

Example 1 with WarningEmailBody

use of com.topcom.cms.yuqing.vo.email.WarningEmailBody in project topcom-cloud by 545314690.

the class WarningJob method sendEmail.

private void sendEmail(Page page, CustomSubject subject, Long wangningLogId) {
    WarningTask warningTask = subject.getWarning();
    // logger.info("开始推送Email消息:" + subject.getName());
    List<Contact> contacts = warningTask.getContacts();
    /**
     * 获取所有联系人
     */
    Set<String> emailSet = contacts.stream().filter(contact -> Contact.Type.EMAIL.equals(contact.getType())).map(contact -> contact.getAccount()).collect(Collectors.toSet());
    logger.info("开始推送Email消息:" + subject.getName() + emailSet.toString());
    if (emailSet.size() > 0) {
        /**
         *先保存预警日志,否则下面warningLog.getId()取到的日志Id总为0,保存日志的时候,不要调用save
         */
        // 发送邮件
        SenderFactory emailSenderFactory = new EmailSenderFactory();
        Sender emailSender = emailSenderFactory.create();
        String[] emails = {};
        emails = emailSet.toArray(emails);
        try {
            WarningEmailBody warningEmailBody = new WarningEmailBody();
            warningEmailBody.setContent(page.getContent());
            warningEmailBody.setSpecialNumber(String.valueOf(page.getTotalElements()));
            warningEmailBody.setSpecialName(subject.getName());
            User user = userManager.findById(subject.getUserId());
            warningEmailBody.setUser(user.getFullName() == null ? user.getUsername() : user.getFullName());
            // warningEmailBody.setSubject("");
            warningEmailBody.setSpecialUrl(warningEmailSetting.getSubjectUrl() + subject.getId());
            warningEmailBody.setWarningLogUrl(warningEmailSetting.getLogUrl() + wangningLogId);
            WarningEmail warningEmail = new WarningEmail().create(emails, warningEmailBody);
            emailSender.send(warningEmail);
            // 更新上次预警时间
            warningTask.setLastWarningDate(new Date());
            warningManager.save(warningTask);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : WarningEmailBody(com.topcom.cms.yuqing.vo.email.WarningEmailBody) Date(java.util.Date) TemplateException(freemarker.template.TemplateException) Logger(org.jboss.logging.Logger) Autowired(org.springframework.beans.factory.annotation.Autowired) MessagingException(javax.mail.MessagingException) Job(org.quartz.Job) com.topcom.cms.yuqing.domain(com.topcom.cms.yuqing.domain) SubscriptionFollowerManager(com.topcom.cms.yuqing.service.SubscriptionFollowerManager) Map(java.util.Map) Pageable(org.springframework.data.domain.Pageable) CustomSubjectManager(com.topcom.cms.yuqing.service.CustomSubjectManager) Sender(com.topcom.cms.yuqing.task.sender.Sender) ServerProperties(org.springframework.boot.autoconfigure.web.ServerProperties) WechatUtil(com.topcom.cms.yuqing.utils.WechatUtil) WebUtils(org.springframework.web.util.WebUtils) JobExecutionContext(org.quartz.JobExecutionContext) UserManager(com.topcom.cms.service.UserManager) SenderFactory(com.topcom.cms.yuqing.task.sender.factory.SenderFactory) User(com.topcom.cms.domain.User) WechatSendRequest(com.topcom.cms.yuqing.vo.request.WechatSendRequest) WarningEmailSetting(com.topcom.cms.yuqing.vo.email.WarningEmailSetting) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) IOException(java.io.IOException) Page(org.springframework.data.domain.Page) WarningManager(com.topcom.cms.yuqing.service.WarningManager) Collectors(java.util.stream.Collectors) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory) JobExecutionException(org.quartz.JobExecutionException) WarningLogManager(com.topcom.cms.yuqing.service.WarningLogManager) Component(org.springframework.stereotype.Component) List(java.util.List) WeChat(com.topcom.cms.data.domain.WeChat) WarningEmail(com.topcom.cms.yuqing.vo.email.WarningEmail) PostConstruct(javax.annotation.PostConstruct) JSONObject(net.sf.json.JSONObject) WarningEmailBody(com.topcom.cms.yuqing.vo.email.WarningEmailBody) User(com.topcom.cms.domain.User) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) WarningEmail(com.topcom.cms.yuqing.vo.email.WarningEmail) Date(java.util.Date) TemplateException(freemarker.template.TemplateException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException) Sender(com.topcom.cms.yuqing.task.sender.Sender) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory) SenderFactory(com.topcom.cms.yuqing.task.sender.factory.SenderFactory) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory)

Aggregations

WeChat (com.topcom.cms.data.domain.WeChat)1 User (com.topcom.cms.domain.User)1 UserManager (com.topcom.cms.service.UserManager)1 com.topcom.cms.yuqing.domain (com.topcom.cms.yuqing.domain)1 CustomSubjectManager (com.topcom.cms.yuqing.service.CustomSubjectManager)1 SubscriptionFollowerManager (com.topcom.cms.yuqing.service.SubscriptionFollowerManager)1 WarningLogManager (com.topcom.cms.yuqing.service.WarningLogManager)1 WarningManager (com.topcom.cms.yuqing.service.WarningManager)1 Sender (com.topcom.cms.yuqing.task.sender.Sender)1 EmailSenderFactory (com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory)1 SenderFactory (com.topcom.cms.yuqing.task.sender.factory.SenderFactory)1 WechatUtil (com.topcom.cms.yuqing.utils.WechatUtil)1 WarningEmail (com.topcom.cms.yuqing.vo.email.WarningEmail)1 WarningEmailBody (com.topcom.cms.yuqing.vo.email.WarningEmailBody)1 WarningEmailSetting (com.topcom.cms.yuqing.vo.email.WarningEmailSetting)1 WechatSendRequest (com.topcom.cms.yuqing.vo.request.WechatSendRequest)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1