Commit 22fb4044c92edcb29b3226b32809dcd142dceb95
Committed by
Gitee
!1152 租户套餐名重名校验
Merge pull request !1152 from 小新/master-jdk17
Showing
3 changed files
with
30 additions
and
0 deletions
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
| @@ -111,6 +111,7 @@ public interface ErrorCodeConstants { | @@ -111,6 +111,7 @@ public interface ErrorCodeConstants { | ||
| 111 | ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1_002_016_000, "租户套餐不存在"); | 111 | ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1_002_016_000, "租户套餐不存在"); |
| 112 | ErrorCode TENANT_PACKAGE_USED = new ErrorCode(1_002_016_001, "租户正在使用该套餐,请给租户重新设置套餐后再尝试删除"); | 112 | ErrorCode TENANT_PACKAGE_USED = new ErrorCode(1_002_016_001, "租户正在使用该套餐,请给租户重新设置套餐后再尝试删除"); |
| 113 | ErrorCode TENANT_PACKAGE_DISABLE = new ErrorCode(1_002_016_002, "名字为【{}】的租户套餐已被禁用"); | 113 | ErrorCode TENANT_PACKAGE_DISABLE = new ErrorCode(1_002_016_002, "名字为【{}】的租户套餐已被禁用"); |
| 114 | + ErrorCode TENANT_PACKAGE_NAME_DUPLICATE = new ErrorCode(1_002_016_003, "已经存在该名字的租户套餐"); | ||
| 114 | 115 | ||
| 115 | // ========== 社交用户 1-002-018-000 ========== | 116 | // ========== 社交用户 1-002-018-000 ========== |
| 116 | ErrorCode SOCIAL_USER_AUTH_FAILURE = new ErrorCode(1_002_018_000, "社交授权失败,原因是:{}"); | 117 | ErrorCode SOCIAL_USER_AUTH_FAILURE = new ErrorCode(1_002_018_000, "社交授权失败,原因是:{}"); |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenant/TenantPackageMapper.java
| @@ -29,4 +29,8 @@ public interface TenantPackageMapper extends BaseMapperX<TenantPackageDO> { | @@ -29,4 +29,8 @@ public interface TenantPackageMapper extends BaseMapperX<TenantPackageDO> { | ||
| 29 | default List<TenantPackageDO> selectListByStatus(Integer status) { | 29 | default List<TenantPackageDO> selectListByStatus(Integer status) { |
| 30 | return selectList(TenantPackageDO::getStatus, status); | 30 | return selectList(TenantPackageDO::getStatus, status); |
| 31 | } | 31 | } |
| 32 | + | ||
| 33 | + default TenantPackageDO selectByName(String name) { | ||
| 34 | + return selectOne(TenantPackageDO::getName, name); | ||
| 35 | + } | ||
| 32 | } | 36 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImpl.java
| 1 | package cn.iocoder.yudao.module.system.service.tenant; | 1 | package cn.iocoder.yudao.module.system.service.tenant; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| @@ -10,6 +11,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; | @@ -10,6 +11,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; | ||
| 10 | import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; |
| 11 | import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantPackageMapper; | 12 | import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantPackageMapper; |
| 12 | import com.baomidou.dynamic.datasource.annotation.DSTransactional; | 13 | import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
| 14 | +import com.google.common.annotations.VisibleForTesting; | ||
| 13 | import org.springframework.context.annotation.Lazy; | 15 | import org.springframework.context.annotation.Lazy; |
| 14 | import org.springframework.stereotype.Service; | 16 | import org.springframework.stereotype.Service; |
| 15 | import org.springframework.validation.annotation.Validated; | 17 | import org.springframework.validation.annotation.Validated; |
| @@ -38,6 +40,8 @@ public class TenantPackageServiceImpl implements TenantPackageService { | @@ -38,6 +40,8 @@ public class TenantPackageServiceImpl implements TenantPackageService { | ||
| 38 | 40 | ||
| 39 | @Override | 41 | @Override |
| 40 | public Long createTenantPackage(TenantPackageSaveReqVO createReqVO) { | 42 | public Long createTenantPackage(TenantPackageSaveReqVO createReqVO) { |
| 43 | + // 校验套餐名是否重复 | ||
| 44 | + validateTenantPackageNameUnique(null, createReqVO.getName()); | ||
| 41 | // 插入 | 45 | // 插入 |
| 42 | TenantPackageDO tenantPackage = BeanUtils.toBean(createReqVO, TenantPackageDO.class); | 46 | TenantPackageDO tenantPackage = BeanUtils.toBean(createReqVO, TenantPackageDO.class); |
| 43 | tenantPackageMapper.insert(tenantPackage); | 47 | tenantPackageMapper.insert(tenantPackage); |
| @@ -50,6 +54,8 @@ public class TenantPackageServiceImpl implements TenantPackageService { | @@ -50,6 +54,8 @@ public class TenantPackageServiceImpl implements TenantPackageService { | ||
| 50 | public void updateTenantPackage(TenantPackageSaveReqVO updateReqVO) { | 54 | public void updateTenantPackage(TenantPackageSaveReqVO updateReqVO) { |
| 51 | // 校验存在 | 55 | // 校验存在 |
| 52 | TenantPackageDO tenantPackage = validateTenantPackageExists(updateReqVO.getId()); | 56 | TenantPackageDO tenantPackage = validateTenantPackageExists(updateReqVO.getId()); |
| 57 | + // 校验套餐名是否重复 | ||
| 58 | + validateTenantPackageNameUnique(updateReqVO.getId(), updateReqVO.getName()); | ||
| 53 | // 更新 | 59 | // 更新 |
| 54 | TenantPackageDO updateObj = BeanUtils.toBean(updateReqVO, TenantPackageDO.class); | 60 | TenantPackageDO updateObj = BeanUtils.toBean(updateReqVO, TenantPackageDO.class); |
| 55 | tenantPackageMapper.updateById(updateObj); | 61 | tenantPackageMapper.updateById(updateObj); |
| @@ -111,4 +117,23 @@ public class TenantPackageServiceImpl implements TenantPackageService { | @@ -111,4 +117,23 @@ public class TenantPackageServiceImpl implements TenantPackageService { | ||
| 111 | return tenantPackageMapper.selectListByStatus(status); | 117 | return tenantPackageMapper.selectListByStatus(status); |
| 112 | } | 118 | } |
| 113 | 119 | ||
| 120 | + | ||
| 121 | + @VisibleForTesting | ||
| 122 | + void validateTenantPackageNameUnique(Long id, String name) { | ||
| 123 | + if (StrUtil.isBlank(name)) { | ||
| 124 | + return; | ||
| 125 | + } | ||
| 126 | + TenantPackageDO tenantPackage = tenantPackageMapper.selectByName(name); | ||
| 127 | + if (tenantPackage == null) { | ||
| 128 | + return; | ||
| 129 | + } | ||
| 130 | + // 如果 id 为空,说明不用比较是否为相同 id 的用户 | ||
| 131 | + if (id == null) { | ||
| 132 | + throw exception(TENANT_PACKAGE_NAME_DUPLICATE); | ||
| 133 | + } | ||
| 134 | + if (!tenantPackage.getId().equals(id)) { | ||
| 135 | + throw exception(TENANT_PACKAGE_NAME_DUPLICATE); | ||
| 136 | + } | ||
| 137 | + } | ||
| 138 | + | ||
| 114 | } | 139 | } |