package cc.smtweb.biz.mztb.web.tool;

import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.jdbc.IdGenerator;
import cc.smtweb.framework.core.exception.BizException;
import com.zaxxer.hikari.util.DriverDataSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.sql.*;
import java.util.List;
import java.util.Properties;

public class DbUtil {
    protected String jdbcUrl;
    protected String username;
    protected String password;
    public Connection connection;
    protected static DbUtil instance;


    public DbUtil(String jdbcUrl,String username,String password) {
        this.jdbcUrl = jdbcUrl;
        this.username = username;
        this.password = password;
        try {
            connection = DriverManager.getConnection(jdbcUrl,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public DbUtil(Connection connection) {
        this.connection = connection;
    }

//    public static DbUtil getInstance(String jdbcUrl,String username,String password) {
//        if(instance == null) {
//            synchronized (DbUtil.class){
//                if (instance== null){
//                    instance = new DbUtil(jdbcUrl,password,username);
//                }
//            }
//        }
//        return instance;
//    }
//    public static DbUtil getInstance(Connection connection) {
//        if(instance == null) {
//            synchronized (DbUtil.class){
//                if (instance== null){
//                    instance = new DbUtil(connection);
//                }
//            }
//        }
//        return instance;
//    }


    public boolean insert(String sql) throws SQLException {
        Statement statement = getStatement();
        boolean execute = statement.execute(sql);
        connection.close();
        return execute;
    }



    public ResultSet dbHandSql( String sql, String handleType)
            throws SQLException {
        Statement stmt = getStatement();
        switch (handleType) {
            case "query" :return stmt.executeQuery(sql);
        }
        return stmt.executeQuery(sql);
    }
    public int update(String sql) throws SQLException {
        int i = getStatement().executeUpdate(sql);
        connection.close();
        return i;
    }

    /**
     * 获取statement 对象
     * @return statement
     * @throws SQLException sql执行异常
     */
    public Statement getStatement() throws SQLException {
        Statement stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_READ_ONLY);
        stmt.setFetchSize(Integer.MIN_VALUE);
        stmt.setQueryTimeout(60);
        return stmt;
    }



//
//    public DbEngine getDbEngine(String dbType) {
//        if(dbEngine != null) {
//            return dbEngine;
//        }
//        String driverClassname = getDriverClassNameWithDbType(dbType);
//        DataSource dataSource = new DriverDataSource(jdbcUrl,driverClassname,new Properties(),username,password);
////        IdGenerator idGenerator = new IdGenerator(12);
//        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
//        dbEngine = new DbEngine(namedParameterJdbcTemplate,idGenerator,dbType);
//        return dbEngine;
//    }
//
//    private String getDriverClassNameWithDbType(String type) {
//       switch (type) {
//           case "mysql" : return  "com.mysql.cj.jdbc.Driver";
//           case "oracle" : return "com.mysql.cj.jdbc";
//           default: return "";
//       }
//    }
    public boolean batchInsert(List<String> insertSql){
        if (connection == null){
            throw new BizException("获取连接失败！");
        }
        try {
            connection.setAutoCommit(false);
            try (Statement statement = connection.createStatement()) {
                for (int i = 0; i <insertSql.size(); i++) {
                    statement.addBatch(insertSql.get(i)); // 添加到批处理中
                }
                int[] updateCounts = statement.executeBatch(); // 执行批处理
                if (updateCounts.length != insertSql.size()){
                    throw new BizException("数据数量异常");
                }
                connection.commit(); // 提交事务
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
                try {
                    connection.rollback(); // 回滚事务
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    return false;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                connection.setAutoCommit(true); // 重新开启自动提交
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }



}
