package com.ruoyi.framework.config;

import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.keygen.KeyGenerators;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mybatisflex.core.FlexGlobalConfig;
import org.mybatis.spring.annotation.MapperScan;
import jakarta.annotation.PostConstruct;
import com.github.pagehelper.PageInterceptor;

/**
 * Mybatis支持*匹配扫描包
 * Mybatis-Flex配置
 *
 * @author dataprince数据小王子
 */
@Configuration
@MapperScan("com.ruoyi.**.mapper")
public class MyBatisConfig
{
    @Bean
    public PageInterceptor pageInterceptor(){
        PageInterceptor pageInterceptor = new PageInterceptor();
        return pageInterceptor;
    }

    @PostConstruct
    public void setFlexConfig()
    {
        FlexGlobalConfig globalConfig = FlexGlobalConfig.getDefaultConfig();
        // 关闭banner
        globalConfig.setPrintBanner(false);

        //统一设置数据库表主键为雪花算法
        FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
        keyConfig.setKeyType(KeyType.Generator);
        keyConfig.setValue(KeyGenerators.snowFlakeId);
        //keyConfig.setBefore(true);
        FlexGlobalConfig.getDefaultConfig().setKeyConfig(keyConfig);

        //设置数据库正常时的值
        globalConfig.setNormalValueOfLogicDelete("0");
        //设置数据已被删除时的值
        globalConfig.setDeletedValueOfLogicDelete("1");
    }
}