//package com.jay.monitor.data.server.ext.mybatis.interceptor;
//
//import com.baomidou.mybatisplus.core.metadata.TableInfo;
//import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
//import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
//import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
//import com.jay.monitor.data.server.config.props.MonitorProperties;
//import com.jay.monitor.data.server.enums.DataSplitType;
//import com.jay.monitor.data.server.store.mysql.MysqlDDLProcess;
//import com.jay.monitor.data.server.utils.MonitorPropertiesUtil;
//import org.apache.ibatis.executor.statement.StatementHandler;
//import org.apache.ibatis.mapping.*;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.plugin.Intercepts;
//import org.apache.ibatis.plugin.Invocation;
//import org.apache.ibatis.plugin.Signature;
//import org.apache.ibatis.reflection.MetaObject;
//import org.apache.ibatis.reflection.SystemMetaObject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.sql.Connection;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * @Module 拦截器
// * @Description 负责重写表名称的数据，按照拆分的规则找到对应的表。
// * @Author liukaixiong
// * @Date 2020/11/5 10:55
// */
//@Component
//@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
//public class SplitTableInterceptor extends AbstractSqlParserHandler implements Interceptor {
//
//    @Autowired
//    private MysqlDDLProcess ddlProcess;
//
//    @Autowired
//    private MonitorProperties monitorProperties;
//
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//
//        StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
//        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
//        // SQL 解析
//        this.sqlParser(metaObject);
//        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//
//        // 如果是查询方法
//        if (SqlCommandType.SELECT == mappedStatement.getSqlCommandType()
//                || StatementType.CALLABLE == mappedStatement.getStatementType()) {
//            return invocation.proceed();
//        }
//        // 针对定义了rowBounds，做为mapper接口方法的参数
//        BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
//        TableInfo tableInfo = TableInfoHelper.getTableInfo(getRequestClass(boundSql));
//        if (tableInfo != null) {
//            String tableName = tableInfo.getTableName();
//            // 从配置文件中获取对应的规则
//            Map<String, DataSplitType> tableRuleMap = MonitorPropertiesUtil.getMysqlTableRuleMap(monitorProperties);
//
//            if (tableRuleMap != null) {
//                DataSplitType dataSplitType = tableRuleMap.get(tableName);
//                if (dataSplitType != null) {
//                    String sql = boundSql.getSql();
//                    String dataId = ddlProcess.getDataId(tableName, dataSplitType);
//                    String newSql = sql.replace(tableName, dataId);
//                    List<ParameterMapping> mappings = new ArrayList<>(boundSql.getParameterMappings());
//                    metaObject.setValue("delegate.boundSql.sql", newSql);
//                    metaObject.setValue("delegate.boundSql.parameterMappings", mappings);
//                }
//            }
//        }
//        return invocation.proceed();
//    }
//
//    private Class<?> getRequestClass(BoundSql boundSql) {
//        Object parameterObject = boundSql.getParameterObject();
//        if (parameterObject instanceof Map) {
//            Map parameterMap = (Map) parameterObject;
//            if (parameterMap.containsKey("collection")) {
//                Object collection = parameterMap.get("collection");
//                if (collection instanceof List) {
//                    List list = (List) collection;
//                    if (list != null && list.size() > 0) {
//                        return list.get(0).getClass();
//                    }
//                }
//            }
//        }
//        return boundSql.getParameterObject().getClass();
//    }
//}
