mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 12:18:42 +08:00 
			
		
		
		
	邮件模块 添加用户类型 用户编码信息
This commit is contained in:
		@@ -18,9 +18,9 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
 | 
			
		||||
@ToString(callSuper = true)
 | 
			
		||||
public class MailLogPageReqVO extends PageParam {
 | 
			
		||||
 | 
			
		||||
    // TODO @wangjingyi:required 为 false 时,它是默认值,所以不用谢
 | 
			
		||||
    // TODO @wangjingyi:required 为 false 时,它是默认值,所以不用谢 DONE
 | 
			
		||||
 | 
			
		||||
    @ApiModelProperty(value = "邮箱" , required = false , example = "yudaoyuanma@123.com")
 | 
			
		||||
    @ApiModelProperty(value = "邮箱" , example = "yudaoyuanma@123.com")
 | 
			
		||||
    private String from;
 | 
			
		||||
 | 
			
		||||
    @ApiModelProperty(value = "模版编号" , required = false , example = "templeId")
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,16 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.dal.dataobject.mail;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.mail.MailLogUserTypeEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.mail.MailSendStatusEnum;
 | 
			
		||||
import com.baomidou.mybatisplus.annotation.TableField;
 | 
			
		||||
import com.baomidou.mybatisplus.annotation.TableName;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
 | 
			
		||||
import lombok.*;
 | 
			
		||||
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 邮箱日志
 | 
			
