package com.dgtly.apiframework.interceptor;


import com.dgtly.common.annotation.SwitchEn;
import com.dgtly.common.utils.ServletUtils;
import com.dgtly.common.utils.StringUtils;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Properties;

/*放过中英文切换*/
//@Component
//@Intercepts({@Signature(
//        type = Executor.class,
//        method = "query",
//        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
//), @Signature(
//        type = Executor.class,
//        method = "query",
//        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
//)})
public class SwitchEnSqlInterceptor  implements Interceptor {
    private Logger log= LoggerFactory.getLogger(SwitchEnSqlInterceptor.class);
    public SwitchEnSqlInterceptor() {
    }

    public Object intercept(Invocation invocation) throws Throwable {
        // 获取原始sql语句
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Object parameter = invocation.getArgs()[1];
        BoundSql boundSql = (BoundSql) invocation.getArgs()[5];
        String oldsql = boundSql.getSql();
        //oldsql = oldsql.replaceAll("gi.name","gi.name_en name");

        //获取前端标识是否切换获取中英文切换
        HttpServletRequest request = ServletUtils.getRequest();
        String language = request.getHeader("language");

        if(StringUtils.isNotEmpty(language) && "en".equalsIgnoreCase(language)){
            // 方法中含有获取注解
            Class<?> classType = Class.forName(mappedStatement.getId().substring(0,mappedStatement.getId().lastIndexOf(".")));
            String mName  = mappedStatement.getId().substring(mappedStatement.getId().lastIndexOf(".") + 1 ,mappedStatement.getId().length());
            for(Method method : classType.getDeclaredMethods()){
                if(method!=null && mName.equals(method.getName()) && method.isAnnotationPresent(SwitchEn.class))
                {
                    SwitchEn switchEn = method.getAnnotation(SwitchEn.class);
                    String[] oldfiles = switchEn.oldfiles();
                    String[] newfiles = switchEn.newfiles();
                    //旧字段和替换字段长度相同
                    if(newfiles.length==oldfiles.length){
                        for(int i = 0;i<oldfiles.length;i++){
                            oldsql = oldsql.replaceFirst(oldfiles[i],newfiles[i]);
                        }
                    }
                }
            }
            // 改变sql语句
            BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), oldsql ,
                    boundSql.getParameterMappings(), boundSql.getParameterObject());
            invocation.getArgs()[5] = newBoundSql;
        }

        // 继续执行
        Object result = invocation.proceed();
        return result;
    }


    public Object plugin(Object target) {
        // TODO Auto-generated method stub
        return Plugin.wrap(target, this);
    }

    public void setProperties(Properties properties) {

    }
}
