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 * ID
*/ */
String DEFAULT_TENANT_ID = "000000"; Long DEFAULT_TENANT_ID = 0L;
} }

View File

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

View File

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

View File

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

View File

@ -134,7 +134,7 @@ public class LoginHelper {
/** /**
* ID * ID
*/ */
public static String getTenantId() { public static Long getTenantId() {
try { try {
LoginUser loginUser = getLoginUser(); LoginUser loginUser = getLoginUser();
if (loginUser == null) { if (loginUser == null) {
@ -142,7 +142,7 @@ public class LoginHelper {
if (tenantId == null) { if (tenantId == null) {
return null; return null;
} }
return tenantId.toString(); return (Long) tenantId;
} }
return loginUser.getTenantId(); return loginUser.getTenantId();
} catch (Exception e) { } catch (Exception e) {
@ -156,7 +156,7 @@ public class LoginHelper {
* *
* @param tenantId ID * @param tenantId ID
*/ */
public static void setTenantId(String tenantId) { public static void setTenantId(Long tenantId) {
SaHolder.getStorage().set(TENANT_KEY, 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.satoken.utils.LoginHelper;
import cc.iotkit.common.tenant.helper.TenantHelper; import cc.iotkit.common.tenant.helper.TenantHelper;
import cc.iotkit.common.utils.StringUtils; import cc.iotkit.common.utils.StringUtils;
import cn.hutool.core.util.ObjectUtil;
import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
@ -49,12 +50,12 @@ public class TenantFilterAspect {
@AfterReturning(pointcut = "openSession()", returning = "session") @AfterReturning(pointcut = "openSession()", returning = "session")
public void afterOpenSession(Object session) { public void afterOpenSession(Object session) {
if (session instanceof Session) { if (session instanceof Session) {
String tenantId = LoginHelper.getTenantId(); Long tenantId = LoginHelper.getTenantId();
String dynamic = TenantHelper.getDynamic(); Long dynamic = TenantHelper.getDynamic();
if (StringUtils.isNotBlank(dynamic)) { if (ObjectUtil.isNotNull(dynamic)) {
tenantId = dynamic; tenantId = dynamic;
} }
if (tenantId != null && !tenantId.equals("000000")) { if (tenantId != null) {
org.hibernate.Filter filter = ((Session) session).enableFilter("tenantFilter"); org.hibernate.Filter filter = ((Session) session).enableFilter("tenantFilter");
filter.setParameter("tenantId", tenantId); filter.setParameter("tenantId", tenantId);
} }

View File

@ -63,7 +63,7 @@ public abstract class BaseTenantEntity implements TenantAware, Serializable {
private Long id; private Long id;
@Column(name = "tenant_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") @Column(name = "update_time")
private Date updateTime; private Date updateTime;
public BaseTenantEntity(String tenantId) { public BaseTenantEntity(Long tenantId) {
this.tenantId = tenantId; this.tenantId = tenantId;
} }

View File

@ -36,6 +36,8 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; 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 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> * <p>
* web 线 * web 线
*/ */
public static void setDynamic(String tenantId) { public static void setDynamic(Long tenantId) {
if (!SpringMVCUtil.isWeb()) { if (!SpringMVCUtil.isWeb()) {
TEMP_DYNAMIC_TENANT.set(tenantId); TEMP_DYNAMIC_TENANT.set(tenantId);
return; return;
@ -77,13 +79,13 @@ public class TenantHelper {
* <p> * <p>
* web 线 * web 线
*/ */
public static String getDynamic() { public static Long getDynamic() {
if (!SpringMVCUtil.isWeb()) { if (!SpringMVCUtil.isWeb()) {
return TEMP_DYNAMIC_TENANT.get(); return TEMP_DYNAMIC_TENANT.get();
} }
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId(); String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
String tenantId = (String) SaHolder.getStorage().get(cacheKey); Long tenantId = (Long) SaHolder.getStorage().get(cacheKey);
if (StringUtils.isNotBlank(tenantId)) { if (Objects.isNull(tenantId)) {
return tenantId; return tenantId;
} }
tenantId = RedisUtils.getCacheObject(cacheKey); tenantId = RedisUtils.getCacheObject(cacheKey);
@ -107,9 +109,9 @@ public class TenantHelper {
/** /**
* id() * id()
*/ */
public static String getTenantId() { public static Long getTenantId() {
String tenantId = TenantHelper.getDynamic(); Long tenantId = Long.valueOf(TenantHelper.getDynamic());
if (StringUtils.isBlank(tenantId)) { if (Objects.isNull(tenantId)) {
tenantId = LoginHelper.getTenantId(); tenantId = LoginHelper.getTenantId();
} }
return tenantId; return tenantId;

View File

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

View File

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