诊疗下没有项目功能完善

This commit is contained in:
2026-01-26 10:10:42 +08:00
parent 1975fda73c
commit bd873f81d2
18 changed files with 382 additions and 118 deletions

View File

@@ -5,8 +5,6 @@ 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -14,97 +12,39 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* MyBatis-Plus 自动填充处理器
* 用于自动填充创建时间和更新时间,以及创建人和更新人
*/
@Component
public class MybastisColumnsHandler implements MetaObjectHandler {
private static final Logger logger = LoggerFactory.getLogger(MybastisColumnsHandler.class);
// 设置数据新增时的字段自动赋值规则
// 设置数据新增时候的,字段自动赋值规则
@Override
public void insertFill(MetaObject metaObject) {
logger.info("开始执行 insertFill 自动填充");
// 填充创建时间
Date currentTime = new Date();
this.strictInsertFill(metaObject, "createTime", Date.class, currentTime);
this.strictInsertFill(metaObject, "create_time", Date.class, currentTime);
logger.debug("已填充创建时间: {}", currentTime);
// 获取当前登录用户名
String username = getCurrentUsername();
logger.debug("获取到当前用户名: {}", username);
// 填充创建人
this.strictInsertFill(metaObject, "createBy", String.class, username);
this.strictInsertFill(metaObject, "create_by", String.class, username);
logger.debug("已填充创建人: {}", 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);
logger.debug("已填充租户ID: {}", tenantId);
logger.info("insertFill 自动填充完成");
}
// 设置数据修改时的字段自动赋值规则
@Override
public void updateFill(MetaObject metaObject) {
logger.info("开始执行 updateFill 自动填充");
// 填充更新时间
Date currentTime = new Date();
this.strictUpdateFill(metaObject, "updateTime", Date.class, currentTime);
this.strictUpdateFill(metaObject, "update_time", Date.class, currentTime);
logger.debug("已填充更新时间: {}", currentTime);
// 填充更新人
String username = getCurrentUsername();
logger.debug("获取到当前用户名: {}", username);
this.strictUpdateFill(metaObject, "updateBy", String.class, username);
this.strictUpdateFill(metaObject, "update_by", String.class, username);
logger.debug("已填充更新人: {}", username);
logger.info("updateFill 自动填充完成");
}
/**
* 获取当前登录用户名
* @return 当前登录用户名,如果无法获取则返回 "system"
*/
private String getCurrentUsername() {
String username = "system"; // 默认值
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
String username = "system";
try {
LoginUser loginUser = SecurityUtils.getLoginUser();
if (loginUser != null) {
username = loginUser.getUsername();
logger.debug("从SecurityContext获取到用户名: {}", username);
} else {
logger.warn("SecurityContext中没有找到登录用户信息");
// 尝试从请求中获取用户信息
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
// 可以在这里添加额外的逻辑来从请求中获取用户信息
// 例如从请求头、session等获取用户信息
}
}
} catch (Exception e) {
// 记录异常但不中断处理流程
logger.error("获取当前登录用户时发生异常: ", e);
} catch (Exception ignored) {
}
// 使用 fillStrategy 而不是 strictInsertFill确保即使字段已设置也能填充如果为null
this.fillStrategy(metaObject, "createBy", username != null ? username : "system");
this.fillStrategy(metaObject, "tenantId", getCurrentTenantId());
}
return username;
// 设置数据修改update时候的字段自动赋值规则
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
String username = "system";
try {
LoginUser loginUser = SecurityUtils.getLoginUser();
if (loginUser != null) {
username = loginUser.getUsername();
}
} catch (Exception ignored) {
}
this.strictUpdateFill(metaObject, "updateBy", String.class, username);
}
/**
@@ -121,14 +61,10 @@ public class MybastisColumnsHandler implements MetaObjectHandler {
// 获取当前登录用户的租户ID优先使用SecurityUtils中储存的LoginUser的租户ID
try {
if (SecurityUtils.getAuthentication() != null) {
LoginUser loginUser = SecurityUtils.getLoginUser();
if (loginUser != null) {
result = loginUser.getTenantId();
}
result = SecurityUtils.getLoginUser().getTenantId();
}
} catch (Exception e) {
// 记录异常但不中断处理
logger.error("获取当前登录用户租户ID时发生异常: ", e);
result = 1; // 默认租户ID
}
if (result == null) {
@@ -136,31 +72,19 @@ public class MybastisColumnsHandler implements MetaObjectHandler {
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) {
logger.error("解析请求头中的租户ID时发生异常: ", e);
}
}
// 从请求头获取租户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()) {
result = Integer.parseInt(tenantIdHeader);
}
}
}
}
}
// 如果仍然没有获取到租户ID返回默认值
if (result == null) {
logger.warn("未能获取当前租户ID将使用默认租户ID 1");
result = 1; // 默认租户ID
}
return result;
return result != null ? result : 1; // 默认租户ID
}
}