package vip.xiaonuo.modular.task; import cn.hutool.core.date.DateUtil; import cn.hutool.core.lang.Dict; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.template.Template; import cn.hutool.extra.template.TemplateConfig; import cn.hutool.extra.template.TemplateEngine; import cn.hutool.extra.template.TemplateUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import vip.xiaonuo.core.consts.MyConstant; import vip.xiaonuo.core.email.MailSender; import vip.xiaonuo.core.email.modular.model.SendMailParam; import vip.xiaonuo.core.timer.TimerTaskRunner; import vip.xiaonuo.modular.blogarticlecomment.entity.BlogArticleComment; import vip.xiaonuo.modular.blogarticlecomment.service.BlogArticleCommentService; import javax.annotation.Resource; import java.util.List; import java.util.stream.Collectors; /** * ALTER TABLE `blog_dev`.`blog_article_comment` * ADD COLUMN `is_remind` int(4) NOT NULL DEFAULT 0 COMMENT '是否已提醒 0:否 1:是' AFTER `is_read`; */ @Component @Slf4j public class EMailTaskRunner implements TimerTaskRunner { @Resource private BlogArticleCommentService commentService; @Resource private MailSender mailSender; /** * 邮件定时提醒消息 * 最近一天内的未读消息(除了自己发的消息) */ @Override public void action() { log.info("邮件自提醒定时任务来啦。。"); List commentList = commentService.lambdaQuery() .ne(BlogArticleComment::getVisitorNickName, MyConstant.inleft) .eq(BlogArticleComment::getIsRead, MyConstant.No) .eq(BlogArticleComment::getIsRemind, MyConstant.No) .ge(BlogArticleComment::getCreateDate, DateUtil.offsetDay(DateUtil.date(), -1)) .select(BlogArticleComment::getId, BlogArticleComment::getVisitorNickName, BlogArticleComment::getCommentContent) .last("limit 10") .list(); int unReadCount = commentService.lambdaQuery() .ne(BlogArticleComment::getVisitorNickName, MyConstant.inleft) .eq(BlogArticleComment::getIsRead, MyConstant.No) .eq(BlogArticleComment::getIsRemind, MyConstant.No) .ge(BlogArticleComment::getCreateDate, DateUtil.offsetDay(DateUtil.date(), -1)) .count(); if (commentList.size() == 0) { log.info("本次无提醒消息条数。。"); return; } TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig(MyConstant.ftlModel.path, TemplateConfig.ResourceMode.CLASSPATH)); Template template = engine.getTemplate(MyConstant.ftlModel.remind); //发送邮件 Dict dict = Dict.create() .set("contentList", commentList) .set("sendContent", StrUtil.format("新增了 {} 条未读信息", unReadCount)); String result = template.render(dict); SendMailParam param = new SendMailParam(); param.setContent(result); param.setTitle(StrUtil.format("邮箱定时任务(msg:{})..\n", unReadCount)); param.setTo(MyConstant.email); mailSender.sendMailHtml(param); //更新为已提醒 commentService.updateBatchById(commentList.stream().map(e -> { BlogArticleComment updateTarget = new BlogArticleComment(); updateTarget.setId(e.getId()); updateTarget.setIsRemind(MyConstant.Yes); return updateTarget; }).collect(Collectors.toList())); log.info("本次任务完成。。"); } }