liusheng
6 天以前 c7c90b74d723ab08069d40b6b1fadddb2deeeb7e
河南统一去ORGID
已修改1个文件
55 ■■■■■ 文件已修改
ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/HospitalFilterInterceptor.java 55 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/HospitalFilterInterceptor.java
@@ -3,22 +3,28 @@
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import jdk.nashorn.internal.objects.annotations.Where;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;
@Component
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class HospitalFilterInterceptor implements Interceptor {
    @Value("${spring.profiles.active}")
    private String active;
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
@@ -38,6 +44,13 @@
        // 获取原始 SQL
        String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");
        //河南的特殊要求,不要orgid
        if (active.equals("hn")) {
            String s = removeOrgIdCondition(originalSql);
            // 将修改后的 SQL 写回
            metaObject.setValue("delegate.boundSql.sql", s);
            return invocation.proceed();
        }
        // 获取当前 orgid
        String orgid = null;
@@ -63,6 +76,46 @@
        return invocation.proceed();
    }
    private String removeOrgIdCondition(String sql) {
        // 转换为小写便于匹配,但保留原始大小写
        String lowerSql = sql.toLowerCase();
        // 查找 WHERE 子句的位置
        int whereIndex = lowerSql.indexOf("where");
        if (whereIndex == -1) {
            return sql; // 没有 WHERE 子句
        }
        // 提取 WHERE 之后的部分
        String whereClause = sql.substring(whereIndex + 5);
        String beforeWhere = sql.substring(0, whereIndex);
        // 使用正则表达式分割条件
        String[] conditions = whereClause.split("(?i)\\s+(AND|OR)\\s+");
        // 过滤掉包含 orgid 的条件
        List<String> filteredConditions = new ArrayList<>();
        for (String condition : conditions) {
            if (!containsOrgId(condition)) {
                filteredConditions.add(condition.trim());
            }
        }
        // 重新构建 SQL
        if (filteredConditions.isEmpty()) {
            return beforeWhere.trim();
        } else {
            String newWhereClause = String.join(" AND ", filteredConditions);
            return beforeWhere + "WHERE " + newWhereClause;
        }
    }
    private boolean containsOrgId(String condition) {
        // 检查条件是否包含 orgid(考虑别名)
        return condition.toLowerCase().matches(".*\\b(\\w+\\.)?orgid\\s*[=<>].*");
    }
    /**
     * 修改 SQL,注入 orgid 条件
     *