package com.izouma.awesomeadmin.interceptor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
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.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;

/**
 * <p>分页拦截器 </p>
 *
 * @author 姓名 <br />
 * 更新履历 <br />
 * 日期 : 姓名: 更新内容<br />
 * @version 1.0
 */

@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
public class PageInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObjet = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY,
                SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
        MappedStatement mappedStatement = (MappedStatement) metaObjet.getValue("delegate.mappedStatement");
        //配置文件中SQL语句的ID
        String id = mappedStatement.getId();
        //检查以ByPage结尾的id
        if (id.matches(".+ByPage$")) {
            BoundSql bounSql = statementHandler.getBoundSql();
            //原始的SQL语句
            String sql = bounSql.getSql();
            Connection connection = (Connection) invocation.getArgs()[0];

            //默认mysql数据库
            //查询总条数的SQL语句
            String countSql = "select count(1) from (" + sql + ") aliasForPage";

            Page page = new Page();
            Map<?, ?> parameter = (Map<?, ?>) bounSql.getParameterObject();
            if (parameter != null) {

                page = (Page) parameter.get(AppConstant.PAGE);
            }


            if ("SqlServer".equals(getDataBaseType(connection))) {

                String baseSql = sql;
                if (sql.toUpperCase().lastIndexOf(" ORDER ") > 0) {

                    baseSql = sql.substring(0, sql.toUpperCase().lastIndexOf("ORDER"));
                }

                countSql = "select count(1) from (" + baseSql + ") aliasForPage";

            }


            PreparedStatement countStatement = connection.prepareStatement(countSql);
            ParameterHandler parameterHandler = (ParameterHandler) metaObjet.getValue("delegate.parameterHandler");
            parameterHandler.setParameters(countStatement);
            ResultSet rs = countStatement.executeQuery();
            if (rs.next()) {
                page.setTotalNumber(rs.getInt(1));
            }

            //改造后带分页查询的SQL语句
            String pageSql = sql + " limit " + page.getDbIndex() + "," + page.getDbNumber();

            if ("SqlServer".equals(getDataBaseType(connection))) {
                if (sql.toUpperCase().lastIndexOf(" ORDER ") < 0) {
                    sql += " ORDER BY 1 ";
                }
                //改造后带分页查询的SQL语句
                pageSql = sql + " offset " + page.getDbIndex() + " row fetch next " + page.getDbNumber() + " row only ";

            }

            metaObjet.setValue("delegate.boundSql.sql", pageSql);

        }
        return invocation.proceed();
    }


    @Override
    public Object plugin(Object target) {

        return Plugin.wrap(target, this);
    }


    @Override
    public void setProperties(Properties properties) {

        // TODO Auto-generated method stub

    }


    /**
     * 判断数据库类型
     *
     * @return
     */
    public static String getDataBaseType(Connection connection) {
        //通过driverName是否包含关键字判断
        try {
            if (connection.getMetaData().getDriverName().toUpperCase()
                    .indexOf("MYSQL") != -1) {
                return "MySql";
            } else if (connection.getMetaData().getDriverName().toUpperCase()
                    .indexOf("SQL SERVER") != -1) {
                //sqljdbc与sqljdbc4不同，sqlserver中间有空格
                return "SqlServer";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "-1";
    }

}
