inleft
2022-08-23 124ca536917b5eebe059bb98745f6743c52a6836
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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<BlogArticleComment> 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("本次任务完成。。");
 
    }
 
 
}