package com.changhong.epc.config.sqlfilter; import com.changhong.epc.config.tool.MybatisTool; import com.changhong.epc.constter.base.Context; import com.iemsoft.framework.cloud.core.tools.StringUtil; import com.iemsoft.framework.cloud.mybatis.interceptor.MybatisFilter; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.statement.StatementHandler; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Optional; /** * 基础字段sql拦截 */ @Component @Slf4j @Order(Integer.MIN_VALUE) public class BaseParamSqlFilter implements MybatisFilter, SqlFilter { @Override public void filter(StatementHandler rsh, Object[] args, Method method) { try { MybatisTool.setSql(rsh, filter(MybatisTool.getSql(rsh))); }catch (Exception e){ log.error("基础数据添加错误:{}", e.getMessage()); log.error(e.getMessage(), e); } } @Override public String filter(String sql) { return sql.replace("{companyId}", Context.getCompanyIdStr()) .replace("{tenantId}", Context.getTenantIdStr()) .replace("{openId}", Optional.ofNullable(Context.getOpenId()).orElse("")) .replace("{rule}" , StringUtil.toString(Context.getTenantId(), "") +"_"+ StringUtil.toString(Context.getCompanyId(), "")); } @Override public void filter(Executor e, Object[] args, Method method) { } }