package net.tccn.base.dbq.jdbc.api;

import net.tccn.base.MetaKit;
import net.tccn.base.Utils;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * Db 最终执行层
 * Created by liangxianyou at 2019/3/12 14:11.
 */
public class DbKit implements DbSource {

    private DbAccount dbAccount;
    private DbSource dbSource;
    private String catalog;

    /*public DbKit(DbAccount dbAccount) {
        this.dbAccount = dbAccount;
        try {
            DbSource dbSource = X.getDbSource(DbSource.class, dbAccount.getCate());
            dbSource.setDbAccount(dbAccount);

            this.dbSource = dbSource;
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("创建DbKit失败：数据库类型[cate:%s]未知", dbAccount.getCate()));
        }
    }*/
    public DbKit(DbAccount dbAccount, String catalog) {
        this.dbAccount = dbAccount;
        this.catalog = catalog;

        try {
            if (Utils.isEmpty(dbAccount.getPwd())) {
                DbAccount account = MetaKit.getDbPlat(dbAccount.getDbid());
                dbAccount.setPwd(account.getPwd());
            }

            DbSource dbSource = Utils.getDbSource(DbSource.class, dbAccount.getCate());
            dbSource.setDbAccount(dbAccount);
            dbSource.setCatalog(catalog);

            this.dbSource = dbSource;
        } catch (Exception e) {
            e.printStackTrace();
            // throw new IllegalArgumentException(String.format("创建DbKit失败：数据库类型[cate:%s]未知", dbAccount.getCate()));
        }
    }


    @Override
    public void setDbAccount(DbAccount dbAccount) {
        this.dbAccount = dbAccount;
        dbSource.setDbAccount(dbAccount);
    }

    @Override
    public void setCatalog(String catelog) {
        this.catalog = catelog;
        dbSource.setCatalog(catalog);
    }

    @Override
    public <T> List<T> queryList(String sql, Class<T> type) {
        return dbSource.queryList(sql, type);
    }

    @Override
    public <T> T find(String sql, Class<T> type) {
        return dbSource.find(sql, type);
    }

    @Override
    public <T> T findColumn(String sql, Class<T> type) {
        return dbSource.findColumn(sql, type);
    }

    @Override
    public void createTable(String sql) {
        dbSource.createTable(sql);
    }

    @Override
    public void dropTable(String tableName) {
        dbSource.dropTable(tableName);
    }

    public boolean exetute(String sql) {
        return dbSource.exetute(sql);
    }

    @Override
    public int exetuteUpdate(String sql) {
        return dbSource.exetuteUpdate(sql);
    }

    // -----------------------------------------
    public <T> CompletableFuture<T> findAsync(String sql, Class<T> type) {
        return CompletableFuture.supplyAsync(() -> find(sql, type));
    }

    public <T> CompletableFuture<List<T>> queryListAsync(String sql, Class<T> type) {
        return CompletableFuture.supplyAsync(() -> queryList(sql, type));
    }

    public <T> CompletableFuture<T> findColumnAsync(String sql, Class<T> type) {
        return CompletableFuture.supplyAsync(() -> findColumn(sql, type));
    }

    public CompletableFuture<Void> exetuteAsync(String sql) {
        return CompletableFuture.runAsync(() -> exetute(sql));
    }

    @Override
    public String getType() {
        return null;
    }
}
