inleft
2022-08-23 124ca536917b5eebe059bb98745f6743c52a6836
commit | author | age
7fe1bd 1 package vip.xiaonuo.modular.task;
I 2
3 import cn.hutool.core.date.DateUtil;
4 import cn.hutool.core.lang.Dict;
1e152b 5 import cn.hutool.core.util.StrUtil;
7fe1bd 6 import cn.hutool.extra.template.Template;
I 7 import cn.hutool.extra.template.TemplateConfig;
8 import cn.hutool.extra.template.TemplateEngine;
9 import cn.hutool.extra.template.TemplateUtil;
10 import lombok.extern.slf4j.Slf4j;
11 import org.springframework.stereotype.Component;
12 import vip.xiaonuo.core.consts.MyConstant;
13 import vip.xiaonuo.core.email.MailSender;
14 import vip.xiaonuo.core.email.modular.model.SendMailParam;
15 import vip.xiaonuo.core.timer.TimerTaskRunner;
16 import vip.xiaonuo.modular.blogarticlecomment.entity.BlogArticleComment;
17 import vip.xiaonuo.modular.blogarticlecomment.service.BlogArticleCommentService;
18
19 import javax.annotation.Resource;
20 import java.util.List;
21 import java.util.stream.Collectors;
22
23 /**
24  * ALTER TABLE `blog_dev`.`blog_article_comment`
25  * ADD COLUMN `is_remind` int(4) NOT NULL DEFAULT 0 COMMENT '是否已提醒 0:否 1:是' AFTER `is_read`;
26  */
27 @Component
28 @Slf4j
29 public class EMailTaskRunner implements TimerTaskRunner {
30
31     @Resource
32     private BlogArticleCommentService commentService;
33
34     @Resource
35     private MailSender mailSender;
36
37     /**
38      * 邮件定时提醒消息
39      * 最近一天内的未读消息(除了自己发的消息)
40      */
41     @Override
42     public void action() {
43         log.info("邮件自提醒定时任务来啦。。");
44
45         List<BlogArticleComment> commentList = commentService.lambdaQuery()
46                 .ne(BlogArticleComment::getVisitorNickName, MyConstant.inleft)
47                 .eq(BlogArticleComment::getIsRead, MyConstant.No)
48                 .eq(BlogArticleComment::getIsRemind, MyConstant.No)
49                 .ge(BlogArticleComment::getCreateDate, DateUtil.offsetDay(DateUtil.date(), -1))
50                 .select(BlogArticleComment::getId, BlogArticleComment::getVisitorNickName, BlogArticleComment::getCommentContent)
1e152b 51                 .last("limit 10")
7fe1bd 52                 .list();
1e152b 53
I 54         int unReadCount = commentService.lambdaQuery()
55                 .ne(BlogArticleComment::getVisitorNickName, MyConstant.inleft)
56                 .eq(BlogArticleComment::getIsRead, MyConstant.No)
57                 .eq(BlogArticleComment::getIsRemind, MyConstant.No)
58                 .ge(BlogArticleComment::getCreateDate, DateUtil.offsetDay(DateUtil.date(), -1))
59                 .count();
7fe1bd 60
I 61         if (commentList.size() == 0) {
62             log.info("本次无提醒消息条数。。");
63             return;
64         }
65
66         TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig(MyConstant.ftlModel.path, TemplateConfig.ResourceMode.CLASSPATH));
67         Template template = engine.getTemplate(MyConstant.ftlModel.remind);
68         //发送邮件
69         Dict dict = Dict.create()
70                 .set("contentList", commentList)
1e152b 71                 .set("sendContent", StrUtil.format("新增了 {} 条未读信息", unReadCount));
7fe1bd 72         String result = template.render(dict);
I 73
74         SendMailParam param = new SendMailParam();
75         param.setContent(result);
1e152b 76         param.setTitle(StrUtil.format("邮箱定时任务(msg:{})..\n", unReadCount));
7fe1bd 77         param.setTo(MyConstant.email);
I 78         mailSender.sendMailHtml(param);
79
80         //更新为已提醒
81         commentService.updateBatchById(commentList.stream().map(e -> {
82             BlogArticleComment updateTarget = new BlogArticleComment();
83             updateTarget.setId(e.getId());
84             updateTarget.setIsRemind(MyConstant.Yes);
85             return updateTarget;
86         }).collect(Collectors.toList()));
87
88         log.info("本次任务完成。。");
89
90     }
91
92
93 }