//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tohi.app.rest;

import com.tohi.com.tohi.system.db.DBConnectionPool;
import com.tohi.common.pwdutil.PropertiesConfigurerUtil;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.util.StringUtils;

public class JdbcUtil {
    private static String url = PropertiesConfigurerUtil.getProperty("url");
    private static String username = PropertiesConfigurerUtil.getProperty("userName");
    private static String password = PropertiesConfigurerUtil.getProperty("passWord");
    private static SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static String shcemaOwner = PropertiesConfigurerUtil.getProperty("shcema-owner-name");
    public static String doc = PropertiesConfigurerUtil.getProperty("doc");

    private JdbcUtil() {
    }

    public static Connection getConn() throws SQLException {
//        return DBConnectionPool.getInstance().getConnection();
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
             conn =  DriverManager.getConnection(url, username, password);
            changeSchemaOwnerOracle(conn,shcemaOwner);
        } catch (Exception var1) {
            var1.printStackTrace();
        }

        return conn;
    }

    private static void changeSchemaOwnerOracle(Connection connection, String schemaowner) throws Exception {
        Statement st = connection.createStatement();
        String sql = "alter session set current_schema = " + schemaowner;
        st.execute(sql);
        st.close();
    }


    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException var2) {
                var2.printStackTrace();
            }
        }

    }

    public static List<Map<String, String>> findNullToEmpty(Object sql, String[] columns) throws Exception {
        if (sql != null && columns != null && columns.length != 0) {
            String executeSql = null;
            if (sql instanceof String) {
                executeSql = (String)sql;
            } else {
                if (!(sql instanceof StringBuffer)) {
                    return null;
                }

                executeSql = ((StringBuffer)sql).toString();
            }

            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;
            ArrayList rows = new ArrayList();

            try {
                con = getConn();
                stmt = con.createStatement();
                stmt.setQueryTimeout(10);
                rs = stmt.executeQuery(executeSql);
                while(rs.next()) {
                    Map<String, String> row = new HashMap();
                    String[] var11 = columns;
                    int var10 = columns.length;

                    for(int var9 = 0; var9 < var10; ++var9) {
                        String col = var11[var9];
                        row.put(col, rs.getString(col)!=null ? rs.getString(col) : "");
                    }

                    rows.add(row);
                }
            } catch (Exception var20) {
                var20.printStackTrace();
            } finally {
                try {
                    close(stmt, con, rs);
                } catch (SQLException var19) {
                    var19.printStackTrace();
                }

            }

            return  rows;
        } else {
            return null;
        }
    }

    public static Map<String, String> findOne(Object sql, String[] columns) throws Exception {
        if (sql != null && columns != null && columns.length != 0) {
            String executeSql = null;
            if (sql instanceof String) {
                executeSql = (String)sql;
            } else {
                if (!(sql instanceof StringBuffer)) {
                    return null;
                }

                executeSql = ((StringBuffer)sql).toString();
            }

            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;
            HashMap data = new HashMap();

            try {
                con = getConn();
                stmt = con.createStatement();
                stmt.setQueryTimeout(10);
                rs = stmt.executeQuery(executeSql);

                while(rs.next()) {
                    String[] var10 = columns;
                    int var9 = columns.length;

                    for(int var8 = 0; var8 < var9; ++var8) {
                        String col = var10[var8];
                        data.put(col, rs.getString(col));
                    }
                }
            } catch (Exception var19) {
                var19.printStackTrace();
            } finally {
                try {
                    close(stmt, con, rs);
                } catch (SQLException var18) {
                    var18.printStackTrace();
                }

            }

            return data.isEmpty() ? null : data;
        } else {
            return null;
        }
    }

    public static boolean execute(Object sql) throws Exception {
        if (sql == null) {
            return false;
        } else {
            String executeSql = null;
            if (sql instanceof String) {
                executeSql = (String)sql;
            } else {
                if (!(sql instanceof StringBuffer)) {
                    return false;
                }

                executeSql = ((StringBuffer)sql).toString();
            }

            Connection con = null;
            Statement stmt = null;
            boolean result = true;

            try {
                con = getConn();
                stmt = con.createStatement();
                stmt.setQueryTimeout(10);
                result = stmt.execute(executeSql);
            } catch (Exception var14) {
                var14.printStackTrace();
            } finally {
                try {
                    close(stmt, con, (ResultSet)null);
                } catch (SQLException var13) {
                    var13.printStackTrace();
                }

            }

            return result;
        }
    }

    public static List<Map<String, String>> find(Object sql, String[] columns) throws Exception {
        if (sql != null && columns != null && columns.length != 0) {
            String executeSql = null;
            if (sql instanceof String) {
                executeSql = (String)sql;
            } else {
                if (!(sql instanceof StringBuffer)) {
                    return null;
                }

                executeSql = ((StringBuffer)sql).toString();
            }

            Statement stmt = null;
            Connection con = null;
            ResultSet rs = null;
            ArrayList rows = new ArrayList();

            try {
                con = getConn();
                stmt = con.createStatement();
                stmt.setQueryTimeout(10);
                rs = stmt.executeQuery(executeSql);

                while(rs.next()) {
                    Map<String, String> row = new HashMap();
                    String[] var11 = columns;
                    int var10 = columns.length;

                    for(int var9 = 0; var9 < var10; ++var9) {
                        String col = var11[var9];
                        row.put(col, rs.getString(col));
                    }

                    rows.add(row);
                }
            } catch (Exception var20) {
                var20.printStackTrace();
            } finally {
                try {
                    close(stmt, con, rs);
                } catch (SQLException var19) {
                    var19.printStackTrace();
                }

            }

            return rows.isEmpty() ? null : rows;
        } else {
            return null;
        }
    }

    public static String insertSentence(Map<String, String> obj, String tableName) {
        if (obj == null) {
            return null;
        } else {
            Set<String> keys = obj.keySet();
            StringBuffer buffer = new StringBuffer(" insert into " + tableName);
            int count = 0;

            String key;
            Iterator var6;
            for(var6 = keys.iterator(); var6.hasNext(); ++count) {
                key = (String)var6.next();
                if (count == 0) {
                    buffer.append(" (" + key);
                } else {
                    buffer.append("," + key);
                }
            }

            buffer.append(") values ");
            count = 0;

            for(var6 = keys.iterator(); var6.hasNext(); ++count) {
                key = (String)var6.next();
                if (count == 0) {
                    buffer.append("(" + getParam(obj.get(key)));
                } else {
                    buffer.append("," + getParam(obj.get(key)));
                }
            }

            buffer.append(")");
             count = 0;
            return buffer.toString();
        }
    }

    public static void executeBatch(List<String> sqls) throws RemoteException, SQLException {
        Connection con = getConn();
        Statement stmt = null;

        try {
            if (sqls == null || sqls.isEmpty()) {
                throw new Exception("JDBCHelpException: SQL集合 不能为空！ DateTime: " + datetime.format(new Date()));
            }

            con.setAutoCommit(false);
            stmt = con.createStatement();
            stmt.setQueryTimeout(10);
            Iterator var4 = sqls.iterator();

            while(var4.hasNext()) {
                String sql = (String)var4.next();
                stmt.executeUpdate(sql);
            }

            con.commit();
            con.setAutoCommit(true);
        } catch (Exception var10) {
            try {
                con.rollback();
            } catch (SQLException var9) {
                var9.printStackTrace();
            }
        } finally {
            close(stmt, con, (ResultSet)null);
        }

    }

    public static String updateSentence(Map<String, String> obj, String tableName, String where) {
        if (obj == null) {
            return null;
        } else {
            Set<String> keys = obj.keySet();
            StringBuffer buffer = new StringBuffer(" update " + tableName + " set ");
            int count = 0;

            for(Iterator var7 = keys.iterator(); var7.hasNext(); ++count) {
                String key = (String)var7.next();
                if (count == 0) {
                    buffer.append(key + " = " + getParam(obj.get(key)) + " ");
                } else {
                    buffer.append("," + key + " = " + getParam(obj.get(key)) + " ");
                }
            }

             count = 0;
            if (StringUtils.hasText(where)) {
                buffer.append(where);
            }

            return buffer.toString();
        }
    }

    private static String getParam(Object params) {
        if (params == null) {
            return "null";
        } else if (params instanceof Integer) {
            return params.toString();
        } else if (params instanceof String) {
            String str = params.toString();
            if ("null".equalsIgnoreCase(str)) {
                return "null";
            } else if (str.indexOf("isSentence:") >= 0) {
                return params.toString().substring(str.indexOf(":") + 1);
            } else {
                return str.toUpperCase().indexOf(".NEXTVAL") >= 0 ? params.toString() : "'" + params.toString() + "'";
            }
        } else {
            return params instanceof Double ? params.toString() : null;
        }
    }

    private static void close(Statement stmt, Connection con, ResultSet rs) throws SQLException {
        if (stmt != null) {
            stmt.close();
            stmt = null;
        }

        if (con != null) {
            con.close();
            con = null;
        }

        if (rs != null) {
            rs.close();
            rs = null;
        }

    }
}
