Commit 55358aa170c2f641cdb63c12a04c8939b8d232bb

Authored by shizhong
1 parent ea4ca187

租户添加或修改时租户名称改为不可以重复,否则会造成重复名称的租户无法正常登录的问题

yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
... ... @@ -103,6 +103,7 @@ public interface ErrorCodeConstants {
103 103 ErrorCode TENANT_DISABLE = new ErrorCode(1002015001, "名字为【{}】的租户已被禁用");
104 104 ErrorCode TENANT_EXPIRE = new ErrorCode(1002015002, "名字为【{}】的租户已过期");
105 105 ErrorCode TENANT_CAN_NOT_UPDATE_SYSTEM = new ErrorCode(1002015003, "系统租户不能进行修改、删除等操作!");
  106 + ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1002015004, "已经存在该名称的租户");
106 107  
107 108 // ========== 租户套餐 1002016000 ==========
108 109 ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1002016000, "租户套餐不存在");
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java
... ... @@ -29,6 +29,7 @@ import cn.iocoder.yudao.module.system.service.permission.RoleService;
29 29 import cn.iocoder.yudao.module.system.service.tenant.handler.TenantInfoHandler;
30 30 import cn.iocoder.yudao.module.system.service.tenant.handler.TenantMenuHandler;
31 31 import cn.iocoder.yudao.module.system.service.user.AdminUserService;
  32 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
32 33 import lombok.extern.slf4j.Slf4j;
33 34 import org.springframework.beans.factory.annotation.Autowired;
34 35 import org.springframework.context.annotation.Lazy;
... ... @@ -97,6 +98,9 @@ public class TenantServiceImpl implements TenantService {
97 98 @Override
98 99 @Transactional(rollbackFor = Exception.class)
99 100 public Long createTenant(TenantCreateReqVO createReqVO) {
  101 + // 校验租户名称是否重复
  102 + validTenantName(createReqVO.getName(), null);
  103 +
100 104 // 校验套餐被禁用
101 105 TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId());
102 106  
... ... @@ -139,6 +143,10 @@ public class TenantServiceImpl implements TenantService {
139 143 public void updateTenant(TenantUpdateReqVO updateReqVO) {
140 144 // 校验存在
141 145 TenantDO tenant = validateUpdateTenant(updateReqVO.getId());
  146 +
  147 + // 校验租户名称是否重复
  148 + validTenantName(updateReqVO.getName(), updateReqVO.getId());
  149 +
142 150 // 校验套餐被禁用
143 151 TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId());
144 152  
... ... @@ -151,6 +159,17 @@ public class TenantServiceImpl implements TenantService {
151 159 }
152 160 }
153 161  
  162 + protected void validTenantName(String tenantName, Long id) {
  163 + LambdaQueryWrapper<TenantDO> wrapper = new LambdaQueryWrapper<>();
  164 + wrapper.eq(TenantDO::getName, tenantName);
  165 + if (id != null) {
  166 + wrapper.ne(TenantDO::getId, id);
  167 + }
  168 + if (tenantMapper.selectCount(wrapper) > 0) {
  169 + throw exception(TENANT_NAME_DUPLICATE);
  170 + }
  171 + }
  172 +
154 173 @Override
155 174 @Transactional(rollbackFor = Exception.class)
156 175 public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {
... ...