package com.lantone.qc.kernel;

import com.lantone.qc.pub.jdbc.MysqlJdbc;
import com.lantone.qc.pub.util.PropertiesUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @ClassName : MysqlJdbcUtil
 * @Description :
 * @Author : 楼辉荣
 * @Date: 2020-04-16 18:29
 */
public class MysqlJdbcUtil {
    public static MysqlJdbc initOracleJdbc() {
        PropertiesUtil propertiesUtil = new PropertiesUtil("kernel.properties");
        MysqlJdbc mysqlJdbc = new MysqlJdbc(propertiesUtil.getProperty("oracle.taizhou.user"),
                propertiesUtil.getProperty("oracle.taizhou.password"),
                propertiesUtil.getProperty("oracle.taizhou.url"), propertiesUtil.getProperty("oracle.taizhou.driver"));
        return mysqlJdbc;
    }

    public static MysqlJdbc initMysqlJdbc() {
        PropertiesUtil propertiesUtil = new PropertiesUtil("kernel.properties");
        MysqlJdbc mysqlJdbc = new MysqlJdbc(propertiesUtil.getProperty("mysql.test.user"),
                propertiesUtil.getProperty("mysql.test.password"),
                propertiesUtil.getProperty("mysql.test.url"));
        return mysqlJdbc;
    }

    public static Connection getConnection(String hospitalId) throws Exception {
        PropertiesUtil propertiesUtil = new PropertiesUtil("kernel.properties");
        Class.forName(propertiesUtil.getProperty("oracle.test.driver"));

        String url = "";
        String user = "";
        String password = "";
        Properties props = new Properties();
        if ("1".equals(hospitalId)) {
            url = propertiesUtil.getProperty("oracle.test.url");
            // 127.0.0.1是本机地址，1521是Oracle默认连接端口，DATABASE_NAME为实例名（SID）
            user = propertiesUtil.getProperty("oracle.test.user");// 用户系统默认的账户名
            password = propertiesUtil.getProperty("oracle.test.password");// 你安装时选设置的密码
        } else if ("3".equals(hospitalId)) {
            url = propertiesUtil.getProperty("oracle.taizhou.url");
            // 127.0.0.1是本机地址，1521是Oracle默认连接端口，DATABASE_NAME为实例名（SID）
            user = propertiesUtil.getProperty("oracle.taizhou.user");// 用户系统默认的账户名
            password = propertiesUtil.getProperty("oracle.taizhou.password");// 你安装时选设置的密码
        }
        props.put("user", user);
        props.put("password", password);
        props.put("defaultRowPrefetch", "15");
        props.put("remarksReporting", "true");
        //默认情况下不允许用dba身份连接数据库，通过property设置以实现dba连接
        Connection conn = DriverManager.getConnection(url, props);
        return conn;
    }
}