package com.seer.teach.common.config;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.seer.teach.common.config.mybatis.plugin.MybatisSqlFormatPlugin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Properties;

/**
 * @Author: Captain
 * @Autograph: 安稳
 * @Description:
 * @Date: 2023-07-23 01:31:19
 */
@EnableTransactionManagement
@Configuration // 配置类
public class MyBatisPlusConfig {

    @Value("${mybatis.print-sql:true}")
    private String printSql;

    /**
     * @Author: Captain
     * @Desc: 把自动注入数据的配置类注入进MyabtisPlus里面
     * @Params: []
     * @Return: com.tianyi.config.MyMetaObjectHandler
     * @Time: 2024/1/22 11:54
     */
    @Bean
    public MyMetaObjectHandler myMetaObjectHandler() {
        return new MyMetaObjectHandler();
    }

    @Bean
    public UpdateTimePlugin updateTimePlugin() {
        return new UpdateTimePlugin();
    }

    @Bean
    public MybatisSqlFormatPlugin mybatisSqlFormatPlugin() {
        MybatisSqlFormatPlugin plugin = new MybatisSqlFormatPlugin();
        // 设置插件属性，启用SQL打印
        Properties properties = new Properties();
        properties.setProperty("printSql", printSql);
        plugin.setProperties(properties);
        return plugin;
    }

    /**
     * 乐观锁插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(UpdateTimePlugin updateTimePlugin,MybatisSqlFormatPlugin mybatisSqlFormatPlugin) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        interceptor.addInnerInterceptor(updateTimePlugin);
        interceptor.addInnerInterceptor(mybatisSqlFormatPlugin);
        return interceptor;
    }
}