From b81e8e9e722feb1bd1f36feb8861421281aa1495 Mon Sep 17 00:00:00 2001 From: jay <75509151@qq.com> Date: Thu, 13 Jun 2024 19:08:42 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E7=A7=9F=E6=88=B7id=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=20String->Long?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/constant/TenantConstants.java | 2 +- .../java/cc/iotkit/common/model/LoginUser.java | 2 +- .../common/log/event/LogininforEvent.java | 2 +- .../iotkit/common/log/event/OperLogEvent.java | 2 +- .../common/satoken/utils/LoginHelper.java | 6 +++--- .../tenant/aspect/TenantFilterAspect.java | 9 +++++---- .../common/tenant/entiry/BaseTenantEntity.java | 4 ++-- .../common/tenant/helper/TenantHelper.java | 18 ++++++++++-------- .../common/tenant/listener/TenantListener.java | 9 +++++---- .../web/interceptor/TenantInterceptor.java | 2 +- 10 files changed, 30 insertions(+), 26 deletions(-) diff --git a/iot-common-core/src/main/java/cc/iotkit/common/constant/TenantConstants.java b/iot-common-core/src/main/java/cc/iotkit/common/constant/TenantConstants.java index d8d20c7..ad59b55 100755 --- a/iot-common-core/src/main/java/cc/iotkit/common/constant/TenantConstants.java +++ b/iot-common-core/src/main/java/cc/iotkit/common/constant/TenantConstants.java @@ -63,6 +63,6 @@ public interface TenantConstants { /** * 默认租户ID */ - String DEFAULT_TENANT_ID = "000000"; + Long DEFAULT_TENANT_ID = 0L; } diff --git a/iot-common-core/src/main/java/cc/iotkit/common/model/LoginUser.java b/iot-common-core/src/main/java/cc/iotkit/common/model/LoginUser.java index 216a3d2..18c2c7a 100755 --- a/iot-common-core/src/main/java/cc/iotkit/common/model/LoginUser.java +++ b/iot-common-core/src/main/java/cc/iotkit/common/model/LoginUser.java @@ -46,7 +46,7 @@ public class LoginUser implements Serializable { /** * 租户ID */ - private String tenantId; + private Long tenantId; /** * 用户ID diff --git a/iot-common-log/src/main/java/cc/iotkit/common/log/event/LogininforEvent.java b/iot-common-log/src/main/java/cc/iotkit/common/log/event/LogininforEvent.java index 0a04061..9ea2ea7 100755 --- a/iot-common-log/src/main/java/cc/iotkit/common/log/event/LogininforEvent.java +++ b/iot-common-log/src/main/java/cc/iotkit/common/log/event/LogininforEvent.java @@ -42,7 +42,7 @@ public class LogininforEvent implements Serializable { /** * 租户ID */ - private String tenantId; + private Long tenantId; /** * 用户账号 diff --git a/iot-common-log/src/main/java/cc/iotkit/common/log/event/OperLogEvent.java b/iot-common-log/src/main/java/cc/iotkit/common/log/event/OperLogEvent.java index 1a08399..00746e6 100755 --- a/iot-common-log/src/main/java/cc/iotkit/common/log/event/OperLogEvent.java +++ b/iot-common-log/src/main/java/cc/iotkit/common/log/event/OperLogEvent.java @@ -46,7 +46,7 @@ public class OperLogEvent implements Serializable { /** * 租户ID */ - private String tenantId; + private Long tenantId; /** * 操作模块 diff --git a/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java b/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java index 49d0547..2af69e6 100755 --- a/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java +++ b/iot-common-satoken/src/main/java/cc/iotkit/common/satoken/utils/LoginHelper.java @@ -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); } diff --git a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/aspect/TenantFilterAspect.java b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/aspect/TenantFilterAspect.java index b08c3a5..21c4d03 100755 --- a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/aspect/TenantFilterAspect.java +++ b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/aspect/TenantFilterAspect.java @@ -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); } diff --git a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/entiry/BaseTenantEntity.java b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/entiry/BaseTenantEntity.java index a99aa4e..4340aad 100755 --- a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/entiry/BaseTenantEntity.java +++ b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/entiry/BaseTenantEntity.java @@ -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; } diff --git a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java index d6024dd..25b4b9c 100755 --- a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java +++ b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/helper/TenantHelper.java @@ -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 TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); + private static final ThreadLocal TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>(); /** * 租户功能是否启用 @@ -62,7 +64,7 @@ public class TenantHelper { *

* 如果为非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 { *

* 如果为非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; diff --git a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/listener/TenantListener.java b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/listener/TenantListener.java index 3586ccd..adda6a7 100755 --- a/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/listener/TenantListener.java +++ b/iot-common-tenant/src/main/java/cc/iotkit/common/tenant/listener/TenantListener.java @@ -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); } } diff --git a/iot-common-web/src/main/java/cc/iotkit/common/web/interceptor/TenantInterceptor.java b/iot-common-web/src/main/java/cc/iotkit/common/web/interceptor/TenantInterceptor.java index 878b92b..8bbdc84 100755 --- a/iot-common-web/src/main/java/cc/iotkit/common/web/interceptor/TenantInterceptor.java +++ b/iot-common-web/src/main/java/cc/iotkit/common/web/interceptor/TenantInterceptor.java @@ -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;