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

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("删除邮箱账号")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@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);
return success(true);
}
@ -83,11 +83,4 @@ public class MailAccountController {
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
@RequestMapping("/system/mail-log")
public class MailLogController {
@Autowired
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.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.MailTemplateCreateReqVO;
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
@RequestMapping("/system/mail-template")
public class MailTemplateController {
@Autowired
MailTemplateService mailTempleService; // TODO @wangjingyiprivate和上面要空一行
MailTemplateService mailTempleService;
@PostMapping("/create")
@ApiOperation("创建邮箱模版")
@ -64,7 +66,7 @@ public class MailTemplateController {
@GetMapping("/page")
@ApiOperation("获得邮箱模版分页")
@PreAuthorize("@ss.hasPermission('system:mail-account:query')")
@PreAuthorize("@ss.hasPermission('system:mail-template:query')")
public CommonResult<PageResult<MailTemplateBaseVO>> getMailTemplatePage(@Valid MailTemplatePageReqVO pageReqVO) {
PageResult<MailTemplateDO> pageResult = mailTempleService.getMailTemplatePage(pageReqVO);
return success(MailTemplateConvert.INSTANCE.convertPage(pageResult));
@ -78,4 +80,12 @@ public class MailTemplateController {
list.sort(Comparator.comparing(MailTemplateDO::getId));
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.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
@ApiModel("管理后台 - 邮箱账号基类 Base VO")
@Data
public class MailAccountBaseVO {
@ -11,6 +14,7 @@ public class MailAccountBaseVO {
private String from;
@ApiModelProperty(value = "用户名" , required = true , example = "yudao")
@NotNull(message = "用户名必填")
private String username;
@ApiModelProperty(value = "密码" , required = true , example = "123456")

View File

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

View File

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

View File

@ -11,34 +11,45 @@ import lombok.experimental.Accessors;
import java.io.Serializable;
@TableName(value = "system_mail_account", autoResultMap = true)
@Data
@EqualsAndHashCode(callSuper = 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 {
// TODO @wangjingyi每个字段的注释字段名如果一直不用 @TableField
@TableId
/**
* 主键
*/
private Long id;
@TableField("from")
/**
* 邮箱
*/
private String from;
@TableField("username")
/**
* 用户名
*/
private String username;
@TableField("password")
/**
* 密码
*/
private String password;
@TableField("host")
/**
* 主机
*/
private String host;
@TableField("port")
/**
* 端口
*/
private Integer port;
@TableField("sslEnable")
/**
* 是否开启ssl
*/
private Boolean sslEnable;

View File

@ -5,6 +5,8 @@ import java.sql.Timestamp;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -18,44 +20,65 @@ import lombok.experimental.Accessors;
* @author wangjingyi
* @since 2022-03-21
*/
@TableName(value = "system_mail_log", autoResultMap = true)
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value="SystemMailLog对象", description="")
public class MailLogDO extends BaseDO implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
/**
* 主键
*/
private Long id;
@TableField("account_code")
/**
* 邮箱账号编号
*/
private String accountCode;
@TableField("from")
/**
* 邮箱账号
*/
private String from;
@TableField("template_id")
/**
* 模版主键
*/
private String templateId;
@TableField("template_code")
/**
* 模版编号
*/
private String templateCode;
@TableField("title")
/**
* 标题
*/
private String title;
@TableField("content")
/**
* 内容
*/
private String content;
@TableField("to")
/**
* 收件人
*/
private String to;
@TableField("sendTime")
/**
* 发送时间
*/
private Timestamp sendTime;
@TableField("sendStatus")
/**
* 发送状态
*/
private Boolean sendStatus;
@TableField("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.TableField;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ -19,35 +21,50 @@ import lombok.experimental.Accessors;
* @author wangjingyi
* @since 2022-03-21
*/
@TableName(value = "system_mail_template", autoResultMap = true)
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value="SystemMailTemplate对象", description="")
public class MailTemplateDO extends BaseDO implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
/**
* 主键
*/
private Long id;
@TableField("name")
/**
* 模版名称
*/
private String name;
@TableField("code")
/**
* 模版编号
*/
private String code;
@TableField("username")
/**
* 用户名
*/
private String username;
@TableField("title")
/**
* 标题
*/
private String title;
@TableField("content")
/**
* 内容
*/
private String content;
@TableField("status")
/**
* 状态
*/
private String status;
@TableField("remark")
/**
* 备注
*/
private String remark;

View File

@ -22,12 +22,9 @@ public interface MailAccountMapper extends BaseMapperX<MailAccountDO> {
);
}
// TODO @wangjingyi不要提供这样的泛的方法而是明确的查询方法
default MailAccountDO selectByParams(Map params){
default MailAccountDO selectByUserName(String userName){
QueryWrapperX queryWrapperX = new QueryWrapperX<MailAccountDO>();
params.forEach((k , v)->{
queryWrapperX.eqIfPresent((String) k, v);
});
return this.selecOne(queryWrapperX);
queryWrapperX.eqIfPresent("username", userName);
return this.selectOne(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.dal.dataobject.mail.MailAccountDO;
import javax.validation.Valid;
import java.util.List;
@ -22,46 +23,47 @@ public interface MailAccountService {
/**
* 创建邮箱账号 // 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);
/**
* 获取邮箱账号信息
* @param id
* @return
*
* @param id 编号
* @return 邮箱账号信息
*/
MailAccountDO getMailAccount(Long id);
/**
* 获取邮箱账号分页信息
* @param pageReqVO
* @return
*
* @param pageReqVO 邮箱账号分页参数
* @return 邮箱账号分页信息
*/
PageResult<MailAccountDO> getMailAccountPage(MailAccountPageReqVO pageReqVO);
/**
* 获取邮箱数组信息
* @return
*
* @return 邮箱账号信息数组
*/
List<MailAccountDO> getMailAccountList();
/**
* 发送邮件
* @param mailReqVO
*/
void sendMail(MailReqVO mailReqVO);
}

View File

@ -1,11 +1,13 @@
package cn.iocoder.yudao.module.system.service.mail;
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.MailTemplatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
import javax.validation.Valid;
import java.util.List;
/**
@ -16,44 +18,52 @@ import java.util.List;
*/
public interface MailTemplateService {
// TODO @wangjingyi注释完整参数校验还是要做的
/**
* 邮箱模版创建
* @param createReqVO
* @return
* @param createReqVO 邮箱信息
* @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);
/**
* 获取邮箱模版
* @param id
* @return
*
* @param id 编号
* @return 邮件模版
*/
MailTemplateDO getMailTemplate(Long id);
/**
* 获取邮箱模版分页
* @param pageReqVO
* @return
*
* @param pageReqVO 模版信息
* @return 邮箱模版分页信息
*/
PageResult<MailTemplateDO> getMailTemplatePage(MailTemplatePageReqVO pageReqVO);
/**
* 获取邮箱模板数组
* @return
*
* @return 模版数组
*/
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.service.mail.MailAccountService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.HashMap;
@ -33,7 +34,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOU
* @since 2022-03-21
*/
@Service
// TODO @wangjingyi需要 @Validated 注解开启参数校验
@Validated
public class MailAccountServiceImpl implements MailAccountService {
@Resource
@ -45,9 +46,7 @@ public class MailAccountServiceImpl implements MailAccountService {
@Override
public Long create(MailAccountCreateReqVO createReqVO) {
// username 要校验唯一
Map<String , String> map = new HashMap<>();
map.put("username" , createReqVO.getUsername());
this.validateMailAccountOnly(map);
this.validateMailAccountOnlyByUserName(createReqVO.getUsername());
MailAccountDO mailAccountDO = MailAccountConvert.INSTANCE.convert(createReqVO);
mailAccountMapper.insert(mailAccountDO);
return mailAccountDO.getId();
@ -55,10 +54,8 @@ public class MailAccountServiceImpl implements MailAccountService {
@Override
public void update(MailAccountUpdateReqVO updateReqVO) {
// username 要校验唯一 TODO @wangjingyi不要用 map 参数
Map<String , String> map = new HashMap<>();
map.put("username" , updateReqVO.getUsername());
this.validateMailAccountOnly(map); // TODO @wangjingyi如果 username 是自己用呢要排除下自己呀
// username 要校验唯一
this.validateMailAccountOnlyByUserName(updateReqVO.getUsername());
MailAccountDO mailAccountDO = MailAccountConvert.INSTANCE.convert(updateReqVO);
// 校验是否存在
this.validateMailAccountExists(mailAccountDO.getId());
@ -87,35 +84,14 @@ public class MailAccountServiceImpl implements MailAccountService {
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) {
if (mailAccountMapper.selectById(id) == null) {
throw exception(MAIL_ACCOUNT_NOT_EXISTS);
}
}
private void validateMailAccountOnly(Map params){
MailAccountDO mailAccountDO = mailAccountMapper.selectByParams(params);
private void validateMailAccountOnlyByUserName(String userName){
MailAccountDO mailAccountDO = mailAccountMapper.selectByUserName(userName);
if (mailAccountDO != null) {
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.List;
@ -19,6 +20,7 @@ import java.util.List;
* @since 2022-03-21
*/
@Service
@Validated
public class MailLogServiceImpl implements MailLogService {
@Autowired

View File

@ -1,18 +1,28 @@
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.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.MailTemplatePageReqVO;
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.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 cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -28,29 +38,27 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPL
* @since 2022-03-21
*/
@Service
// TODO @wangjingyi需要 @Validated 注解开启参数校验
@Validated
public class MailTemplateServiceImpl implements MailTemplateService {
@Resource
private MailTemplateMapper mailTemplateMapper;
@Resource
private MailAccountMapper mailAccountMapper;
@Override
public Long create(MailTemplateCreateReqVO createReqVO) {
// name 要校验唯一
Map<String , String> map = new HashMap<>();
map.put("name" , createReqVO.getName()); // TODO @wangjingyi模板名重复没关系的code 不能重复
this.validateMailTemplateOnly(map);
// code 要校验唯一
this.validateMailTemplateOnlyByCode(createReqVO.getCode());
MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(createReqVO);
mailTemplateMapper.insert(mailTemplateDO);
return mailTemplateDO.getId();
}
@Override
public void update(MailTemplateUpdateReqVO updateReqVO) {
// username 要校验唯一
Map<String , String> map = new HashMap<>();
map.put("username" , updateReqVO.getUsername()); // TODO @wangjingyi模板名重复没关系的code 不能重复
this.validateMailTemplateOnly(map);
public void update(@Valid MailTemplateUpdateReqVO updateReqVO) {
// code 要校验唯一
this.validateMailTemplateOnlyByCode(updateReqVO.getCode());
MailTemplateDO mailTemplateDO = MailTemplateConvert.INSTANCE.convert(updateReqVO);
// 校验是否存在
this.validateMailTemplateExists(mailTemplateDO.getId());
@ -75,18 +83,35 @@ public class MailTemplateServiceImpl implements MailTemplateService {
@Override
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) {
if (mailTemplateMapper.selectById(id) == null) {
throw exception(MAIL_TEMPLATE_NOT_EXISTS);
}
}
private void validateMailTemplateOnly(Map params){
QueryWrapper queryWrapper = new QueryWrapper<MailTemplateDO>();
params.forEach((k , v)->{
queryWrapper.like(k , v);
});
if (mailTemplateMapper.selectOne(queryWrapper) != null) {
private void validateMailTemplateOnlyByCode(String code){
if (mailTemplateMapper.selectOneByCode(code) != null) {
throw exception(MAIL_TEMPLATE_EXISTS);
}
}