package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ruoyi.ibs.handler.MyTableNameHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Mybatis Plus 配置
 * 
 * @author ruoyi
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig
{
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor()
    {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 乐观锁插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        // 阻断插件
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());

        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler(
                new MyTableNameHandler(
                        "cust_info_business",
                        "cust_info_merchant",
                        "cust_info_retail",
                        "ibs_anchor",
                        "ibs_anchor_address",
                        "cust_dept_user_grid",
                        "draw_shape_cust",
                        "grid_virtual_cust_user",
                        "grid_virtual_cust",
                        "grid_virtual_cust_user_unbind",
                        "grid_region_cust_user_unbind",
                        "cust_dept_user_cmpm",
                        "load_ind_dm_pa_item_home",
                        "loan_contract_due_warn_orc",
                        "family_member",
                        "grid_cmpm_count_gongsi",
                        "grid_cmpm_count_lingshou",
                        "grid_cust_count_gongsi",
                        "grid_cust_count_lingshou",
                        "draw_layer_audit",
                        "admin_region_index",
                        "campaign_count",
                        "visit_info",
                        "visit_campaign_count",
                        "visit_trajectory",
                        "grid_virtual_count_lingshou",
                        "grid_virtual_count_gongsi",
                        "grid_virtual_cust_gongsi",
                        "grid_virtual_cust_lingshou",
                        "grid_region_cust_user",
                        "grid_shape_count_gongsi",
                        "grid_shape_count_lingshou",
                        "grid_shape_count_gongsi_detail",
                        "grid_shape_count_lingshou_detail",
                        "grid_shape_cust_gongsi",
                        "grid_shape_cust_lingshou",
                        "grid_union_cust_gongsi",
                        "grid_union_cust_lingshou",
                        "grid_union_cmpm_gongsi",
                        "grid_union_cmpm_lingshou",
                        "signed_products",
                        "sys_cust_tag",
                        "merchant_mcsp_info",
                        "appointment_info",
                        "tab_ranking",
                        "grid_cmpm_retail",
                        "grid_cmpm_corporate",
                        "grid_cmpm_corporate_account",
                        "dwb_retail_cust_level_manager_detail"
                        )
        );
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        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;
    }

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

    /**
     * 如果是对全表的删除或更新操作，就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
     */
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
    {
        return new BlockAttackInnerInterceptor();
    }
}