From 1740128dc3ef779649d9215d3860c6b0d54a99ae Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 12 Apr 2021 01:07:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=9F=AD=E4=BF=A1=E6=B8=A0?= =?UTF-8?q?=E9=81=93=E6=97=B6=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=9C=89=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/vo/channel/SysSmsChannelBaseVO.java | 1 - .../dal/mysql/sms/SysSmsTemplateMapper.java | 4 ++++ .../system/enums/SysErrorCodeConstants.java | 1 + .../service/sms/SysSmsTemplateService.java | 8 +++++++ .../sms/impl/SysSmsChannelServiceImpl.java | 9 ++++++++ .../sms/impl/SysSmsTemplateServiceImpl.java | 7 +++++-- .../service/sms/SysSmsChannelServiceTest.java | 21 +++++++++++++++++++ 7 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/channel/SysSmsChannelBaseVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/channel/SysSmsChannelBaseVO.java index df760c862..9959b8af0 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/channel/SysSmsChannelBaseVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/channel/SysSmsChannelBaseVO.java @@ -35,5 +35,4 @@ public class SysSmsChannelBaseVO { @URL(message = "回调 URL 格式不正确") private String callbackUrl; - } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/sms/SysSmsTemplateMapper.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/sms/SysSmsTemplateMapper.java index cebfb9c17..2193b2416 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/sms/SysSmsTemplateMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/sms/SysSmsTemplateMapper.java @@ -41,4 +41,8 @@ public interface SysSmsTemplateMapper extends BaseMapperX { .orderByDesc("id")); } + default Integer selectCountByChannelId(Long channelId) { + return selectCount("channel_id", channelId); + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java b/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java index 77046bb96..a5a7dc08a 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java @@ -81,6 +81,7 @@ public interface SysErrorCodeConstants { // ========== 短信渠道 1002011000 ========== ErrorCode SMS_CHANNEL_NOT_EXISTS = new ErrorCode(1002011000, "短信渠道不存在"); ErrorCode SMS_CHANNEL_DISABLE = new ErrorCode(1002011001, "短信渠道不处于开启状态,不允许选择"); + ErrorCode SMS_CHANNEL_HAS_CHILDREN = new ErrorCode(1002011002, "无法删除,该短信渠道还有短信模板"); // ========== 短信模板 1002011000 ========== ErrorCode SMS_TEMPLATE_NOT_EXISTS = new ErrorCode(1002011000, "短信模板不存在"); diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsTemplateService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsTemplateService.java index 2c17b8a7a..4402f5f48 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsTemplateService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsTemplateService.java @@ -91,4 +91,12 @@ public interface SysSmsTemplateService { */ List getSmsTemplateList(SysSmsTemplateExportReqVO exportReqVO); + /** + * 获得指定短信渠道下的短信模板数量 + * + * @param channelId 短信渠道编号 + * @return 数量 + */ + Integer countByChannelId(Long channelId); + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsChannelServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsChannelServiceImpl.java index 859a0dea8..f5d63cc64 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsChannelServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsChannelServiceImpl.java @@ -11,6 +11,7 @@ import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsChannelConvert; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO; import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsChannelMapper; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService; +import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @@ -19,6 +20,7 @@ import java.util.Collection; import java.util.List; import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN; import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS; /** @@ -36,6 +38,9 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService { @Resource private SysSmsChannelMapper smsChannelMapper; + @Resource + private SysSmsTemplateService smsTemplateService; + @Override @PostConstruct public void initSmsClients() { @@ -70,6 +75,10 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService { public void deleteSmsChannel(Long id) { // 校验存在 this.validateSmsChannelExists(id); + // 校验是否有字典数据 + if (smsTemplateService.countByChannelId(id) > 0) { + throw exception(SMS_CHANNEL_HAS_CHILDREN); + } // 更新 smsChannelMapper.deleteById(id); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsTemplateServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsTemplateServiceImpl.java index 0d3b00b4b..7cd15e4ba 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsTemplateServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsTemplateServiceImpl.java @@ -21,7 +21,6 @@ import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; import com.google.common.annotations.VisibleForTesting; import org.springframework.stereotype.Service; import org.springframework.util.Assert; -import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.Collection; @@ -40,7 +39,6 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; * @date 2021/1/25 9:25 */ @Service -@Validated public class SysSmsTemplateServiceImpl implements SysSmsTemplateService { /** @@ -142,6 +140,11 @@ public class SysSmsTemplateServiceImpl implements SysSmsTemplateService { return smsTemplateMapper.selectList(exportReqVO); } + @Override + public Integer countByChannelId(Long channelId) { + return smsTemplateMapper.selectCountByChannelId(channelId); + } + @VisibleForTesting public SysSmsChannelDO checkSmsChannel(Long channelId) { SysSmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId); diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java index 630adf50a..4a598d3de 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java @@ -20,12 +20,15 @@ import javax.annotation.Resource; import java.util.function.Consumer; import static cn.hutool.core.util.RandomUtil.randomEle; +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN; import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS; import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; import static cn.iocoder.dashboard.util.RandomUtils.*; import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; /** * {@link SysSmsChannelServiceImpl} 的单元测试类 @@ -44,6 +47,9 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest { @Resource private SysSmsChannelMapper smsChannelMapper; + @MockBean + private SysSmsTemplateService smsTemplateService; + @Test public void testCreateSmsChannel_success() { // 准备参数 @@ -67,6 +73,7 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest { SysSmsChannelUpdateReqVO reqVO = randomPojo(SysSmsChannelUpdateReqVO.class, o -> { o.setId(dbSmsChannel.getId()); // 设置更新的 ID o.setStatus(randomCommonStatus()); + o.setCallbackUrl(randomString()); }); // 调用 @@ -108,6 +115,20 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest { assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_NOT_EXISTS); } + @Test + public void testDeleteSmsChannel_hasChildren() { + // mock 数据 + SysSmsChannelDO dbSmsChannel = randomSmsChannelDO(); + smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据 + // 准备参数 + Long id = dbSmsChannel.getId(); + // mock 方法 + when(smsTemplateService.countByChannelId(eq(id))).thenReturn(10); + + // 调用, 并断言异常 + assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_HAS_CHILDREN); + } + @Test public void testGetSmsChannelPage() { // mock 数据