package com.bonus.core.config.mybatisPlusConfig;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.bonus.core.config.json.JsonNodeTypeHandler;
import com.github.pagehelper.dialect.helper.MySqlDialect;
import com.github.pagehelper.page.PageAutoDialect;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;
import org.springframework.aop.Advisor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Role;

import javax.sql.DataSource;
import java.util.List;

//@AutoConfiguration(
//   after = {DataSourceAutoConfiguration.class}
//)
//@ConditionalOnBean({DataSource.class})
//@EnableConfigurationProperties({TenantConfigProperties.class})
@Configuration
@Import({JsonNodeTypeHandler.class, LocalDateTimeTypeHandler.class})
public class MybatisPlusConfiguration {
//   @Bean
//   @ConditionalOnMissingBean
//   public MybatisPlusInterceptor paginationInterceptor() {
//      MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//      interceptor.addInnerInterceptor(new MasterSlaveInterceptor());
//      DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
//      dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
//         if (RoutingRule.isUseGlobalTs()) {
//            String var10000;
//            if (TenantContextHolder.getTenantId() != null) {
//               var10000 = RoutingRule.getContext().getDatabase();
//               return var10000 + ".t_" + TenantContextHolder.getTenantId() + "_" + tableName;
//            } else {
//               var10000 = RoutingRule.getContext().getDatabase();
//               return var10000 + ".t_" + tableName;
//            }
//         } else {
//            return tableName;
//         }
//      });
//      interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
//      interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//      return interceptor;
//   }
//
//   @Bean
//   public ConfigurationCustomizer configurationCustomizer(ObjectProvider<List<SqlMutator>> sqlMutatorProvider) {
//      sqlMutatorProvider.ifAvailable((sqlMutators) -> {
//         MybatisXMLDriver.sqlMutators = sqlMutators;
//      });
//      return (configuration) -> {
//         configuration.setDefaultScriptingLanguage(MybatisXMLDriver.class);
//      };
//   }
//
//   @Bean
//   @Role(2)
//   public Advisor useGlobalTsAnnotationAdvisor() {
//      return new UseDataSourceAnnotationAdvisor(UseGlobalTs.class);
//   }
//
//   @Bean
//   @Role(2)
//   public Advisor timeSerialAnnotationAdvisor() {
//      return new UseDataSourceAnnotationAdvisor(UseSystem.class);
//   }
//
//   static {
//      PageAutoDialect.registerDialectAlias("taos-rs", MySqlDialect.class);
//      PageAutoDialect.registerDialectAlias("taos", MySqlDialect.class);
//   }
}
