From 1bf1d3ee6a4125d7efd23be8ce1e6513fcae9ef5 Mon Sep 17 00:00:00 2001 From: wangjingyi Date: Wed, 30 Mar 2022 14:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dal/mysql/mail/MailAccountMapper.java | 13 +++++++ .../mail/impl/MailAccountServiceImpl.java | 35 +++++++------------ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailAccountMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailAccountMapper.java index f7e06862f..effeed1b9 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailAccountMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailAccountMapper.java @@ -5,8 +5,14 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.apache.ibatis.annotations.Mapper; +import java.util.Map; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_EXISTS; + @Mapper public interface MailAccountMapper extends BaseMapperX { @@ -20,4 +26,11 @@ public interface MailAccountMapper extends BaseMapperX { ); } + default MailAccountDO selectByParams(Map params){ + QueryWrapperX queryWrapperX = new QueryWrapperX(); + params.forEach((k , v)->{ + queryWrapperX.eqIfPresent((String) k, v); + }); + return this.selectOne(queryWrapperX); + }; } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailAccountServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailAccountServiceImpl.java index c4610222a..8c8900dcf 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailAccountServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailAccountServiceImpl.java @@ -1,19 +1,19 @@ package cn.iocoder.yudao.module.system.service.mail.impl; +import cn.hutool.core.util.StrUtil; import cn.hutool.extra.mail.MailAccount; import cn.hutool.extra.mail.MailUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; -import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailSendVO; +import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO; 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.service.mail.MailAccountService; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -27,9 +27,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOU /** - *

* 邮箱账号 Service 实现类 - *

* * @author wangjingyi * @since 2022-03-21 @@ -66,7 +64,6 @@ public class MailAccountServiceImpl implements MailAccountService { mailAccountMapper.updateById(mailAccountDO); } - @Override public void delete(Long id) { // 校验是否存在 @@ -90,20 +87,16 @@ public class MailAccountServiceImpl implements MailAccountService { } @Override - public void sendMail(MailSendVO mailSendVO) { - // FIXME 查询模版信息 查询模版多条时 使用规则是什么 - // 回复:选择某一条模板,进行发送邮件。 - List mailTemplateDOList = mailTemplateMapper.selectList( - "username",mailSendVO.getFrom() - ); + public void sendMail(MailReqVO mailReqVO) { + MailTemplateDO mailTemplateDO = mailTemplateMapper.selectById(mailReqVO.getTemplateId()); //查询账号信息 MailAccountDO mailAccountDO = mailAccountMapper.selectOne( - "from",mailSendVO.getFrom() + "from", mailReqVO.getFrom() ); - // FIXME 模版和邮件内容合成方式未知 - // 回复:参考短信的方式,通过 {name} {mobile} 这样的占位符。搜 formatSmsTemplateContent 方法 - String content = mailSendVO.getContent(); - String templateContent = ""; + String content = mailReqVO.getContent(); + Map params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content); + content = StrUtil.format(mailTemplateDO.getContent(), params); + // 后续功能 TODO :附件查询 //List fileIds = mailSendVO.getFileIds(); @@ -111,7 +104,7 @@ public class MailAccountServiceImpl implements MailAccountService { MailAccount account = MailAccountConvert.INSTANCE.convertAccount(mailAccountDO); //发送 - MailUtil.send(account , mailSendVO.getTos() , mailSendVO.getTitle() , mailSendVO.getContent() , false); + MailUtil.send(account , mailReqVO.getTos() , mailReqVO.getTitle() , content , false); } private void validateMailAccountExists(Long id) { @@ -121,12 +114,8 @@ public class MailAccountServiceImpl implements MailAccountService { } private void validateMailAccountOnly(Map params){ - // TODO wangjingyi:Service 里,不允许出现 MyBatis 操作。而是 Mapper 提供对应查询方法 - QueryWrapper queryWrapper = new QueryWrapper(); - params.forEach((k , v)->{ - queryWrapper.like(k , v); // TODO wangjingyi:账号,应该是 equlas,不能是 like - }); - if (mailAccountMapper.selectOne(queryWrapper) != null) { + MailAccountDO mailAccountDO = mailAccountMapper.selectByParams(params); + if (mailAccountDO != null) { throw exception(MAIL_ACCOUNT_EXISTS); } }