提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.mail; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.pojo.PageResult; |
|
4 |
import com.iailab.module.system.controller.admin.mail.vo.log.MailLogPageReqVO; |
|
5 |
import com.iailab.module.system.dal.dataobject.mail.MailAccountDO; |
|
6 |
import com.iailab.module.system.dal.dataobject.mail.MailLogDO; |
|
7 |
import com.iailab.module.system.dal.dataobject.mail.MailTemplateDO; |
|
8 |
import com.iailab.module.system.dal.mysql.mail.MailLogMapper; |
|
9 |
import com.iailab.module.system.enums.mail.MailSendStatusEnum; |
|
10 |
import org.springframework.stereotype.Service; |
|
11 |
import org.springframework.validation.annotation.Validated; |
|
12 |
|
|
13 |
import javax.annotation.Resource; |
|
14 |
import java.time.LocalDateTime; |
|
15 |
import java.util.Map; |
|
16 |
import java.util.Objects; |
|
17 |
|
|
18 |
import static cn.hutool.core.exceptions.ExceptionUtil.getRootCauseMessage; |
|
19 |
|
|
20 |
/** |
|
21 |
* 邮件日志 Service 实现类 |
|
22 |
* |
|
23 |
* @author wangjingyi |
|
24 |
* @since 2022-03-21 |
|
25 |
*/ |
|
26 |
@Service |
|
27 |
@Validated |
|
28 |
public class MailLogServiceImpl implements MailLogService { |
|
29 |
|
|
30 |
@Resource |
|
31 |
private MailLogMapper mailLogMapper; |
|
32 |
|
|
33 |
@Override |
|
34 |
public PageResult<MailLogDO> getMailLogPage(MailLogPageReqVO pageVO) { |
|
35 |
return mailLogMapper.selectPage(pageVO); |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public MailLogDO getMailLog(Long id) { |
|
40 |
return mailLogMapper.selectById(id); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public Long createMailLog(Long userId, Integer userType, String toMail, |
|
45 |
MailAccountDO account, MailTemplateDO template, |
|
46 |
String templateContent, Map<String, Object> templateParams, Boolean isSend) { |
|
47 |
MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder(); |
|
48 |
// 根据是否要发送,设置状态 |
|
49 |
logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus() |
|
50 |
: MailSendStatusEnum.IGNORE.getStatus()) |
|
51 |
// 用户信息 |
|
52 |
.userId(userId).userType(userType).toMail(toMail) |
|
53 |
.accountId(account.getId()).fromMail(account.getMail()) |
|
54 |
// 模板相关字段 |
|
55 |
.templateId(template.getId()).templateCode(template.getCode()).templateNickname(template.getNickname()) |
|
56 |
.templateTitle(template.getTitle()).templateContent(templateContent).templateParams(templateParams); |
|
57 |
|
|
58 |
// 插入数据库 |
|
59 |
MailLogDO logDO = logDOBuilder.build(); |
|
60 |
mailLogMapper.insert(logDO); |
|
61 |
return logDO.getId(); |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public void updateMailSendResult(Long logId, String messageId, Exception exception) { |
|
66 |
// 1. 成功 |
|
67 |
if (exception == null) { |
|
68 |
mailLogMapper.updateById(new MailLogDO().setId(logId).setSendTime(LocalDateTime.now()) |
|
69 |
.setSendStatus(MailSendStatusEnum.SUCCESS.getStatus()).setSendMessageId(messageId)); |
|
70 |
return; |
|
71 |
} |
|
72 |
// 2. 失败 |
|
73 |
mailLogMapper.updateById(new MailLogDO().setId(logId).setSendTime(LocalDateTime.now()) |
|
74 |
.setSendStatus(MailSendStatusEnum.FAILURE.getStatus()).setSendException(getRootCauseMessage(exception))); |
|
75 |
|
|
76 |
} |
|
77 |
|
|
78 |
} |