package com.qiaoba.module.tenant.init;

import com.qiaoba.common.base.enums.DataBaseEnum;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.module.tenant.init.impl.MysqlInitTablesStrategy;
import com.qiaoba.module.tenant.init.impl.PgSqlInitTablesStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略工厂-租户初始化-创建表
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/6/27 16:41
 */
@Component
@RequiredArgsConstructor
public class InitTablesStrategyFactory {

    private static Map<String, InitTablesStrategy> FACTORY = new ConcurrentHashMap<>();

    private final MysqlInitTablesStrategy mysqlInitTablesStrategy;
    private final PgSqlInitTablesStrategy pgSqlInitTablesStrategy;


    @PostConstruct
    public void register() {
        FACTORY.put(DataBaseEnum.MY_SQL.getType(), mysqlInitTablesStrategy);
        FACTORY.put(DataBaseEnum.POSTGRE_SQL.getType(), pgSqlInitTablesStrategy);
    }

    public static InitTablesStrategy getStrategy(String name) {
        InitTablesStrategy strategy = FACTORY.get(name);
        if (Objects.isNull(strategy)) {
            throw new ServiceException("[ InitTablesStrategy ]策略工厂异常, 类型:[" + name + "]找不到相对应的策略");
        }
        return strategy;
    }
}