		||||
@@ -29,15 +33,29 @@ public class MailLogDO extends BaseDO implements Serializable {
 | 
			
		||||
     */
 | 
			
		||||
    private Long id;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 用户编码
 | 
			
		||||
     */
 | 
			
		||||
    private Long userId;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 用户类型
 | 
			
		||||
     *
 | 
			
		||||
     * 冗余 {@link MailLogUserTypeEnum#getUserType}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer userType;
 | 
			
		||||
 | 
			
		||||
    // TODO @wangjingyi:accountId
 | 
			
		||||
    /**
 | 
			
		||||
     * 邮箱账号编号
 | 
			
		||||
     */
 | 
			
		||||
    private Long accountId;
 | 
			
		||||
 | 
			
		||||
    // TODO @wangjingyi:如果是冗余字段,记得 @ 下;
 | 
			
		||||
    // TODO @wangjingyi:如果是冗余字段,记得 @ 下;DONE
 | 
			
		||||
    /**
 | 
			
		||||
     * 邮箱账号
 | 
			
		||||
     *
 | 
			
		||||
     * 冗余 {@link MailAccountDO}
 | 
			
		||||
     */
 | 
			
		||||
    private String fromAddress;
 | 
			
		||||
 | 
			
		||||
@@ -47,30 +65,22 @@ public class MailLogDO extends BaseDO implements Serializable {
 | 
			
		||||
    private Long templateId;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 模版编号
 | 
			
		||||
     * 模版内容
 | 
			
		||||
     */
 | 
			
		||||
    private String templateCode;
 | 
			
		||||
    private String templateContent;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 标题
 | 
			
		||||
     * 基于 {@link MailTemplateDO#getParams()} 输入后的参数
 | 
			
		||||
     */
 | 
			
		||||
    private String title;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 内容
 | 
			
		||||
     */
 | 
			
		||||
    private String content;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 收件人
 | 
			
		||||
     */
 | 
			
		||||
    private String to;
 | 
			
		||||
    @TableField(typeHandler = JacksonTypeHandler.class)
 | 
			
		||||
    private Map<String,Object> templateParams;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送时间
 | 
			
		||||
     */
 | 
			
		||||
    private Date sendTime;
 | 
			
		||||
 | 
			
		||||
    //=========接收相关字段=========
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送状态
 | 
			
		||||
     *
 | 
			
		||||
@@ -83,5 +93,10 @@ public class MailLogDO extends BaseDO implements Serializable {
 | 
			
		||||
     */
 | 
			
		||||
    private String sendResult;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *  消息ID
 | 
			
		||||
     */
 | 
			
		||||
    private String messageId;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,11 +2,13 @@ package cn.iocoder.yudao.module.system.dal.dataobject.mail;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
 | 
			
		||||
import com.baomidou.mybatisplus.annotation.TableField;
 | 
			
		||||
import com.baomidou.mybatisplus.annotation.TableName;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 邮箱模版
 | 
			
		||||
@@ -43,6 +45,11 @@ public class MailTemplateDO extends BaseDO {
 | 
			
		||||
     * 内容
 | 
			
		||||
     */
 | 
			
		||||
    private String content;
 | 
			
		||||
    /**
 | 
			
		||||
     * 参数数组(自动根据内容生成)
 | 
			
		||||
     */
 | 
			
		||||
    @TableField(typeHandler = JacksonTypeHandler.class)
 | 
			
		||||
    private List<String> params;
 | 
			
		||||
    /**
 | 
			
		||||
     * 状态
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.mq.consumer.mail;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.mail.MailTemplateRefreshMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.sms.SmsTemplateRefreshMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.mq.message.mail;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
 | 
			
		||||
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
@@ -46,7 +47,7 @@ public class MailSendMessage extends AbstractStreamMessage {
 | 
			
		||||
     * 收件人
 | 
			
		||||
     */
 | 
			
		||||
    @NotNull(message = "收件人不能为空")
 | 
			
		||||
    private List<String> tos;
 | 
			
		||||
    private String to;
 | 
			
		||||
    /**
 | 
			
		||||
     * 标题
 | 
			
		||||
     */
 | 
			
		||||
@@ -69,6 +70,10 @@ public class MailSendMessage extends AbstractStreamMessage {
 | 
			
		||||
     * 是否开启 SSL
 | 
			
		||||
     */
 | 
			
		||||
    private Boolean sslEnable;
 | 
			
		||||
    /**
 | 
			
		||||
     * 邮箱模板参数
 | 
			
		||||
     */
 | 
			
		||||
    private List<KeyValue<String, Object>> templateParams;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getStreamKey() {
 | 
			
		||||
 
 | 
			
		||||
@@ -7,9 +7,6 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.mail.MailAccountRefreshMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.mail.MailTemplateRefreshMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.sms.SmsChannelRefreshMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.sms.SmsSendMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.sms.SmsTemplateRefreshMessage;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
@@ -48,25 +45,27 @@ public class MailProducer {
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送 {@link MailSendMessage} 消息
 | 
			
		||||
     *
 | 
			
		||||
     * @param sendLogId 发送日志编码
 | 
			
		||||
     * @param mailAccountDO 邮箱账号信息
 | 
			
		||||
     * @param mailTemplateDO 邮箱模版信息
 | 
			
		||||
     * @param content 内容
 | 
			
		||||
     * @param tos 收件人
 | 
			
		||||
     * @param title 标题
 | 
			
		||||
     * @param templateParams 邮箱模版参数
 | 
			
		||||
     * @param to 收件人
 | 
			
		||||
     */
 | 
			
		||||
    public void sendMailSendMessage(MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String content, List<String> tos, String title , Long sendLogId) {
 | 
			
		||||
    public void sendMailSendMessage(Long sendLogId,MailAccountDO mailAccountDO, MailTemplateDO mailTemplateDO, String content,List<KeyValue<String, Object>> templateParams,String to) {
 | 
			
		||||
        MailSendMessage message = new MailSendMessage();
 | 
			
		||||
        message.setContent(content);
 | 
			
		||||
        message.setFromAddress(mailAccountDO.getFromAddress());
 | 
			
		||||
        message.setHost(mailAccountDO.getHost());
 | 
			
		||||
        message.setPort(mailAccountDO.getPort());
 | 
			
		||||
        message.setPassword(mailAccountDO.getPassword());
 | 
			
		||||
        message.setUsername(mailAccountDO.getUsername());
 | 
			
		||||
        message.setSslEnable(mailAccountDO.getSslEnable());
 | 
			
		||||
        message.setTemplateCode(mailTemplateDO.getCode());
 | 
			
		||||
        message.setTitle(title);
 | 
			
		||||
        message.setTos(tos);
 | 
			
		||||
        message.setLogId(sendLogId);
 | 
			
		||||
        message.setContent(content)
 | 
			
		||||
        .setFromAddress(mailAccountDO.getFromAddress())
 | 
			
		||||
        .setHost(mailAccountDO.getHost())
 | 
			
		||||
        .setPort(mailAccountDO.getPort())
 | 
			
		||||
        .setPassword(mailAccountDO.getPassword())
 | 
			
		||||
        .setUsername(mailAccountDO.getUsername())
 | 
			
		||||
        .setSslEnable(mailAccountDO.getSslEnable())
 | 
			
		||||
        .setTemplateCode(mailTemplateDO.getCode())
 | 
			
		||||
        .setTitle(mailTemplateDO.getTitle())
 | 
			
		||||
        .setTo(to)
 | 
			
		||||
        .setLogId(sendLogId)
 | 
			
		||||
        .setTemplateParams(templateParams);
 | 
			
		||||
        redisMQTemplate.send(message);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailLogDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 邮箱日志服务类
 | 
			
		||||
@@ -36,15 +37,16 @@ public interface MailLogService {
 | 
			
		||||
    /**
 | 
			
		||||
     * 创建邮箱日志
 | 
			
		||||
     *
 | 
			
		||||
     * @param userId 用户编码
 | 
			
		||||
     * @param userType 用户类型
 | 
			
		||||
     * @param to 收件人
 | 
			
		||||
     * @param mailAccountDO 邮箱账号信息
 | 
			
		||||
     * @param template      模版信息
 | 
			
		||||
     * @param from          邮箱
 | 
			
		||||
     * @param content       内容
 | 
			
		||||
     * @param tos           收件人
 | 
			
		||||
     * @param title         标题
 | 
			
		||||
     * @param templateContent 模版内容
 | 
			
		||||
     * @param templateParams 模版参数
 | 
			
		||||
     * @param isSend        是否发送成功
 | 
			
		||||
     */
 | 
			
		||||
    Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List<String> tos, String title, Boolean isSend);
 | 
			
		||||
    Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map<String, Object> templateParams, Boolean isSend);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 更新邮件发送结果
 | 
			
		||||
@@ -54,4 +56,12 @@ public interface MailLogService {
 | 
			
		||||
     */
 | 
			
		||||
    void updateMailSendResult(Long logId, String result);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 更新邮件发送结果
 | 
			
		||||
     *
 | 
			
		||||
     * @param logId  发送日志Id
 | 
			
		||||
     * @param exception 发送结果
 | 
			
		||||
     */
 | 
			
		||||
    void updateFailMailSendResult(Long logId, String exception);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -26,10 +26,12 @@ public interface MailSendService {
 | 
			
		||||
     * @param templateCode 邮件模版编码
 | 
			
		||||
     * @param from 邮箱
 | 
			
		||||
     * @param content 内容
 | 
			
		||||
     * @param tos 收件人
 | 
			
		||||
     * @param title 标题
 | 
			
		||||
     * @param templateParams 模版参数
 | 
			
		||||
     * @param to 收件人
 | 
			
		||||
     * @param userId 用户编码
 | 
			
		||||
     * @param userType 用户类型
 | 
			
		||||
     */
 | 
			
		||||
    void sendMail(String templateCode, String from , String content , List<String> tos , String title);
 | 
			
		||||
    void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 执行真正的邮件发送
 | 
			
		||||
 
 | 
			
		||||
@@ -13,8 +13,10 @@ import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.validation.annotation.Validated;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -27,9 +29,9 @@ import java.util.Objects;
 | 
			
		||||
@Validated
 | 
			
		||||
public class MailLogServiceImpl implements MailLogService {
 | 
			
		||||
 | 
			
		||||
    // TODO @wangjingyi:private,然后使用 @Resource
 | 
			
		||||
    @Autowired
 | 
			
		||||
    MailLogMapper mailLogMapper;
 | 
			
		||||
    // TODO @wangjingyi:private,然后使用 @Resource DONE
 | 
			
		||||
    @Resource
 | 
			
		||||
    private MailLogMapper mailLogMapper;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public PageResult<MailLogDO> getMailLogPage(MailLogPageReqVO pageVO) {
 | 
			
		||||
@@ -42,26 +44,29 @@ public class MailLogServiceImpl implements MailLogService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Long createMailLog(MailAccountDO mailAccountDO, MailTemplateDO template, String from, String content, List<String> tos, String title, Boolean isSend) {
 | 
			
		||||
    public Long createMailLog(Long userId,Integer userType,String to,MailAccountDO mailAccountDO, MailTemplateDO template , String templateContent, Map<String, Object> templateParams, Boolean isSend) {
 | 
			
		||||
        MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
 | 
			
		||||
        // TODO @wangjingyi:使用 builder 的时候,不用每个 set 是一行。
 | 
			
		||||
        // TODO @wangjingyi:使用 builder 的时候,不用每个 set 是一行。DONE
 | 
			
		||||
        // 根据是否要发送,设置状态
 | 
			
		||||
        logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus()
 | 
			
		||||
                : MailSendStatusEnum.IGNORE.getStatus());
 | 
			
		||||
        // 设置邮箱相关字段
 | 
			
		||||
        // TODO @wangjingyi:userId、userType
 | 
			
		||||
                : MailSendStatusEnum.IGNORE.getStatus())
 | 
			
		||||
                // 设置邮箱相关字段
 | 
			
		||||
                .fromAddress(mailAccountDO.getFromAddress())
 | 
			
		||||
                .accountId(mailAccountDO.getId())
 | 
			
		||||
                // TODO @wangjingyi:userId、userType
 | 
			
		||||
                //用户信息
 | 
			
		||||
                .userId(userId).userType(userType)
 | 
			
		||||
                //模版信息
 | 
			
		||||
                .templateId(template.getId()).templateParams(templateParams).templateContent(templateContent);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        logDOBuilder.fromAddress(mailAccountDO.getFromAddress());
 | 
			
		||||
        logDOBuilder.accountId(mailAccountDO.getId());
 | 
			
		||||
        // TODO @wangjingyi:每个接收人一条日志。发送多个人,就调用多次,业务方。因为某个邮箱有问题,会导致所有都发送失败。
 | 
			
		||||
        logDOBuilder.to(tos.toString());
 | 
			
		||||
        // TODO @wangjingyi:每个接收人一条日志。发送多个人,就调用多次,业务方。因为某个邮箱有问题,会导致所有都发送失败。 DONE
 | 
			
		||||
        // 设置模板相关字段
 | 
			
		||||
        // TODO @wangjingyi:可以参考下 sms 短信的逻辑,templateContent、templateParams
 | 
			
		||||
        logDOBuilder.templateId(template.getId());
 | 
			
		||||
        logDOBuilder.templateCode(template.getCode());
 | 
			
		||||
        logDOBuilder.title(title);
 | 
			
		||||
        logDOBuilder.content(content);
 | 
			
		||||
        // TODO @wangjingyi:有结果的时候,才是 sendTime 哈
 | 
			
		||||
        logDOBuilder.sendTime(new Date());
 | 
			
		||||
        // TODO @wangjingyi:有结果的时候,才是 sendTime 哈 DONE
 | 
			
		||||
        //logDOBuilder.sendTime(new Date());
 | 
			
		||||
 | 
			
		||||
        // 插入数据库
 | 
			
		||||
        MailLogDO logDO = logDOBuilder.build();
 | 
			
		||||
@@ -69,14 +74,20 @@ public class MailLogServiceImpl implements MailLogService {
 | 
			
		||||
        return logDO.getId();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO @wangjingyi:还是加几个字段哈,日志上。sendStatus,成功、失败;messageId 消息标号。sendException 记录发送的异常。这样界面才好筛选邮件的发送结果。
 | 
			
		||||
    // TODO @wangjingyi:还是加几个字段哈,日志上。sendStatus,成功、失败;messageId 消息标号。sendException 记录发送的异常。这样界面才好筛选邮件的发送结果。DONE
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateMailSendResult(Long logId, String result) {
 | 
			
		||||
        MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
 | 
			
		||||
        logDOBuilder.id(logId);
 | 
			
		||||
        logDOBuilder.sendResult(result);
 | 
			
		||||
        logDOBuilder.id(logId).sendTime(new Date()).sendResult(result).messageId(result).sendStatus(MailSendStatusEnum.SUCCESS.getStatus());
 | 
			
		||||
        MailLogDO mailLogDO = logDOBuilder.build();
 | 
			
		||||
        mailLogMapper.updateById(mailLogDO);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateFailMailSendResult(Long logId, String exception) {
 | 
			
		||||
        MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
 | 
			
		||||
        logDOBuilder.id(logId).sendTime(new Date()).sendResult(exception).sendStatus(MailSendStatusEnum.FAILURE.getStatus());
 | 
			
		||||
        MailLogDO mailLogDO = logDOBuilder.build();
 | 
			
		||||
        mailLogMapper.updateById(mailLogDO);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,17 +2,18 @@ package cn.iocoder.yudao.module.system.service.mail.impl;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.extra.mail.MailAccount;
 | 
			
		||||
import cn.hutool.extra.mail.MailUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
 | 
			
		||||
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.mail.MailLogService;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.mail.MailSendService;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
 | 
			
		||||
import com.google.common.annotations.VisibleForTesting;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.validation.annotation.Validated;
 | 
			
		||||
@@ -20,6 +21,7 @@ import org.springframework.validation.annotation.Validated;
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 | 
			
		||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
 | 
			
		||||
@@ -34,9 +36,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
 | 
			
		||||
@Validated
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class MailSendServiceImpl implements MailSendService {
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private MailTemplateMapper mailTemplateMapper;
 | 
			
		||||
    
 | 
			
		||||
    @Resource
 | 
			
		||||
    private MailAccountMapper mailAccountMapper;
 | 
			
		||||
    @Resource
 | 
			
		||||
@@ -48,7 +48,7 @@ public class MailSendServiceImpl implements MailSendService {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void sendMail(String templateCode, String from , String content , List<String> tos , String title) {
 | 
			
		||||
    public void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams) {
 | 
			
		||||
        // TODO @@wangjingyi:发送的时候,参考下短信;DONE
 | 
			
		||||
        //校验邮箱模版是否合法
 | 
			
		||||
        MailTemplateDO mailTemplateDO =  this.checkMailTemplateValid(templateCode);
 | 
			
		||||
@@ -58,14 +58,11 @@ public class MailSendServiceImpl implements MailSendService {
 | 
			
		||||
        MailAccountDO mailAccountDO = this.checkMailAccountValid(from);
 | 
			
		||||
        Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content);
 | 
			
		||||
        content = mailTemplateService.formatMailTemplateContent(mailTemplateDO.getContent(), params);
 | 
			
		||||
        Long sendLogId = mailLogService.createMailLog(mailAccountDO , mailTemplateDO , from , content , tos , title , isSend);
 | 
			
		||||
 | 
			
		||||
        // 后续功能 TODO :附件查询
 | 
			
		||||
        //List<String> fileIds = mailSendVO.getFileIds();
 | 
			
		||||
 | 
			
		||||
        Long sendLogId = mailLogService.createMailLog(userId,userType,to,mailAccountDO , mailTemplateDO , content,  templateParams,  isSend);
 | 
			
		||||
        List<KeyValue<String,Object>> newTemplateParams = buildTemplateParams(mailTemplateDO,templateParams);
 | 
			
		||||
        // 发送 MQ 消息,异步执行发送短信
 | 
			
		||||
        if (isSend) {
 | 
			
		||||
            mailProducer.sendMailSendMessage(mailAccountDO , mailTemplateDO ,content , tos , title , sendLogId);
 | 
			
		||||
            mailProducer.sendMailSendMessage(sendLogId,mailAccountDO , mailTemplateDO ,content, newTemplateParams,to);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -84,10 +81,10 @@ public class MailSendServiceImpl implements MailSendService {
 | 
			
		||||
        MailAccount account  = MailAccountConvert.INSTANCE.convertAccount(message);
 | 
			
		||||
        //发送邮件
 | 
			
		||||
        try{
 | 
			
		||||
            String messageId = MailUtil.send(account,message.getTos(),message.getTitle(),message.getContent(),false,null);
 | 
			
		||||
            String messageId = MailUtil.send(account,message.getTo(),message.getTitle(),message.getContent(),false,null);
 | 
			
		||||
            mailLogService.updateMailSendResult(message.getLogId() , messageId);
 | 
			
		||||
        }catch (Exception e){
 | 
			
		||||
            mailLogService.updateMailSendResult(message.getLogId() , e.getMessage());
 | 
			
		||||
            mailLogService.updateFailMailSendResult(message.getLogId() , e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
@@ -99,16 +96,21 @@ public class MailSendServiceImpl implements MailSendService {
 | 
			
		||||
        }
 | 
			
		||||
        return mailTemplateDO;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void validateMailTemplateExists(Long id) {
 | 
			
		||||
        if (mailTemplateMapper.selectById(id) == null) {
 | 
			
		||||
            throw exception(MAIL_TEMPLATE_NOT_EXISTS);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void validateMailTemplateOnlyByCode(String code){
 | 
			
		||||
        if (mailTemplateMapper.selectOneByCode(code) != null) {
 | 
			
		||||
            throw exception(MAIL_TEMPLATE_EXISTS);
 | 
			
		||||
        }
 | 
			
		||||
    /**
 | 
			
		||||
     * 将参数模板,处理成有序的 KeyValue 数组
 | 
			
		||||
     *
 | 
			
		||||
     * @param template 邮箱模板
 | 
			
		||||
     * @param templateParams 原始参数
 | 
			
		||||
     * @return 处理后的参数
 | 
			
		||||
     */
 | 
			
		||||
    @VisibleForTesting
 | 
			
		||||
    public List<KeyValue<String, Object>> buildTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) {
 | 
			
		||||
        return template.getParams().stream().map(key -> {
 | 
			
		||||
            Object value = templateParams.get(key);
 | 
			
		||||
            if (value == null) {
 | 
			
		||||
                throw exception(MAIL_SEND_TEMPLATE_PARAM_MISS, key);
 | 
			
		||||
            }
 | 
			
		||||
            return new KeyValue<>(key, value);
 | 
			
		||||
        }).collect(Collectors.toList());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -83,7 +83,7 @@ public class MailTemplateServiceImpl implements MailTemplateService {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void update(@Valid MailTemplateUpdateReqVO updateReqVO) {
 | 
			
		||||
        // 校验是否唯一
 | 
			
		||||
        // TODO @wangjingyi:参考下我在 account 给的唯一校验的说明。
 | 
			
		||||
        // TODO @wangjingyi:参考下我在 account 给的唯一校验的说明。DONE
 | 
			
		||||
        this.validateMailTemplateOnlyByCode(updateReqVO.getId(),updateReqVO.getCode());
 | 
			
		||||
        MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(updateReqVO);
 | 
			
		||||
        mailTemplateMapper.updateById(mailTemplateDO);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user