package com.heyu.api.data.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.lz.mybatis.plugin.config.ResolverBeanPostProcessor;
import com.lz.mybatis.plugins.interceptor.MapF2FInterceptor;
import com.lz.mybatis.plugins.interceptor.utils.PSpringContextUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.List;

/**
 * mybatis-plus配置类
 *
 * @author Lion Li
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
// 指定要扫描的Mapper类的包的路径
@MapperScan("${mybatis-plus.mapperPackage}")
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 乐观锁插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        return interceptor;
    }

    /**
     * 分页插件，自动识别数据库类型
     * https://baomidou.com/guide/interceptor-pagination.html
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置数据库类型为mysql
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        return paginationInnerInterceptor;
    }




    @Bean("mapF2FInterceptor")
    public MapF2FInterceptor mapF2FInterceptor() {
        return new MapF2FInterceptor();
    }

    //打印日志
    @Bean("sqlLogInterceptor")
    public SqlLogInterceptor sqlLogInterceptor() {
        return new SqlLogInterceptor();
    }

    //打印日志
    @Bean("sQLInsertLogInterceptr")
    public SQLInsertLogInterceptor sQLInsertLogInterceptr() {
        return new SQLInsertLogInterceptor();
    }

    //打印日志
    @Bean("sQLBatchInsertLogInterceptor")
    public SQLBatchInsertLogInterceptor sqlBatchInsertLogInterceptor() {
        return new SQLBatchInsertLogInterceptor();
    }

    //打印日志
    @Bean("sqlSelectLogInterceptor")
    public SqlSelectLogInterceptor sqlSelectLogInterceptor() {
        return new SqlSelectLogInterceptor();
    }


    /**
     * 乐观锁插件
     * https://baomidou.com/guide/interceptor-optimistic-locker.html
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }

    @Bean
    public ResolverBeanPostProcessor resolverBeanPostProcessor() {
        return new ResolverBeanPostProcessor(new MyBatisBaomidouServiceImpl());
    }


    @Bean
    public PSpringContextUtils pSpringContextUtils() {
        return new PSpringContextUtils();
    }


}
