From e1d79b5ea948afe1d8c5b7524061824fe0996f84 Mon Sep 17 00:00:00 2001 From: wangjingyi Date: Thu, 31 Mar 2022 18:00:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E6=A8=A1=E5=9D=97=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=B3=A8=E9=87=8A=E3=80=81=E6=B3=A8=E8=A7=A3=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=9B=E5=9E=8B=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/mail/MailAccountController.java | 9 +-- .../admin/mail/MailLogController.java | 1 + .../admin/mail/MailTemplateController.java | 14 ++++- .../mail/vo/account/MailAccountBaseVO.java | 4 ++ .../admin/mail/vo/send/MailReqVO.java | 2 +- .../mail/vo/template/MailTemplateBaseVO.java | 3 + .../dal/dataobject/mail/MailAccountDO.java | 33 +++++++---- .../system/dal/dataobject/mail/MailLogDO.java | 49 +++++++++++----- .../dal/dataobject/mail/MailTemplateDO.java | 37 ++++++++---- .../dal/mysql/mail/MailAccountMapper.java | 9 +-- .../dal/mysql/mail/MailTemplateMapper.java | 3 + .../service/mail/MailAccountService.java | 34 +++++------ .../service/mail/MailTemplateService.java | 36 +++++++----- .../mail/impl/MailAccountServiceImpl.java | 38 +++---------- .../service/mail/impl/MailLogServiceImpl.java | 2 + .../mail/impl/MailTemplateServiceImpl.java | 57 +++++++++++++------ 16 files changed, 204 insertions(+), 127 deletions(-) diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailAccountController.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailAccountController.java index 660ea4ab4..c16c7a60b 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailAccountController.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailAccountController.java @@ -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 deleteMailAccount(@Valid @RequestParam Long id) { // TODO @wangjingyi:不需要 @Valid 这里。了解下 Validator + public CommonResult 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 sendMail(MailReqVO mailReqVO){ // TODO @wangjingyi:应该是测试短信模板,做到 MailTemplateController 里。参考下短信那的做法哈 - mailAccountService.sendMail(mailReqVO); - return success(true); - } } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailLogController.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailLogController.java index f6375f290..1cad6a02a 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailLogController.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailLogController.java @@ -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; diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailTemplateController.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailTemplateController.java index 258dc3856..cffd7f980 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailTemplateController.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailTemplateController.java @@ -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 @wangjingyi:private;和上面要空一行; + 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> getMailTemplatePage(@Valid MailTemplatePageReqVO pageReqVO) { PageResult 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 sendMail(@Valid @RequestBody MailReqVO mailReqVO){ + mailTempleService.sendMail(mailReqVO); + return success(true); + } } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/account/MailAccountBaseVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/account/MailAccountBaseVO.java index 49400316b..870d06e48 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/account/MailAccountBaseVO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/account/MailAccountBaseVO.java @@ -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") diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/send/MailReqVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/send/MailReqVO.java index 0bd9d5d22..de75cddc9 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/send/MailReqVO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/send/MailReqVO.java @@ -9,7 +9,7 @@ import java.util.List; @ApiModel("管理后台 - 邮件发送 Req VO") @Data -public class MailReqVO { // TODO @wangjingyi:1)参数校验;2)ReqVO +public class MailReqVO { @ApiModelProperty(value = "邮箱" , required = true , example = "yudaoyuanma@123.com") @NotNull(message = "邮箱账号不能为空") diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/template/MailTemplateBaseVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/template/MailTemplateBaseVO.java index b718e2918..074ed69d2 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/template/MailTemplateBaseVO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/vo/template/MailTemplateBaseVO.java @@ -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("发件人") diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailAccountDO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailAccountDO.java index a7db81860..0d603cb99 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailAccountDO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailAccountDO.java @@ -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; diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java index 8183174a1..4fc0c0a5c 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailLogDO.java @@ -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; diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java index e2d5a6d8c..83ebb533d 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/mail/MailTemplateDO.java @@ -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; 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 6785e33ec..d0a43e679 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 @@ -22,12 +22,9 @@ public interface MailAccountMapper extends BaseMapperX { ); } - // TODO @wangjingyi:不要提供这样的泛的方法,而是明确的查询方法 - default MailAccountDO selectByParams(Map params){ + default MailAccountDO selectByUserName(String userName){ QueryWrapperX queryWrapperX = new QueryWrapperX(); - params.forEach((k , v)->{ - queryWrapperX.eqIfPresent((String) k, v); - }); - return this.selecOne(queryWrapperX); + queryWrapperX.eqIfPresent("username", userName); + return this.selectOne(queryWrapperX); }; } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailTemplateMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailTemplateMapper.java index 06e76d9ab..635a2bf90 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailTemplateMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailTemplateMapper.java @@ -22,4 +22,7 @@ public interface MailTemplateMapper extends BaseMapperX { ); } + default MailTemplateDO selectOneByCode(String code){ + return selectOne("code" , code); + }; } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailAccountService.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailAccountService.java index 572aa3ffb..9792c6cc5 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailAccountService.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailAccountService.java @@ -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 getMailAccountPage(MailAccountPageReqVO pageReqVO); /** * 获取邮箱数组信息 - * @return + * + * @return 邮箱账号信息数组 */ List getMailAccountList(); - /** - * 发送邮件 - * @param mailReqVO - */ - void sendMail(MailReqVO mailReqVO); } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateService.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateService.java index 98fe87ef9..1511309eb 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateService.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateService.java @@ -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 getMailTemplatePage(MailTemplatePageReqVO pageReqVO); /** * 获取邮箱模板数组 - * @return + * + * @return 模版数组 */ List getMailTemplateList(); + + /** + * 发送邮件 + * + * @param mailReqVO 邮件发送信息 + */ + void sendMail(MailReqVO mailReqVO); } 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 4cb5ed0e2..fa2f8879e 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 @@ -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 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 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 params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content); - content = StrUtil.format(mailTemplateDO.getContent(), params); - - // 后续功能 TODO :附件查询 - //List 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); } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java index 7e4950f6d..c6b492498 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java @@ -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 diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java index 9fd80bdfe..7ce118303 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java @@ -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 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 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 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 params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content); + content = StrUtil.format(mailTemplateDO.getContent(), params); + + // 后续功能 TODO :附件查询 + //List 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(); - 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); } }