邮件模块修改注释、注解、修改泛型方法

This commit is contained in:
wangjingyi 2022-03-31 18:00:03 +08:00
parent 7d9a6cb2ef
commit e1d79b5ea9
16 changed files with 204 additions and 127 deletions

View File

@ -52,7 +52,7 @@ public class MailAccountController {
@ApiOperation("删除邮箱账号") @ApiOperation("删除邮箱账号")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('system:mail-account:delete')") @PreAuthorize("@ss.hasPermission('system:mail-account:delete')")
public CommonResult<Boolean> deleteMailAccount(@Valid @RequestParam Long id) { // TODO @wangjingyi不需要 @Valid 这里了解下 Validator public CommonResult<Boolean> deleteMailAccount(@RequestParam Long id) {
mailAccountService.delete(id); mailAccountService.delete(id);
return success(true); return success(true);
} }
@ -83,11 +83,4 @@ public class MailAccountController {
return success(MailAccountConvert.INSTANCE.convertList02(list)); return success(MailAccountConvert.INSTANCE.convertList02(list));
} }
@PostMapping("/send")
@ApiOperation("发送邮件")
@PreAuthorize("@ss.hasPermission('system:mail-account:send')")
public CommonResult<Boolean> sendMail(MailReqVO mailReqVO){ // TODO @wangjingyi应该是测试短信模板做到 MailTemplateController 参考下短信那的做法哈
mailAccountService.sendMail(mailReqVO);
return success(true);
}
} }

View File

