package com.core.framework.handler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.core.common.core.domain.model.LoginUser; import com.core.common.utils.SecurityUtils; import com.core.framework.config.TenantContext; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * MyBatis-Plus 自动填充处理器 * 用于自动填充创建时间和更新时间,以及创建人和更新人 */ @Component public class MybastisColumnsHandler implements MetaObjectHandler { // 设置数据新增时的字段自动赋值规则 @Override public void insertFill(MetaObject metaObject) { // 填充创建时间 Date currentTime = new Date(); this.strictInsertFill(metaObject, "createTime", Date.class, currentTime); this.strictInsertFill(metaObject, "create_time", Date.class, currentTime); // 获取当前登录用户名 String username = getCurrentUsername(); // 填充创建人 this.strictInsertFill(metaObject, "createBy", String.class, username); this.strictInsertFill(metaObject, "create_by", String.class, username); // 确保tenantId被设置 Integer tenantId = getCurrentTenantId(); if (tenantId == null) { throw new RuntimeException("无法获取当前租户ID,请确保用户已登录或正确设置租户上下文"); } this.strictInsertFill(metaObject, "tenantId", Integer.class, tenantId); this.strictInsertFill(metaObject, "tenant_id", Integer.class, tenantId); } // 设置数据修改时的字段自动赋值规则 @Override public void updateFill(MetaObject metaObject) { // 填充更新时间 Date currentTime = new Date(); this.strictUpdateFill(metaObject, "updateTime", Date.class, currentTime); this.strictUpdateFill(metaObject, "update_time", Date.class, currentTime); // 填充更新人 String username = getCurrentUsername(); this.strictUpdateFill(metaObject, "updateBy", String.class, username); this.strictUpdateFill(metaObject, "update_by", String.class, username); } /** * 获取当前登录用户名 * @return 当前登录用户名,如果无法获取则返回 "system" */ private String getCurrentUsername() { String username = "system"; // 默认值 try { LoginUser loginUser = SecurityUtils.getLoginUser(); if (loginUser != null) { username = loginUser.getUsername(); } else { // 尝试从请求中获取用户信息 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes != null) { HttpServletRequest request = attributes.getRequest(); // 可以在这里添加额外的逻辑来从请求中获取用户信息 // 例如从请求头、session等获取用户信息 } } } catch (Exception e) { // 记录异常但不中断处理流程 System.err.println("获取当前登录用户时发生异常: " + e.getMessage()); // 可以考虑记录日志 } return username; } /** * 获取当前租户 ID */ private Integer getCurrentTenantId() { Integer result = null; // 首先尝试从线程局部变量中获取租户ID(适用于定时任务等场景) Integer threadLocalTenantId = TenantContext.getCurrentTenant(); if (threadLocalTenantId != null) { result = threadLocalTenantId; } else { // 获取当前登录用户的租户ID(优先使用SecurityUtils中储存的LoginUser的租户ID) try { if (SecurityUtils.getAuthentication() != null) { LoginUser loginUser = SecurityUtils.getLoginUser(); if (loginUser != null) { result = loginUser.getTenantId(); } } } catch (Exception e) { // 记录异常但不中断处理 System.err.println("获取当前登录用户租户ID时发生异常: " + e.getMessage()); } if (result == null) { // 尝试从请求头中获取租户ID ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); if (attributes != null) { HttpServletRequest request = attributes.getRequest(); if (request != null) { // 从请求头获取租户ID,假设header名称为"X-Tenant-ID" ; 登录接口前端把租户id放到请求头里 String tenantIdHeader = request.getHeader("X-Tenant-ID"); String requestMethodName = request.getHeader("Request-Method-Name"); // 登录 if ("login".equals(requestMethodName)) { if (tenantIdHeader != null && !tenantIdHeader.isEmpty()) { try { result = Integer.parseInt(tenantIdHeader); } catch (NumberFormatException e) { System.err.println("解析请求头中的租户ID时发生异常: " + e.getMessage()); } } } } } } } // 如果仍然没有获取到租户ID,返回默认值 if (result == null) { System.out.println("警告: 未能获取当前租户ID,将使用默认租户ID 1"); result = 1; // 默认租户ID } return result; } }