Explorar o código

fix:
租户名称不能重复

18339543638 %!s(int64=2) %!d(string=hai) anos
pai
achega
47054930a1

+ 20 - 0
tr-modules/tr-module-system/src/main/java/cn/tr/module/sys/tenant/service/impl/SysTenantServiceImpl.java

@@ -2,6 +2,7 @@ package cn.tr.module.sys.tenant.service.impl;
 
 
 
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.tr.core.annotation.TenantIgnore;
 import cn.tr.core.annotation.TenantIgnore;
 import cn.tr.core.exception.ServiceException;
 import cn.tr.core.exception.ServiceException;
@@ -21,6 +22,7 @@ import cn.tr.module.sys.user.service.*;
 import cn.tr.module.sys.tenant.service.ISysTenantService;
 import cn.tr.module.sys.tenant.service.ISysTenantService;
 import cn.tr.plugin.biz.tenant.context.TenantContextHolder;
 import cn.tr.plugin.biz.tenant.context.TenantContextHolder;
 import cn.tr.plugin.biz.tenant.utils.TenantUtils;
 import cn.tr.plugin.biz.tenant.utils.TenantUtils;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.context.annotation.Lazy;
@@ -82,12 +84,14 @@ public class SysTenantServiceImpl implements ISysTenantService {
 
 
     @Override
     @Override
     public boolean updateSysTenantById(SysTenantCommonDTO source) {
     public boolean updateSysTenantById(SysTenantCommonDTO source) {
+        validateSource(source);
         return tenantRepository.updateById(SysTenantMapper.INSTANCE.toPO(source))!=0;
         return tenantRepository.updateById(SysTenantMapper.INSTANCE.toPO(source))!=0;
     }
     }
 
 
     @Override
     @Override
     @Transactional(rollbackFor = Exception.class)
     @Transactional(rollbackFor = Exception.class)
     public boolean insertSysTenant(SysTenantAddDTO source) {
     public boolean insertSysTenant(SysTenantAddDTO source) {
+        validateSource(source);
         String orgId= IdWorker.getIdStr();
         String orgId= IdWorker.getIdStr();
         String userId= IdWorker.getIdStr();
         String userId= IdWorker.getIdStr();
         String roleId= IdWorker.getIdStr();
         String roleId= IdWorker.getIdStr();
@@ -149,6 +153,22 @@ public class SysTenantServiceImpl implements ISysTenantService {
         return  tenantPackageMenuService.findMenuIdByPackageId(tenant.getPackageId());
         return  tenantPackageMenuService.findMenuIdByPackageId(tenant.getPackageId());
     }
     }
 
 
+    private void validateSource(SysTenantAddDTO source){
+        SysTenantPO sysTenantPO = tenantRepository.selectOne(new LambdaQueryWrapper<SysTenantPO>()
+                .eq(SysTenantPO::getName, source.getName()));
+        if(ObjectUtil.isNotNull(sysTenantPO)){
+            throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"租户名称不能重复");
+        }
+    }
+
+    private void validateSource(SysTenantCommonDTO source){
+        SysTenantPO sysTenantPO = tenantRepository.selectOne(new LambdaQueryWrapper<SysTenantPO>()
+                .ne(StrUtil.isNotEmpty(source.getId()),SysTenantPO::getId,source.getId())
+                .eq(SysTenantPO::getName, source.getName()));
+        if(ObjectUtil.isNotNull(sysTenantPO)){
+            throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"租户名称不能重复");
+        }
+    }
     private SysPortalDTO buildPortal(String portalId){
     private SysPortalDTO buildPortal(String portalId){
         SysPortalDTO org = new SysPortalDTO();
         SysPortalDTO org = new SysPortalDTO();
         org.setId(portalId);
         org.setId(portalId);