/**
 *
 */
package org.springblade.core.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springblade.core.mp.app.PlatomixAppHandler;
import org.springblade.core.mp.app.PlatomixAppInterceptor;
import org.springblade.core.mp.properties.MybatisPlusProperties;
import org.springblade.core.mp.tenant.PlatomixTenantHandler;
import org.springblade.core.mp.tenant.PlatomixTenantInterceptor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wookvn
 *
 */
@Configuration
@MapperScan(basePackages = "org.springblade.**.mapper.**")
@EnableConfigurationProperties(MybatisPlusProperties.class)
public class MybatisConfiguration {

    /**
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(MybatisPlusProperties mybatisPlusProperties,
                                                         @Qualifier("platomixTenantInterceptor") TenantLineInnerInterceptor platomixTenantInterceptor,
                                                         @Qualifier("platomixAppInterceptor") TenantLineInnerInterceptor platomixAppInterceptor

    ) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setMaxLimit(mybatisPlusProperties.getPageLimit());
        paginationInnerInterceptor.setOverflow(mybatisPlusProperties.getOverflow());
        paginationInnerInterceptor.setOptimizeJoin(mybatisPlusProperties.getOptimizeJoin());
        //多租户设置
        interceptor.addInnerInterceptor(platomixTenantInterceptor);
        if (mybatisPlusProperties.getAppSupport()){
            interceptor.addInnerInterceptor(platomixAppInterceptor);
        }
        // 向Mybatis过滤器链中添加分页拦截器
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

//	@Bean
//	public ConfigurationCustomizer configurationCustomizer() {
//		return configuration -> configuration.setUseDeprecatedExecutor(false);
//	}

    /**
     * 自定义多租户处理器
     * @return TenantHandler
     */
    @Bean("appHandler")
//    @Primary
    public PlatomixAppHandler appHandler(MybatisPlusProperties mybatisPlusProperties) {
        return new PlatomixAppHandler(mybatisPlusProperties.getTenant(), mybatisPlusProperties.getApp());
    }

    /**
     * 自定义租户拦截器
     *
     * @param tenantHandler    多租户处理器
     * @param mybatisPlusProperties 多租户配置类
     * @return BladeTenantInterceptor
     */
    @Bean("platomixAppInterceptor")
//    @Primary
    public PlatomixAppInterceptor appLineInnerInterceptor(PlatomixAppHandler tenantHandler, MybatisPlusProperties mybatisPlusProperties) {
        PlatomixAppInterceptor tenantInterceptor = new PlatomixAppInterceptor();
        tenantInterceptor.setTenantLineHandler(tenantHandler);
        tenantInterceptor.setTenantProperties(mybatisPlusProperties.getTenant());
        return tenantInterceptor;
    }

    /**
     * 自定义多租户处理器
     * @return TenantHandler
     */
    @Bean("tenantHandler")
//    @Primary
    public TenantLineHandler tenantHandler(MybatisPlusProperties mybatisPlusProperties) {
        return new PlatomixTenantHandler(mybatisPlusProperties.getTenant());
    }

    /**
     * 自定义租户拦截器
     *
     * @param tenantHandler    多租户处理器
     * @param mybatisPlusProperties 多租户配置类
     * @return BladeTenantInterceptor
     */
    @Bean("platomixTenantInterceptor")
//    @Primary
    public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantLineHandler tenantHandler, MybatisPlusProperties mybatisPlusProperties) {
        PlatomixTenantInterceptor tenantInterceptor = new PlatomixTenantInterceptor();
        tenantInterceptor.setTenantLineHandler(tenantHandler);
        tenantInterceptor.setTenantProperties(mybatisPlusProperties.getTenant());
        return tenantInterceptor;
    }
}
