【8.3.4】【mp】调整租户的相关接口

master
stylefeng 2025-05-05 21:30:04 +08:00
parent 3956efe691
commit 979cc77555
9 changed files with 271 additions and 1 deletions

View File

@ -107,4 +107,9 @@ public interface RuleConstants {
*/
String TENANT_DB_PREFIX = "sys_tenant_db_";
/**
* ID
*/
Long DEFAULT_ROOT_TENANT_ID = 1L;
}

View File

@ -0,0 +1,29 @@
package cn.stylefeng.roses.kernel.db.mp.tenant;
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
/**
* API
*
* @author fengshuonan
* @since 2025/5/4 18:35
*/
public interface SaasServiceApi {
/**
* id
*
* @author fengshuonan
* @since 2025/5/4 18:35
*/
TenantSwitchInfo getTenantSwitchInfo(Long tenantId);
/**
*
*
* @author fengshuonan
* @since 2025/5/5 11:31
*/
TenantSwitchInfo getTenantSwitchInfo(String tenantCode);
}

View File

@ -0,0 +1,47 @@
package cn.stylefeng.roses.kernel.db.mp.tenant;
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
import java.util.function.Supplier;
/**
* API
* <p>
* id
*
* @author fengshuonan
* @since 2025/5/5 10:59
*/
public interface TenantSwitchApi {
/**
*
*
* @param tenantCode
* @param action
* @author fengshuonan
* @since 2025/5/4 17:23
*/
<T> T changeTenant(String tenantCode, Supplier<T> action);
/**
* id
*
* @param tenantId id
* @param action
* @author fengshuonan
* @since 2025/5/4 17:23
*/
<T> T changeTenant(Long tenantId, Supplier<T> action);
/**
*
*
* @param tenantSwitchInfo
* @param action
* @author fengshuonan
* @since 2025/5/4 17:23
*/
<T> T changeTenant(TenantSwitchInfo tenantSwitchInfo, Supplier<T> action);
}

View File

@ -0,0 +1,21 @@
package cn.stylefeng.roses.kernel.db.mp.tenant.context;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.db.mp.tenant.TenantSwitchApi;
/**
*
*
* @author fengshuonan
* @since 2025/5/5 11:21
*/
public class TenantSwitchContext {
public TenantSwitchContext() {
}
public static TenantSwitchApi me() {
return SpringUtil.getBean(TenantSwitchApi.class);
}
}

View File

@ -0,0 +1,25 @@
package cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl;
import cn.stylefeng.roses.kernel.db.mp.tenant.SaasServiceApi;
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
/**
*
*
* @author fengshuonan
* @since 2025/5/5 21:06
*/
public class DefaultSaasServiceImpl implements SaasServiceApi {
@Override
public TenantSwitchInfo getTenantSwitchInfo(Long tenantId) {
return new TenantSwitchInfo(RuleConstants.DEFAULT_ROOT_TENANT_ID, null, null);
}
@Override
public TenantSwitchInfo getTenantSwitchInfo(String tenantCode) {
return new TenantSwitchInfo(RuleConstants.DEFAULT_ROOT_TENANT_ID, tenantCode, null);
}
}

View File

@ -0,0 +1,31 @@
package cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl;
import cn.stylefeng.roses.kernel.db.mp.tenant.TenantSwitchApi;
import cn.stylefeng.roses.kernel.db.mp.tenant.pojo.TenantSwitchInfo;
import java.util.function.Supplier;
/**
*
*
* @author fengshuonan
* @since 2025/5/5 21:06
*/
public class DefaultTenantSwitchImpl implements TenantSwitchApi {
@Override
public <T> T changeTenant(String tenantCode, Supplier<T> action) {
return action.get();
}
@Override
public <T> T changeTenant(Long tenantId, Supplier<T> action) {
return action.get();
}
@Override
public <T> T changeTenant(TenantSwitchInfo tenantSwitchInfo, Supplier<T> action) {
return action.get();
}
}

View File

@ -0,0 +1,42 @@
package cn.stylefeng.roses.kernel.db.mp.tenant.pojo;
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
import lombok.Data;
/**
*
*
* @author fengshuonan
* @since 2025/5/4 20:38
*/
@Data
public class TenantSwitchInfo {
/**
* id
*/
@ChineseDescription("租户id")
private Long tenantId;
/**
*
*/
@ChineseDescription("租户唯一标识")
private String tenantCode;
/**
* 1-id2-
*/
@ChineseDescription("数据隔离方式1-租户id隔离2-数据库分离")
private Integer dataMode;
public TenantSwitchInfo() {
}
public TenantSwitchInfo(Long tenantId, String tenantCode, Integer dataMode) {
this.tenantId = tenantId;
this.tenantCode = tenantCode;
this.dataMode = dataMode;
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.db.starter;
import cn.stylefeng.roses.kernel.db.mp.tenant.SaasServiceApi;
import cn.stylefeng.roses.kernel.db.mp.tenant.TenantSwitchApi;
import cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl.DefaultSaasServiceImpl;
import cn.stylefeng.roses.kernel.db.mp.tenant.defaultimpl.DefaultTenantSwitchImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author fengshuonan
* @since 2025/5/5 21:03
*/
@Configuration
public class ProjectTenantAutoConfiguration {
/**
*
*
* @author fengshuonan
* @since 2025/5/5 21:07
*/
@Bean
@ConditionalOnMissingBean
public SaasServiceApi saasServiceApi() {
return new DefaultSaasServiceImpl();
}
/**
*
*
* @author fengshuonan
* @since 2025/5/5 21:08
*/
@Bean
@ConditionalOnMissingBean
public TenantSwitchApi tenantSwitchApi() {
return new DefaultTenantSwitchImpl();
}
}

View File

@ -24,6 +24,8 @@
*/
package cn.stylefeng.roses.kernel.sys.api.constants;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
/**
*
*
@ -75,6 +77,6 @@ public interface SysConstants {
/**
* ID
*/
Long DEFAULT_ROOT_TENANT_ID = 1L;
Long DEFAULT_ROOT_TENANT_ID = RuleConstants.DEFAULT_ROOT_TENANT_ID;
}