package com.blueland.common.core.config;

import com.blueland.common.core.domain.BaseEntity;
import com.blueland.common.core.listener.FlexCommonListener;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer {

    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        // 我们可以在这里进行一些列的初始化配置
        FlexCommonListener flexCommonListener = new FlexCommonListener();
        globalConfig.registerInsertListener(flexCommonListener, BaseEntity.class);
        globalConfig.registerUpdateListener(flexCommonListener, BaseEntity.class);

        FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
        keyConfig.setKeyType(KeyType.Generator);
        keyConfig.setValue(KeyGenerators.snowFlakeId);
        keyConfig.setBefore(true);

        globalConfig.setKeyConfig(keyConfig);
    }

}