package com.jay.monitor.data.server.factory.callback;

import com.jay.monitor.data.server.enums.DataSplitType;
import com.jay.monitor.data.server.store.SplitDataManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Module 回调
 * @Description 当生成编号发生改变的时候, 默认触发的事件
 * @Author liukaixiong
 * @Date 2020/11/6 16:14
 */
@Component
public class DefaultIdChangeCallback implements IdChangeCallback, ApplicationContextAware {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void onChange(DataSplitType splitType, String id) {
        // todo 后续这里要优化，不是非得指定MYSQL数据库
        getDataManager().generateMysql(splitType);
    }

    private SplitDataManager getDataManager() {
        return this.applicationContext.getBean(SplitDataManager.class);
    }
}
