package com.xunhong.erp.turbo.datasource.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.xunhong.erp.turbo.datasource.interceptor.EmptyListInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shenyifei
 */
@Configuration
@MapperScan(basePackages = "com.xunhong.erp.turbo.*.mapper")
public class DatasourceConfiguration {


	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		interceptor.addInnerInterceptor(emptyListInterceptor());
		//乐观锁插件
		interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
		//防全表更新与删除插件
		interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
		//分页插件
		//配置mybatis plus分页拦截器,放在租户插件之后，避免租户的id没有拼接
		PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
		paginationInnerInterceptor.setDbType(DbType.MYSQL);
		paginationInnerInterceptor.setDialect(new MySqlDialect());
		interceptor.addInnerInterceptor(paginationInnerInterceptor);
		return interceptor;
	}

	@Bean
	public EmptyListInterceptor emptyListInterceptor() {
		return new EmptyListInterceptor();
	}

	@Bean
	public IdentifierGenerator identifierGenerator() {
		return new DefaultIdentifierGenerator(1, 1);
	}
}
