All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.minlia.rocket.data.scope.DataScopeInterceptor Maven / Gradle / Ivy

There is a newer version: 2.0.4.1.RELEASE
Show newest version
package com.minlia.rocket.data.scope;

import com.baomidou.mybatisplus.toolkit.PluginUtils;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

/**
 * @author will
 */
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class,
    Integer.class})})
public class DataScopeInterceptor implements Interceptor {

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils
        .realTarget(invocation.getTarget());
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler
        .getValue("delegate.mappedStatement");

    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
      return invocation.proceed();
    }

    BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();

    //查找参数中包含DataScope类型的参数
    DataScope dataScope = findDataScopeObject(parameterObject);

    if (dataScope == null) {
      return invocation.proceed();
    } else {
      String scopeName = dataScope.getScopeName();
      List deptIds = dataScope.getScopeValue();
      String join = StringUtils.join(deptIds, ",");
      originalSql =
          "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName
              + " in (" + join + ")";
      metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
      return invocation.proceed();
    }
  }

  public DataScope findDataScopeObject(Object parameterObj) {
    if (parameterObj instanceof DataScope) {
      return (DataScope) parameterObj;
    } else if (parameterObj instanceof Map) {
      for (Object val : ((Map) parameterObj).values()) {
        if (val instanceof DataScope) {
          return (DataScope) val;
        }
      }
    }
    return null;
  }

  @Override
  public Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }

  @Override
  public void setProperties(Properties properties) {

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy