package com.cesgroup.mybatiswork.intercepter;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
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.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

@Intercepts(@Signature(type = StatementHandler.class, method = "query",
args = {Statement.class,ResultHandler.class}))
public class Mymybatisintercepter implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		if(invocation.getTarget() instanceof RoutingStatementHandler) {
			RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
			StatementHandler delegate = (StatementHandler) ReflectHelper.getFieldValue(statementHandler, "delegate");  
			BoundSql boundSql = delegate.getBoundSql();
            Object paramObj = boundSql.getParameterObject();
            TypeHandlerRegistry typeHandlerRegistry = (TypeHandlerRegistry) ReflectHelper.getFieldValue(delegate, "typeHandlerRegistry");
            Configuration configuration = (Configuration) ReflectHelper.getFieldValue(delegate,"configuration");
            List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
            List<Object> paraList=new ArrayList<Object>();
            if(null!=parameterMappings) {
            	for (int i = 0; i < parameterMappings.size(); i++) {
            		ParameterMapping parameterMapping = parameterMappings.get(i);
            		if(parameterMapping.getMode()!=ParameterMode.OUT) {
            			Object valueObject;
            			//Class<?> paramTypeClass = parameterMapping.getJavaType();
            		//	String typeString =paramTypeClass.getName();
            			String propertyNameString = parameterMapping.getProperty();
            			if(boundSql.hasAdditionalParameter(propertyNameString)) {
            				valueObject = boundSql.getAdditionalParameter(propertyNameString);
            			}else if(null==paramObj) {
							valueObject =null;
						}else if(typeHandlerRegistry.hasTypeHandler(paramObj.getClass())) {
            			    valueObject = paramObj;
            		}else {
						MetaObject metaObject = configuration.newMetaObject(paramObj);
						valueObject = metaObject.getValue(propertyNameString);
            		}
            			paraList.add(valueObject);
				}
            }
            	String sqlString = boundSql.getSql();
            	for (Object param : paraList) {
					sqlString= sqlString.replace("?", "\""+param+"\""+"");
				}
            	System.out.println("boundsql:"+sqlString);
		}   
	  }
		 return invocation.proceed();
	}
	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties arg0) {
	}

}
