package com.seeyon.apps.src_ydctdatasync.utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StatementQueryUtil {

    /**
     * 执行查询语句并将结果转换为 List<Map<String, Object>>
     * @param sql 查询语句
     * @param params 查询参数
     * @return 查询结果列表
     * @throws SQLException 数据库操作异常
     */
    public static List<Map<String, Object>> queryList(String sql,Connection connection,Object ...params) throws SQLException {
        List<Map<String, Object>> resultList = new ArrayList<>();
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            // 设置参数
            for (int i = 0; params != null && i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }

            // 执行查询
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                // 获取元数据
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columnCount = metaData.getColumnCount();

                // 遍历结果集
                while (resultSet.next()) {
                    Map<String, Object> rowMap = new HashMap<>();
                    for (int i = 1; i <= columnCount; i++) {
                        String columnName = metaData.getColumnLabel(i);
                        Object columnValue = resultSet.getObject(i);
                        rowMap.put(columnName, columnValue);
                    }
                    resultList.add(rowMap);
                }
            }
        }
        return resultList;
    }
    
    /**
     * 
     * @param sql 查询语句
     * @param params 查询参数
     * @return 查询结果列表
     * @throws SQLException 数据库操作异常
     */
    public static Map<String, Object> querySingle(String sql,Connection connection,Object ...params) throws SQLException {
        Map<String,Object> resMap = new HashMap<>();
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            // 设置参数
            for (int i = 0; params != null && i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }

            // 执行查询
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                // 获取元数据
                ResultSetMetaData metaData = resultSet.getMetaData();
                // 填充结果
                if(!resultSet.next()) {
                	return resMap;
                }
                String columnName = metaData.getColumnLabel(0);
                Object columnValue = resultSet.getObject(0);
                resMap.put(columnName, columnValue);
            }
        }
        return resMap;
    }
    
    /**
     * 
     * @param sql 查询语句
     * @param params 查询参数
     * @return 查询结果条数
     * @throws SQLException 数据库操作异常
     */
    public static long queryCount(String sql, Connection connection,Object... params) throws SQLException {
        long count = 0;
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            // 设置参数
            for (int i = 0; params != null && i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }

            // 执行查询
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    count = resultSet.getLong(1); // 获取第一列的值
                }
            }
        }
        return count;
    }
}
