package com.ruoyi.ucp.service;

import com.alibaba.druid.pool.DruidDataSource;
import com.ruoyi.ucp.entity.UcpDataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Service
public class DataSourceService {
    private final Map<Integer, DataSource> dataSourceMap = new HashMap<>();

    public void addDataSource(UcpDataSource config) {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.type(DruidDataSource.class);
        dataSourceBuilder.driverClassName(config.getDriverClassName());
        dataSourceBuilder.url(config.getUrl());
        dataSourceBuilder.username(config.getUserName());
        dataSourceBuilder.password(config.getPassWord());

        DataSource dataSource = dataSourceBuilder.build();
        dataSourceMap.put(config.getId(), dataSource);
    }

    public DataSource getDataSource() {
        return dataSourceMap.get(1);
    }

    public void removeDataSource(String id) {
        dataSourceMap.remove(id);
    }
}
