package xyz.appint.union.dao.dao;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.util.Properties;
import java.util.regex.Pattern;

/**
 * MyBatis分页拦截器
 * Created by Justin on 2014/8/12.
 */
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
public class PaginationInterceptor implements Interceptor {
    protected final Logger logger = LoggerFactory.getLogger(PaginationInterceptor.class);

    private final static String SQL_SELECT_REGEX = "(?is)^\\s*SELECT.*$";
    private final static String SQL_COUNT_REGEX = "(?is)^\\s*SELECT\\s+COUNT\\s*\\(\\s*(?:\\*|\\w+)\\s*\\).*$";
    private static final ThreadLocal<Integer> PAGINATION_TOTAL = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public static final ThreadLocal<Boolean> IS_COUNT = new ThreadLocal<Boolean>() {
        @Override
        protected Boolean initialValue() {
            return false;
        }
    };


    public static int getPaginationTotal() {
        return PAGINATION_TOTAL.get();
    }

    public static void setIsCount(boolean isCount) {
        IS_COUNT.set(isCount);
    }


    public static void clean() {
        PAGINATION_TOTAL.remove();
    }

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

        if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        }


        BoundSql boundSql = statementHandler.getBoundSql();
        String sql = boundSql.getSql();
        // 当为select语句，并且不为select count
        if (sql.matches(SQL_SELECT_REGEX) && !Pattern.matches(SQL_COUNT_REGEX, sql)) {
            if (IS_COUNT.get()) {
                Object parameterObject = boundSql.getParameterObject();
                Connection connection = (Connection) invocation.getArgs()[0];
                int count = PageCountHelper.getCount(mappedStatement, connection, parameterObject);
                PAGINATION_TOTAL.set(count);
                if (count == 0) {
                    return invocation.proceed();
                }
            }
            metaStatementHandler.setValue("delegate.boundSql.sql", getPageSql(sql, rowBounds));
            metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
            metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
        }
        return invocation.proceed();
    }


    @Deprecated
    private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
            StringBuffer keyProperties = new StringBuffer();
            for (String keyProperty : ms.getKeyProperties()) {
                keyProperties.append(keyProperty).append(",");
            }
            keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
            builder.keyProperty(keyProperties.toString());
        }

        builder.timeout(ms.getTimeout());

        builder.parameterMap(ms.getParameterMap());

        builder.resultMaps(ms.getResultMaps());
        builder.resultSetType(ms.getResultSetType());

        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }

    public String getPageSql(String sql, RowBounds rowBounds) {
        StringBuilder pageSql = new StringBuilder();
        if (rowBounds.getLimit() == 0) {
            return pageSql.toString();
        }
        pageSql.append(sql);
        pageSql.append(" LIMIT ");
        if (rowBounds.getOffset() > 0) {
            pageSql.append(rowBounds.getOffset());
            pageSql.append(" , ");
        }
        pageSql.append(rowBounds.getLimit());

        return pageSql.toString();
    }


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


    @Override
    public void setProperties(Properties properties) {

    }
}

@@ -4703,98 +4703,8 @@
 ();%0A
-        if (rowBounds.getLimit() == 0) %7B%0A            return pageSql.toString();%0A        %7D%0A
     

fix cursor page limit = 0 bug