Commit 78658347b1be27a2b09033d573492c05c62f544c
1 parent
7853ac9f
feat: 新增批量删除邮件模版接口
Showing
3 changed files
with
56 additions
and
10 deletions
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailTemplateController.java
| @@ -10,11 +10,11 @@ import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; | @@ -10,11 +10,11 @@ import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; | ||
| 10 | import io.swagger.v3.oas.annotations.Operation; | 10 | import io.swagger.v3.oas.annotations.Operation; |
| 11 | import io.swagger.v3.oas.annotations.Parameter; | 11 | import io.swagger.v3.oas.annotations.Parameter; |
| 12 | import io.swagger.v3.oas.annotations.tags.Tag; | 12 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 13 | +import jakarta.annotation.Resource; | ||
| 14 | +import jakarta.validation.Valid; | ||
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | 15 | import org.springframework.security.access.prepost.PreAuthorize; |
| 14 | import org.springframework.web.bind.annotation.*; | 16 | import org.springframework.web.bind.annotation.*; |
| 15 | 17 | ||
| 16 | -import jakarta.annotation.Resource; | ||
| 17 | -import jakarta.validation.Valid; | ||
| 18 | import java.util.List; | 18 | import java.util.List; |
| 19 | 19 | ||
| 20 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 20 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| @@ -54,6 +54,15 @@ public class MailTemplateController { | @@ -54,6 +54,15 @@ public class MailTemplateController { | ||
| 54 | return success(true); | 54 | return success(true); |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | + @DeleteMapping("/delete-list") | ||
| 58 | + @Operation(summary = "批量删除邮件模版") | ||
| 59 | + @Parameter(name = "ids", description = "编号列表", required = true) | ||
| 60 | + @PreAuthorize("@ss.hasPermission('system:mail-template:delete')") | ||
| 61 | + public CommonResult<Boolean> deleteMailTemplateList(@RequestParam("ids") List<Long> ids) { | ||
| 62 | + mailTempleService.deleteMailTemplateList(ids); | ||
| 63 | + return success(true); | ||
| 64 | + } | ||
| 65 | + | ||
| 57 | @GetMapping("/get") | 66 | @GetMapping("/get") |
| 58 | @Operation(summary = "获得邮件模版") | 67 | @Operation(summary = "获得邮件模版") |
| 59 | @Parameter(name = "id", description = "编号", required = true, example = "1024") | 68 | @Parameter(name = "id", description = "编号", required = true, example = "1024") |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateService.java
| @@ -4,8 +4,8 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -4,8 +4,8 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; | 4 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; |
| 5 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateSaveReqVO; | 5 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateSaveReqVO; |
| 6 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | 6 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 7 | - | ||
| 8 | import jakarta.validation.Valid; | 7 | import jakarta.validation.Valid; |
| 8 | + | ||
| 9 | import java.util.List; | 9 | import java.util.List; |
| 10 | import java.util.Map; | 10 | import java.util.Map; |
| 11 | 11 | ||
| @@ -40,6 +40,13 @@ public interface MailTemplateService { | @@ -40,6 +40,13 @@ public interface MailTemplateService { | ||
| 40 | void deleteMailTemplate(Long id); | 40 | void deleteMailTemplate(Long id); |
| 41 | 41 | ||
| 42 | /** | 42 | /** |
| 43 | + * 批量删除邮件模版 | ||
| 44 | + * | ||
| 45 | + * @param ids 编号列表 | ||
| 46 | + */ | ||
| 47 | + void deleteMailTemplateList(List<Long> ids); | ||
| 48 | + | ||
| 49 | + /** | ||
| 43 | * 获取邮件模版 | 50 | * 获取邮件模版 |
| 44 | * | 51 | * |
| 45 | * @param id 编号 | 52 | * @param id 编号 |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateServiceImpl.java
| 1 | package cn.iocoder.yudao.module.system.service.mail; | 1 | package cn.iocoder.yudao.module.system.service.mail; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 3 | import cn.hutool.core.util.ObjUtil; | 4 | import cn.hutool.core.util.ObjUtil; |
| 4 | import cn.hutool.core.util.ReUtil; | 5 | import cn.hutool.core.util.ReUtil; |
| 5 | import cn.hutool.core.util.StrUtil; | 6 | import cn.hutool.core.util.StrUtil; |
| @@ -11,14 +12,14 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | @@ -11,14 +12,14 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | ||
| 11 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; | 12 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; |
| 12 | import cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants; | 13 | import cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants; |
| 13 | import com.google.common.annotations.VisibleForTesting; | 14 | import com.google.common.annotations.VisibleForTesting; |
| 15 | +import jakarta.annotation.Resource; | ||
| 16 | +import jakarta.validation.Valid; | ||
| 14 | import lombok.extern.slf4j.Slf4j; | 17 | import lombok.extern.slf4j.Slf4j; |
| 15 | import org.springframework.cache.annotation.CacheEvict; | 18 | import org.springframework.cache.annotation.CacheEvict; |
| 16 | import org.springframework.cache.annotation.Cacheable; | 19 | import org.springframework.cache.annotation.Cacheable; |
| 17 | import org.springframework.stereotype.Service; | 20 | import org.springframework.stereotype.Service; |
| 18 | import org.springframework.validation.annotation.Validated; | 21 | import org.springframework.validation.annotation.Validated; |
| 19 | 22 | ||
| 20 | -import jakarta.annotation.Resource; | ||
| 21 | -import jakarta.validation.Valid; | ||
| 22 | import java.util.List; | 23 | import java.util.List; |
| 23 | import java.util.Map; | 24 | import java.util.Map; |
| 24 | import java.util.regex.Pattern; | 25 | import java.util.regex.Pattern; |
| @@ -97,12 +98,36 @@ public class MailTemplateServiceImpl implements MailTemplateService { | @@ -97,12 +98,36 @@ public class MailTemplateServiceImpl implements MailTemplateService { | ||
| 97 | mailTemplateMapper.deleteById(id); | 98 | mailTemplateMapper.deleteById(id); |
| 98 | } | 99 | } |
| 99 | 100 | ||
| 101 | + @Override | ||
| 102 | + @CacheEvict(cacheNames = RedisKeyConstants.MAIL_TEMPLATE, | ||
| 103 | + allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 code,不好清理 | ||
| 104 | + public void deleteMailTemplateList(List<Long> ids) { | ||
| 105 | + if (CollUtil.isEmpty(ids)) { | ||
| 106 | + return; | ||
| 107 | + } | ||
| 108 | + // 校验存在 | ||
| 109 | + validateMailTemplatesExists(ids); | ||
| 110 | + // 批量删除 | ||
| 111 | + mailTemplateMapper.deleteByIds(ids); | ||
| 112 | + } | ||
| 113 | + | ||
| 100 | private void validateMailTemplateExists(Long id) { | 114 | private void validateMailTemplateExists(Long id) { |
| 101 | if (mailTemplateMapper.selectById(id) == null) { | 115 | if (mailTemplateMapper.selectById(id) == null) { |
| 102 | throw exception(MAIL_TEMPLATE_NOT_EXISTS); | 116 | throw exception(MAIL_TEMPLATE_NOT_EXISTS); |
| 103 | } | 117 | } |
| 104 | } | 118 | } |
| 105 | 119 | ||
| 120 | + private void validateMailTemplatesExists(List<Long> ids) { | ||
| 121 | + if (CollUtil.isEmpty(ids)) { | ||
| 122 | + return; | ||
| 123 | + } | ||
| 124 | + // 校验存在 | ||
| 125 | + List<MailTemplateDO> templates = mailTemplateMapper.selectByIds(ids); | ||
| 126 | + if (CollUtil.isEmpty(templates) || templates.size() != ids.size()) { | ||
| 127 | + throw exception(MAIL_TEMPLATE_NOT_EXISTS); | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | + | ||
| 106 | @Override | 131 | @Override |
| 107 | public MailTemplateDO getMailTemplate(Long id) {return mailTemplateMapper.selectById(id);} | 132 | public MailTemplateDO getMailTemplate(Long id) {return mailTemplateMapper.selectById(id);} |
| 108 | 133 | ||
| @@ -125,14 +150,19 @@ public class MailTemplateServiceImpl implements MailTemplateService { | @@ -125,14 +150,19 @@ public class MailTemplateServiceImpl implements MailTemplateService { | ||
| 125 | return StrUtil.format(content, params); | 150 | return StrUtil.format(content, params); |
| 126 | } | 151 | } |
| 127 | 152 | ||
| 128 | - @VisibleForTesting | ||
| 129 | - public List<String> parseTemplateContentParams(String content) { | ||
| 130 | - return ReUtil.findAllGroup1(PATTERN_PARAMS, content); | ||
| 131 | - } | ||
| 132 | - | ||
| 133 | @Override | 153 | @Override |
| 134 | public long getMailTemplateCountByAccountId(Long accountId) { | 154 | public long getMailTemplateCountByAccountId(Long accountId) { |
| 135 | return mailTemplateMapper.selectCountByAccountId(accountId); | 155 | return mailTemplateMapper.selectCountByAccountId(accountId); |
| 136 | } | 156 | } |
| 137 | 157 | ||
| 158 | + /** | ||
| 159 | + * 获得邮件模板中的参数,形如 {key} | ||
| 160 | + * | ||
| 161 | + * @param content 内容 | ||
| 162 | + * @return 参数列表 | ||
| 163 | + */ | ||
| 164 | + private List<String> parseTemplateContentParams(String content) { | ||
| 165 | + return ReUtil.findAllGroup1(PATTERN_PARAMS, content); | ||
| 166 | + } | ||
| 167 | + | ||
| 138 | } | 168 | } |