package com.sunwin.metro.utils;

import org.apache.commons.dbcp2.BasicDataSource;
import pro.husk.mysql.MySQL;

import java.sql.Connection;

/**
 * @program: sunwin_metro
 * @description: mysql工具类
 * @author: xuYJ
 * @create: 2022-01-14 08:42
 **/
public class MysqlUtil {

    private static Connection con;
    private static MySQL sql3;

    static {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://192.168.20.72:5432/urtmpdb?characterEncoding=utf-8&serverTimezone=UTC");
        dataSource.setUsername("postgres");
        dataSource.setPassword("sw12345");
        dataSource.setMaxWaitMillis(1000 * 60 * 10);
        dataSource.setInitialSize(10);
        dataSource.setMaxTotal(50);
        dataSource.setMinIdle(2);
        try {
            con = dataSource.getConnection();
            System.out.println("创建连接池：" + con);
            con.setAutoCommit(false);
        } catch (Exception e) {
            System.out.println("-----------mysql get connection has exception , msg = " + e.getMessage());
        }
        sql3 = new MySQL("jdbc:postgresql://192.168.20.72:5432/urtmpdb?characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC&rewriteBatchedStatements=true",
                "postgres",
                "sw12345");


    }

    /**
     * 获取postgres数据库链接
     */
    public static Connection getCon() {
        if (null == con) {
            BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName("org.postgresql.Driver");
            dataSource.setUrl("jdbc:postgresql://192.168.20.72:5432/urtmpdb?characterEncoding=utf-8&serverTimezone=UTC");
            dataSource.setUsername("postgres");
            dataSource.setPassword("sw12345");
            dataSource.setMaxWaitMillis(1000 * 60 * 10);
            dataSource.setInitialSize(10);
            dataSource.setMaxTotal(50);
            dataSource.setMinIdle(2);
            try {
                con = dataSource.getConnection();
                System.out.println("异常后重新创建连接池：" + con);
                con.setAutoCommit(false);
            } catch (Exception e) {
                System.out.println("-----------mysql get connection has exception , msg = " + e.getMessage());
            }
        }
        return con;
    }

    public static Connection getCon2() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://192.168.20.72:5432/urtmpdb?characterEncoding=utf-8&serverTimezone=UTC");
        dataSource.setUsername("postgres");
        dataSource.setPassword("sw12345");
        dataSource.setMaxWaitMillis(1000 * 60 * 10);
        dataSource.setInitialSize(10);
        dataSource.setMaxTotal(50);
        dataSource.setMinIdle(2);
        try {
            con = dataSource.getConnection();
            System.out.println("异常后重新创建连接池：" + con);
            con.setAutoCommit(false);
        } catch (Exception e) {
            System.out.println("-----------mysql get connection has exception , msg = " + e.getMessage());
        }
        return con;
    }


    public static MySQL getProMeter() {
        if (null == sql3) {
            sql3 = new MySQL("jdbc:postgresql://192.168.20.72:5432/urtmpdb?characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC&rewriteBatchedStatements=true",
                    "postgres",
                    "sw12345");
        }
        return sql3;
    }

    public static MySQL getProMeter2() {
        sql3 = new MySQL("jdbc:postgresql://192.168.20.72:5432/urtmpdb?characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC&rewriteBatchedStatements=true",
                "postgres",
                "sw12345");
        return sql3;
    }
}
