package com.nbport.zgwl.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Value("${spring.datasource.url:}")
    private String datasourceUrl;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor =
                new PaginationInnerInterceptor(resolveDbType(datasourceUrl));
        paginationInnerInterceptor.setMaxLimit(500L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

    private DbType resolveDbType(String jdbcUrl) {
        if (jdbcUrl == null) {
            return DbType.MYSQL;
        }
        String url = jdbcUrl.toLowerCase();
        if (url.startsWith("jdbc:oracle:")) {
            return DbType.ORACLE;
        }
        if (url.startsWith("jdbc:mysql:")) {
            return DbType.MYSQL;
        }
        return DbType.MYSQL;
    }
}
