package com.njzscloud.common.mp.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.njzscloud.common.mp.support.CustomDataPermissionHandler;
import com.njzscloud.common.mp.support.MetaObjectHandlerImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Mybatis-Plus 配置
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({MybatisPlusProperties.class})
public class MpAutoConfiguration {

    /**
     * 插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(MybatisPlusProperties mybatisPlusProperties) {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 分页
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 乐观锁
        // mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 防止全表更新删除
        mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        // DataPermissionInterceptor dataPermissionInterceptor = new DataPermissionInterceptor(new CustomDataPermissionHandler(mybatisPlusProperties.getIgnoreTables()));
        // mybatisPlusInterceptor.addInnerInterceptor(dataPermissionInterceptor);
        return mybatisPlusInterceptor;
    }

    @Bean
    public MetaObjectHandlerImpl metaObjectHandlerImpl() {
        return new MetaObjectHandlerImpl();
    }
}