@ -28,6 +28,7 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@RestController @RestController
@RequestMapping("/system/mail-log") @RequestMapping("/system/mail-log")
public class MailLogController { public class MailLogController {
@Autowired @Autowired
private MailLogService mailLogService; private MailLogService mailLogService;

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.controller.admin.mail;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateBaseVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateBaseVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO;
@ -27,8 +28,9 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@RestController @RestController
@RequestMapping("/system/mail-template") @RequestMapping("/system/mail-template")
public class MailTemplateController { public class MailTemplateController {
@Autowired @Autowired
MailTemplateService mailTempleService; // TODO @wangjingyiprivate和上面要空一行 MailTemplateService mailTempleService;
@PostMapping("/create") @PostMapping("/create")
@ApiOperation("创建邮箱模版") @ApiOperation("创建邮箱模版")
@ -64,7 +66,7 @@ public class MailTemplateController {
@GetMapping("/page") @GetMapping("/page")
@ApiOperation("获得邮箱模版分页") @ApiOperation("获得邮箱模版分页")
@PreAuthorize("@ss.hasPermission('system:mail-account:query')") @PreAuthorize("@ss.hasPermission('system:mail-template:query')")
public CommonResult<PageResult<MailTemplateBaseVO>> getMailTemplatePage(@Valid MailTemplatePageReqVO pageReqVO) { public CommonResult<PageResult<MailTemplateBaseVO>> getMailTemplatePage(@Valid MailTemplatePageReqVO pageReqVO) {
PageResult<MailTemplateDO> pageResult = mailTempleService.getMailTemplatePage(pageReqVO); PageResult<MailTemplateDO> pageResult = mailTempleService.getMailTemplatePage(pageReqVO);
return success(MailTemplateConvert.INSTANCE.convertPage(pageResult)); return success(MailTemplateConvert.INSTANCE.convertPage(pageResult));
@ -78,4 +80,12 @@ public class MailTemplateController {
list.sort(Comparator.comparing(MailTemplateDO::getId)); list.sort(Comparator.comparing(MailTemplateDO::getId));
return success(MailTemplateConvert.INSTANCE.convertList02(list)); return success(MailTemplateConvert.INSTANCE.convertList02(list));
} }
@PostMapping("/send")
@ApiOperation("发送邮件")
@PreAuthorize("@ss.hasPermission('system:mail-template:send')")
public CommonResult<Boolean> sendMail(@Valid @RequestBody MailReqVO mailReqVO){
mailTempleService.sendMail(mailReqVO);
return success(true);
}
} }

View File

@ -3,6 +3,9 @@ package cn.iocoder.yudao.module.system.controller.admin.mail.vo.account;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotNull;
@ApiModel("管理后台 - 邮箱账号基类 Base VO") @ApiModel("管理后台 - 邮箱账号基类 Base VO")
@Data @Data
public class MailAccountBaseVO { public class MailAccountBaseVO {
@ -11,6 +14,7 @@ public class MailAccountBaseVO {
private String from; private String from;
@ApiModelProperty(value = "用户名" , required = true , example = "yudao") @ApiModelProperty(value = "用户名" , required = true , example = "yudao")
@NotNull(message = "用户名必填")
private String username; private String username;
@ApiModelProperty(value = "密码" , required = true , example = "123456") @ApiModelProperty(value = "密码" , required = true , example = "123456")

View File

@ -9,7 +9,7 @@ import java.util.List;
@ApiModel("管理后台 - 邮件发送 Req VO") @ApiModel("管理后台 - 邮件发送 Req VO")
@Data @Data
public class MailReqVO { // TODO @wangjingyi1参数校验2ReqVO public class MailReqVO {
@ApiModelProperty(value = "邮箱" , required = true , example = "yudaoyuanma@123.com") @ApiModelProperty(value = "邮箱" , required = true , example = "yudaoyuanma@123.com")
@NotNull(message = "邮箱账号不能为空") @NotNull(message = "邮箱账号不能为空")

View File

@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotNull;
@ApiModel("管理后台 - 邮箱模版基类 Base VO") @ApiModel("管理后台 - 邮箱模版基类 Base VO")
@Data @Data
public class MailTemplateBaseVO { public class MailTemplateBaseVO {
@ -14,6 +16,7 @@ public class MailTemplateBaseVO {
private String name; private String name;
@ApiModelProperty("标识") @ApiModelProperty("标识")
@NotNull(message = "邮箱模版code不能为空")
private String code; private String code;
@ApiModelProperty("发件人") @ApiModelProperty("发件人")

View File

@ -11,34 +11,45 @@ import lombok.experimental.Accessors;
import java.io.Serializable; import java.io.Serializable;
@TableName(value = "system_mail_account", autoResultMap = true)
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Accessors(chain = true) @Accessors(chain = true)
@ApiModel(value="MailAccount对象", description="邮箱账号") // TODO @wangjingyi不需要 swagger 注解
@TableName(value = "system_mail_account", autoResultMap = true) // TODO @wangjingyi这个放在最上面关键字段
public class MailAccountDO extends BaseDO implements Serializable { public class MailAccountDO extends BaseDO implements Serializable {
// TODO @wangjingyi每个字段的注释字段名如果一直不用 @TableField /**
* 主键
@TableId */
private Long id; private Long id;
@TableField("from") /**
* 邮箱
*/
private String from; private String from;
@TableField("username") /**
* 用户名
*/
private String username; private String username;
@TableField("password") /**
* 密码
*/
private String password; private String password;
@TableField("host") /**
* 主机
*/
private String host; private String host;
@TableField("port") /**
* 端口
*/
private Integer port; private Integer port;
@TableField("sslEnable") /**
* 是否开启ssl
*/
private Boolean sslEnable; private Boolean sslEnable;

View File

@ -5,6 +5,8 @@ import java.sql.Timestamp;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable; import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -18,44 +20,65 @@ import lombok.experimental.Accessors;
* @author wangjingyi * @author wangjingyi
* @since 2022-03-21 * @since 2022-03-21
*/ */
@TableName(value = "system_mail_log", autoResultMap = true)
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Accessors(chain = true) @Accessors(chain = true)
@ApiModel(value="SystemMailLog对象", description="")
public class MailLogDO extends BaseDO implements Serializable { public class MailLogDO extends BaseDO implements Serializable {
private static final long serialVersionUID = 1L; /**
@TableId * 主键
*/
private Long id; private Long id;
@TableField("account_code") /**
* 邮箱账号编号
*/
private String accountCode; private String accountCode;
@TableField("from") /**
* 邮箱账号
*/
private String from; private String from;
@TableField("template_id") /**
* 模版主键
*/
private String templateId; private String templateId;
@TableField("template_code") /**
* 模版编号
*/
private String templateCode; private String templateCode;
@TableField("title") /**
* 标题
*/
private String title; private String title;
@TableField("content") /**
* 内容
*/
private String content; private String content;
@TableField("to") /**
* 收件人
*/
private String to; private String to;
@TableField("sendTime") /**
* 发送时间
*/
private Timestamp sendTime; private Timestamp sendTime;
@TableField("sendStatus") /**
* 发送状态
*/
private Boolean sendStatus; private Boolean sendStatus;
@TableField("sendResult") /**
* 发送结果
*/
private String sendResult; private String sendResult;

View File

@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable; import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ -19,35 +21,50 @@ import lombok.experimental.Accessors;
* @author wangjingyi * @author wangjingyi
* @since 2022-03-21 * @since 2022-03-21
*/ */
@TableName(value = "system_mail_template", autoResultMap = true)
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Accessors(chain = true) @Accessors(chain = true)
@ApiModel(value="SystemMailTemplate对象", description="")
public class MailTemplateDO extends BaseDO implements Serializable { public class MailTemplateDO extends BaseDO implements Serializable {
private static final long serialVersionUID = 1L; /**
@TableId * 主键
*/
private Long id; private Long id;
@TableField("name") /**
* 模版名称
*/
private String name; private String name;
@TableField("code") /**
* 模版编号
*/
private String code; private String code;
@TableField("username") /**
* 用户名
*/
private String username; private String username;
@TableField("title") /**
* 标题
*/
private String title; private String title;
@TableField("content") /**
* 内容
*/
private String content; private String content;
@TableField("status") /**
* 状态
*/
private String status; private String status;
@TableField("remark") /**
* 备注
*/
private String remark; private String remark;

View File

@ -22,12 +22,9 @@ public interface MailAccountMapper extends BaseMapperX<MailAccountDO> {
); );
} }
// TODO @wangjingyi不要提供这样的泛的方法而是明确的查询方法 default MailAccountDO selectByUserName(String userName){
default MailAccountDO selectByParams(Map params){
QueryWrapperX queryWrapperX = new QueryWrapperX<MailAccountDO>(); QueryWrapperX queryWrapperX = new QueryWrapperX<MailAccountDO>();
params.forEach((k , v)->{ queryWrapperX.eqIfPresent("username", userName);
queryWrapperX.eqIfPresent((String) k, v); return this.selectOne(queryWrapperX);
});
return this.selecOne(queryWrapperX);
}; };
} }

View File

@ -22,4 +22,7 @@ public interface MailTemplateMapper extends BaseMapperX<MailTemplateDO> {
); );
} }
default MailTemplateDO selectOneByCode(String code){
return selectOne("code" , code);
};
} }

View File

@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
import javax.validation.Valid;
import java.util.List; import java.util.List;
@ -22,46 +23,47 @@ public interface MailAccountService {
/** /**
* 创建邮箱账号 // TODO @wangjingyi方法描述和参数要空行 * 创建邮箱账号 // TODO @wangjingyi方法描述和参数要空行
* @param createReqVO *
* @return * @param createReqVO 邮箱账号信息
* @return 编号
*/ */
Long create(MailAccountCreateReqVO createReqVO); Long create(@Valid MailAccountCreateReqVO createReqVO);
/** /**
* 修改邮箱账号 * 修改邮箱账号
* @param updateReqVO *
* @param updateReqVO 邮箱账号信息
*/ */
void update(MailAccountUpdateReqVO updateReqVO); void update(@Valid MailAccountUpdateReqVO updateReqVO);
/** /**
* 删除邮箱账号 * 删除邮箱账号
* @param id *
* @param id 编号
*/ */
void delete(Long id); void delete(Long id);
/** /**
* 获取邮箱账号信息 * 获取邮箱账号信息
* @param id *
* @return * @param id 编号
* @return 邮箱账号信息
*/ */
MailAccountDO getMailAccount(Long id); MailAccountDO getMailAccount(Long id);
/** /**
* 获取邮箱账号分页信息 * 获取邮箱账号分页信息
* @param pageReqVO *
* @return * @param pageReqVO 邮箱账号分页参数
* @return 邮箱账号分页信息
*/ */
PageResult<MailAccountDO> getMailAccountPage(MailAccountPageReqVO pageReqVO); PageResult<MailAccountDO> getMailAccountPage(MailAccountPageReqVO pageReqVO);
/** /**
* 获取邮箱数组信息 * 获取邮箱数组信息
* @return *
* @return 邮箱账号信息数组
*/ */
List<MailAccountDO> getMailAccountList(); List<MailAccountDO> getMailAccountList();
/**
* 发送邮件
* @param mailReqVO
*/
void sendMail(MailReqVO mailReqVO);
} }

View File

@ -1,11 +1,13 @@
package cn.iocoder.yudao.module.system.service.mail; package cn.iocoder.yudao.module.system.service.mail;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
import javax.validation.Valid;
import java.util.List; import java.util.List;
/** /**
@ -16,44 +18,52 @@ import java.util.List;
*/ */
public interface MailTemplateService { public interface MailTemplateService {
// TODO @wangjingyi注释完整参数校验还是要做的
/** /**
* 邮箱模版创建 * 邮箱模版创建
* @param createReqVO * @param createReqVO 邮箱信息
* @return * @return 编号
*/ */
Long create(MailTemplateCreateReqVO createReqVO); Long create(@Valid MailTemplateCreateReqVO createReqVO);
/** /**
* 邮箱模版修改 * 邮箱模版修改
* @param updateReqVO * @param updateReqVO 邮箱信息
*/ */
void update(MailTemplateUpdateReqVO updateReqVO); void update(@Valid MailTemplateUpdateReqVO updateReqVO);
/** /**
* 邮箱模版删除 * 邮箱模版删除
* @param id * @param id 编号
*/ */
void delete(Long id); void delete(Long id);
/** /**
* 获取邮箱模版 * 获取邮箱模版
* @param id *
* @return * @param id 编号
* @return 邮件模版
*/ */
MailTemplateDO getMailTemplate(Long id); MailTemplateDO getMailTemplate(Long id);
/** /**
* 获取邮箱模版分页 * 获取邮箱模版分页
* @param pageReqVO *
* @return * @param pageReqVO 模版信息
* @return 邮箱模版分页信息
*/ */
PageResult<MailTemplateDO> getMailTemplatePage(MailTemplatePageReqVO pageReqVO); PageResult<MailTemplateDO> getMailTemplatePage(MailTemplatePageReqVO pageReqVO);
/** /**
* 获取邮箱模板数组 * 获取邮箱模板数组
* @return *
* @return 模版数组
*/ */
List<MailTemplateDO> getMailTemplateList(); List<MailTemplateDO> getMailTemplateList();
/**
* 发送邮件
*
* @param mailReqVO 邮件发送信息
*/
void sendMail(MailReqVO mailReqVO);
} }

View File

@ -15,6 +15,7 @@ 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.dal.mysql.mail.MailTemplateMapper;
import cn.iocoder.yudao.module.system.service.mail.MailAccountService; import cn.iocoder.yudao.module.system.service.mail.MailAccountService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
@ -33,7 +34,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOU
* @since 2022-03-21 * @since 2022-03-21
*/ */
@Service @Service
// TODO @wangjingyi需要 @Validated 注解开启参数校验 @Validated
public class MailAccountServiceImpl implements MailAccountService { public class MailAccountServiceImpl implements MailAccountService {
@Resource @Resource
@ -45,9 +46,7 @@ public class MailAccountServiceImpl implements MailAccountService {
@Override @Override
public Long create(MailAccountCreateReqVO createReqVO) { public Long create(MailAccountCreateReqVO createReqVO) {
// username 要校验唯一 // username 要校验唯一
Map<String , String> map = new HashMap<>(); this.validateMailAccountOnlyByUserName(createReqVO.getUsername());
map.put("username" , createReqVO.getUsername());
this.validateMailAccountOnly(map);
MailAccountDO mailAccountDO = MailAccountConvert.INSTANCE.convert(createReqVO); MailAccountDO mailAccountDO = MailAccountConvert.INSTANCE.convert(createReqVO);
mailAccountMapper.insert(mailAccountDO); mailAccountMapper.insert(mailAccountDO);
return mailAccountDO.getId(); return mailAccountDO.getId();
@ -55,10 +54,8 @@ public class MailAccountServiceImpl implements MailAccountService {
@Override @Override
public void update(MailAccountUpdateReqVO updateReqVO) { public void update(MailAccountUpdateReqVO updateReqVO) {
// username 要校验唯一 TODO @wangjingyi不要用 map 参数 // username 要校验唯一
Map<String , String> map = new HashMap<>(); this.validateMailAccountOnlyByUserName(updateReqVO.getUsername());
map.put("username" , updateReqVO.getUsername());
this.validateMailAccountOnly(map); // TODO @wangjingyi如果 username 是自己用呢要排除下自己呀
MailAccountDO mailAccountDO = MailAccountConvert.INSTANCE.convert(updateReqVO); MailAccountDO mailAccountDO = MailAccountConvert.INSTANCE.convert(updateReqVO);
// 校验是否存在 // 校验是否存在
this.validateMailAccountExists(mailAccountDO.getId()); this.validateMailAccountExists(mailAccountDO.getId());
@ -87,35 +84,14 @@ public class MailAccountServiceImpl implements MailAccountService {
return mailAccountMapper.selectList(); return mailAccountMapper.selectList();
} }
@Override
public void sendMail(MailReqVO mailReqVO) {
MailTemplateDO mailTemplateDO = mailTemplateMapper.selectById(mailReqVO.getTemplateId());
//查询账号信息
MailAccountDO mailAccountDO = mailAccountMapper.selectOne(
"from", mailReqVO.getFrom()
);
String content = mailReqVO.getContent();
Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content);
content = StrUtil.format(mailTemplateDO.getContent(), params);
// 后续功能 TODO 附件查询
//List<String> fileIds = mailSendVO.getFileIds();
//装载账号信息
MailAccount account = MailAccountConvert.INSTANCE.convertAccount(mailAccountDO);
//发送
MailUtil.send(account , mailReqVO.getTos() , mailReqVO.getTitle() , content , false);
}
private void validateMailAccountExists(Long id) { private void validateMailAccountExists(Long id) {
if (mailAccountMapper.selectById(id) == null) { if (mailAccountMapper.selectById(id) == null) {
throw exception(MAIL_ACCOUNT_NOT_EXISTS); throw exception(MAIL_ACCOUNT_NOT_EXISTS);
} }
} }
private void validateMailAccountOnly(Map params){ private void validateMailAccountOnlyByUserName(String userName){
MailAccountDO mailAccountDO = mailAccountMapper.selectByParams(params); MailAccountDO mailAccountDO = mailAccountMapper.selectByUserName(userName);
if (mailAccountDO != null) { if (mailAccountDO != null) {
throw exception(MAIL_ACCOUNT_EXISTS); throw exception(MAIL_ACCOUNT_EXISTS);
} }

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.system.dal.mysql.mail.MailLogMapper;
import cn.iocoder.yudao.module.system.service.mail.MailLogService; import cn.iocoder.yudao.module.system.service.mail.MailLogService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.List; import java.util.List;
@ -19,6 +20,7 @@ import java.util.List;
* @since 2022-03-21 * @since 2022-03-21
*/ */
@Service @Service
@Validated
public class MailLogServiceImpl implements MailLogService { public class MailLogServiceImpl implements MailLogService {
@Autowired @Autowired

View File

@ -1,18 +1,28 @@
package cn.iocoder.yudao.module.system.service.mail.impl; 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.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
import cn.iocoder.yudao.module.system.convert.mail.MailTemplateConvert; import cn.iocoder.yudao.module.system.convert.mail.MailTemplateConvert;
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.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.dal.mysql.mail.MailTemplateMapper;
import cn.iocoder.yudao.module.system.service.mail.MailAccountService;
import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -28,29 +38,27 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPL
* @since 2022-03-21 * @since 2022-03-21
*/ */
@Service @Service
// TODO @wangjingyi需要 @Validated 注解开启参数校验 @Validated
public class MailTemplateServiceImpl implements MailTemplateService { public class MailTemplateServiceImpl implements MailTemplateService {
@Resource @Resource
private MailTemplateMapper mailTemplateMapper; private MailTemplateMapper mailTemplateMapper;
@Resource
private MailAccountMapper mailAccountMapper;
@Override @Override
public Long create(MailTemplateCreateReqVO createReqVO) { public Long create(MailTemplateCreateReqVO createReqVO) {
// name 要校验唯一 // code 要校验唯一
Map<String , String> map = new HashMap<>(); this.validateMailTemplateOnlyByCode(createReqVO.getCode());
map.put("name" , createReqVO.getName()); // TODO @wangjingyi模板名重复没关系的code 不能重复
this.validateMailTemplateOnly(map);
MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(createReqVO); MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(createReqVO);
mailTemplateMapper.insert(mailTemplateDO); mailTemplateMapper.insert(mailTemplateDO);
return mailTemplateDO.getId(); return mailTemplateDO.getId();
} }
@Override @Override
public void update(MailTemplateUpdateReqVO updateReqVO) { public void update(@Valid MailTemplateUpdateReqVO updateReqVO) {
// username 要校验唯一 // code 要校验唯一
Map<String , String> map = new HashMap<>(); this.validateMailTemplateOnlyByCode(updateReqVO.getCode());
map.put("username" , updateReqVO.getUsername()); // TODO @wangjingyi模板名重复没关系的code 不能重复
this.validateMailTemplateOnly(map);
MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(updateReqVO); MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(updateReqVO);
// 校验是否存在 // 校验是否存在
this.validateMailTemplateExists(mailTemplateDO.getId()); this.validateMailTemplateExists(mailTemplateDO.getId());
@ -75,18 +83,35 @@ public class MailTemplateServiceImpl implements MailTemplateService {
@Override @Override
public List<MailTemplateDO> getMailTemplateList() {return mailTemplateMapper.selectList();} public List<MailTemplateDO> getMailTemplateList() {return mailTemplateMapper.selectList();}
@Override
public void sendMail(MailReqVO mailReqVO) {
MailTemplateDO mailTemplateDO = mailTemplateMapper.selectById(mailReqVO.getTemplateId());
//查询账号信息
MailAccountDO mailAccountDO = mailAccountMapper.selectOne(
"from", mailReqVO.getFrom()
);
String content = mailReqVO.getContent();
Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content);
content = StrUtil.format(mailTemplateDO.getContent(), params);
// 后续功能 TODO 附件查询
//List<String> fileIds = mailSendVO.getFileIds();
//装载账号信息
MailAccount account = MailAccountConvert.INSTANCE.convertAccount(mailAccountDO);
//发送
MailUtil.send(account , mailReqVO.getTos() , mailReqVO.getTitle() , content , false);
}
private void validateMailTemplateExists(Long id) { private void validateMailTemplateExists(Long id) {
if (mailTemplateMapper.selectById(id) == null) { if (mailTemplateMapper.selectById(id) == null) {
throw exception(MAIL_TEMPLATE_NOT_EXISTS); throw exception(MAIL_TEMPLATE_NOT_EXISTS);
} }
} }
private void validateMailTemplateOnly(Map params){ private void validateMailTemplateOnlyByCode(String code){
QueryWrapper queryWrapper = new QueryWrapper<MailTemplateDO>(); if (mailTemplateMapper.selectOneByCode(code) != null) {
params.forEach((k , v)->{
queryWrapper.like(k , v);
});
if (mailTemplateMapper.selectOne(queryWrapper) != null) {
throw exception(MAIL_TEMPLATE_EXISTS); throw exception(MAIL_TEMPLATE_EXISTS);
} }
} }