update 租户id类型 String->Long

master
jay 2024-06-13 19:08:42 +08:00
parent 5307bb3d75
commit b81e8e9e72
10 changed files with 30 additions and 26 deletions

View File

@ -63,6 +63,6 @@ public interface TenantConstants {
/**
* ID
*/
String DEFAULT_TENANT_ID = "000000";
Long DEFAULT_TENANT_ID = 0L;
}

View File

@ -46,7 +46,7 @@ public class LoginUser implements Serializable {
/**
* ID
*/
private String tenantId;
private Long tenantId;
/**
* ID

View File

@ -42,7 +42,7 @@ public class LogininforEvent implements Serializable {
/**
* ID
*/
private String tenantId;
private Long tenantId;
/**
*

View File

@ -46,7 +46,7 @@ public class OperLogEvent implements Serializable {
/**
* ID
*/
private String tenantId;
private Long tenantId;
/**
*

View File

@ -134,7 +134,7 @@ public class LoginHelper {
/**
* ID
*/
public static String getTenantId() {
public static Long getTenantId() {
try {
LoginUser loginUser = getLoginUser();
if (loginUser == null) {
@ -142,7 +142,7 @@ public class LoginHelper {
if (tenantId == null) {
return null;
}
return tenantId.toString();
return (Long) tenantId;
}
return loginUser.getTenantId();
} catch (Exception e) {
@ -156,7 +156,7 @@ public class LoginHelper {
*
* @param tenantId ID
*/
public static void setTenantId(String tenantId) {
public static void setTenantId(Long tenantId) {
SaHolder.getStorage().set(TENANT_KEY, tenantId);
}

View File

@ -27,6 +27,7 @@ package cc.iotkit.common.tenant.aspect;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.tenant.helper.TenantHelper;
import cc.iotkit.common.utils.StringUtils;
import cn.hutool.core.util.ObjectUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@ -49,12 +50,12 @@ public class TenantFilterAspect {
@AfterReturning(pointcut = "openSession()", returning = "session")
public void afterOpenSession(Object session) {
if (session instanceof Session) {
String tenantId = LoginHelper.getTenantId();
String dynamic = TenantHelper.getDynamic();
if (StringUtils.isNotBlank(dynamic)) {
Long tenantId = LoginHelper.getTenantId();
Long dynamic = TenantHelper.getDynamic();
if (ObjectUtil.isNotNull(dynamic)) {
tenantId = dynamic;
}
if (tenantId != null && !tenantId.equals("000000")) {
if (tenantId != null) {
org.hibernate.Filter filter = ((Session) session).enableFilter("tenantFilter");
filter.setParameter("tenantId", tenantId);
}

View File

@ -63,7 +63,7 @@ public abstract class BaseTenantEntity implements TenantAware, Serializable {
private Long id;
@Column(name = "tenant_id")
private String tenantId;
private Long tenantId;
/**
*
@ -98,7 +98,7 @@ public abstract class BaseTenantEntity implements TenantAware, Serializable {
@Column(name = "update_time")
private Date updateTime;
public BaseTenantEntity(String tenantId) {
public BaseTenantEntity(Long tenantId) {
this.tenantId = tenantId;
}

View File

@ -36,6 +36,8 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
/**
*
*
@ -47,7 +49,7 @@ public class TenantHelper {
private static final String DYNAMIC_TENANT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicTenant";
private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>();
private static final ThreadLocal<Long> TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>();
/**
*
@ -62,7 +64,7 @@ public class TenantHelper {
* <p>
* web 线
*/
public static void setDynamic(String tenantId) {
public static void setDynamic(Long tenantId) {
if (!SpringMVCUtil.isWeb()) {
TEMP_DYNAMIC_TENANT.set(tenantId);
return;
@ -77,13 +79,13 @@ public class TenantHelper {
* <p>
* web 线
*/
public static String getDynamic() {
public static Long getDynamic() {
if (!SpringMVCUtil.isWeb()) {
return TEMP_DYNAMIC_TENANT.get();
}
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
String tenantId = (String) SaHolder.getStorage().get(cacheKey);
if (StringUtils.isNotBlank(tenantId)) {
Long tenantId = (Long) SaHolder.getStorage().get(cacheKey);
if (Objects.isNull(tenantId)) {
return tenantId;
}
tenantId = RedisUtils.getCacheObject(cacheKey);
@ -107,9 +109,9 @@ public class TenantHelper {
/**
* id()
*/
public static String getTenantId() {
String tenantId = TenantHelper.getDynamic();
if (StringUtils.isBlank(tenantId)) {
public static Long getTenantId() {
Long tenantId = Long.valueOf(TenantHelper.getDynamic());
if (Objects.isNull(tenantId)) {
tenantId = LoginHelper.getTenantId();
}
return tenantId;

View File

@ -33,6 +33,7 @@ import lombok.extern.slf4j.Slf4j;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import java.util.Objects;
/**
* ...
@ -48,12 +49,12 @@ public class TenantListener {
@PreRemove
@PrePersist
public void setTenant(TenantAware entity) {
String tenantId = LoginHelper.getTenantId();
String dynamic = TenantHelper.getDynamic();
if (StringUtils.isNotBlank(dynamic)) {
Long tenantId = LoginHelper.getTenantId();
Long dynamic = TenantHelper.getDynamic();
if (!Objects.isNull(dynamic)) {
tenantId = dynamic;
}
if (!"000000".equals(tenantId) && tenantId != null) {
if ( tenantId != null) {
entity.setTenantId(tenantId);
}
}

View File

@ -45,7 +45,7 @@ public class TenantInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
if (request.getHeader(TENANT_ID) != null) {
String tenantId = request.getHeader(TENANT_ID);
Long tenantId = Long.valueOf(request.getHeader(TENANT_ID));
SaHolder.getStorage().set("tenantId", tenantId);
}
return true;