refactor(tenant): 优化租户相关代码

This commit is contained in:
2025-07-15 20:09:54 +08:00
parent ed6dd65a51
commit af1079da6d
111 changed files with 2741 additions and 2319 deletions

View File

@@ -89,9 +89,7 @@ public class LogDaoLocalImpl implements LogDao {
// 设置操作人
this.setCreateUser(logDO, logRequest, logResponse);
Long tenantId = TenantContextHolder.getTenantId();
SpringUtil.getBean(TenantHandler.class).execute(tenantId, () -> {
logMapper.insert(logDO);
});
SpringUtil.getBean(TenantHandler.class).execute(tenantId, () -> logMapper.insert(logDO));
}
/**

View File

@@ -17,43 +17,64 @@
package top.continew.admin.config.tenant;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import top.continew.admin.common.constant.SysConstants;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.admin.tenant.annotation.ConditionalOnEnabledTenant;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
/**
* @description: 主数据源切面
* @author: 小熊
* @create: 2025-01-15 16:02
* 租户主数据源切面
*
* @author 小熊
* @author Charles7c
* @since 2025/1/15 16:02
*/
@Aspect
@Component
@ConditionalOnProperty(prefix = PropertiesConstants.TENANT, name = PropertiesConstants.ENABLED, havingValue = "true")
public class DataSourceSwitchAspect {
@ConditionalOnEnabledTenant
@RequiredArgsConstructor
public class TenantDataSourceSwitchAspect {
@Pointcut("execution(* top.continew.admin.tenant.mapper..*(..)) || " + "execution(* top.continew.admin.tenant.service..*(..)) || " + "execution(* top.continew.admin.system.mapper.ClientMapper.*(..)) || " + "execution(* top.continew.admin.system.service.ClientService.*(..)) || " + "execution(* top.continew.admin.system.mapper.DictMapper.*(..)) || " + "execution(* top.continew.admin.system.service.DictService.*(..)) || " + "execution(* top.continew.admin.system.mapper.DictItemMapper.*(..)) || " + "execution(* top.continew.admin.system.service.DictItemService.*(..)) || " + "execution(* top.continew.admin.system.mapper.OptionMapper.*(..)) || " + "execution(* top.continew.admin.system.service.OptionService.*(..)) || " + "execution(* top.continew.admin.system.mapper.StorageMapper.*(..)) || " + "execution(* top.continew.admin.system.service.StorageService.*(..))")
public void MasterDataSourceMethods() {
@Pointcut("""
execution(* top.continew.admin.tenant.mapper..*(..))
|| execution(* top.continew.admin.tenant.service..*(..))
|| execution(* top.continew.admin.system.mapper.ClientMapper.*(..))
|| execution(* top.continew.admin.system.service.ClientService.*(..))
|| execution(* top.continew.admin.system.mapper.DictMapper.*(..))
|| execution(* top.continew.admin.system.service.DictService.*(..))
|| execution(* top.continew.admin.system.mapper.DictItemMapper.*(..))
|| execution(* top.continew.admin.system.service.DictItemService.*(..))
|| execution(* top.continew.admin.system.mapper.OptionMapper.*(..))
|| execution(* top.continew.admin.system.service.OptionService.*(..))
|| execution(* top.continew.admin.system.mapper.StorageMapper.*(..))
|| execution(* top.continew.admin.system.service.StorageService.*(..))
""")
public void masterDataSourceMethods() {
}
@Before("MasterDataSourceMethods()")
/**
* 切换到主数据源
*/
@Before("masterDataSourceMethods()")
public void switchToMasterDataSource() {
if (TenantContextHolder.getIsolationLevel() == TenantIsolationLevel.DATASOURCE) {
DynamicDataSourceContextHolder.push(SysConstants.DEFAULT_DATASOURCE);
DynamicDataSourceContextHolder.push(SysConstants.DEFAULT_TENANT_DATASOURCE);
}
}
@After("MasterDataSourceMethods()")
/**
* 清空数据源
*/
@After("masterDataSourceMethods()")
public void clearDataSourceContext() {
if (TenantContextHolder.getIsolationLevel() == TenantIsolationLevel.DATASOURCE) {
DynamicDataSourceContextHolder.poll();
}
}
}

View File

@@ -44,7 +44,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.auth.model.resp.CaptchaResp;
import top.continew.admin.common.config.properties.CaptchaProperties;
import top.continew.admin.common.config.CaptchaProperties;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.constant.SysConstants;
import top.continew.admin.system.enums.OptionCategoryEnum;

View File

@@ -35,6 +35,10 @@ import top.continew.admin.system.enums.OptionCategoryEnum;
import top.continew.admin.system.model.query.*;
import top.continew.admin.system.model.resp.file.FileUploadResp;
import top.continew.admin.system.service.*;
import top.continew.admin.tenant.model.query.DatasourceQuery;
import top.continew.admin.tenant.model.query.PackageQuery;
import top.continew.admin.tenant.service.DatasourceService;
import top.continew.admin.tenant.service.PackageService;
import top.continew.starter.core.util.validation.ValidationUtils;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
@@ -62,6 +66,8 @@ public class CommonController {
private final MenuService menuService;
private final UserService userService;
private final RoleService roleService;
private final PackageService packageService;
private final DatasourceService datasourceService;
private final DictItemService dictItemService;
private final OptionService optionService;
@@ -104,6 +110,18 @@ public class CommonController {
return roleService.listDict(query, sortQuery);
}
@Operation(summary = "查询套餐字典", description = "查询套餐字典列表")
@GetMapping("/dict/package")
public List<LabelValueResp> listPackageDict(PackageQuery query, SortQuery sortQuery) {
return packageService.listDict(query, sortQuery);
}
@Operation(summary = "查询数据源字典", description = "查询数据源字典列表")
@GetMapping("/dict/datasource")
public List<LabelValueResp> listDatasourceDict(DatasourceQuery query, SortQuery sortQuery) {
return datasourceService.listDict(query, sortQuery);
}
@Operation(summary = "查询字典", description = "查询字典列表")
@Parameter(name = "code", description = "字典编码", example = "notice_type", in = ParameterIn.PATH)
@GetMapping("/dict/{code}")