package com.trade.common.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import org.springframework.util.CollectionUtils;

/**
 * 封装sql执行相关方法,包括结果处理
 */
public class SqlExecutionUtil {

    /**
     * 有参数的预处理执行,返回Id的集合
     *
     * @param connection
     * @param valueList
     * @param sql
     * @return
     */
    public static List<Long> executeForIdsWithParameters(Connection connection, List<Object> valueList, String sql) {
        List<Long> idList = new ArrayList<>();
        if (CollectionUtils.isEmpty(valueList)) {
            return executeForIdsWithoutParameters(connection, sql);
        }
        try (PreparedStatement psm = connection.prepareStatement(sql)) {
            for (int i = 0; i < valueList.size(); i++) {
                psm.setObject(i + 1, valueList.get(i));
            }
            ResultSet resultSet = psm.executeQuery();
            while (resultSet.next()) {
                idList.add(resultSet.getLong(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return idList;
    }

    /**
     * 返回Id的集合 无参数 预处理执行
     *
     * @param connection
     * @param sql
     * @return
     */
    public static List<Long> executeForIdsWithoutParameters(Connection connection, String sql) {
        List<Long> idList = new ArrayList<>();
        try (PreparedStatement psm = connection.prepareStatement(sql)) {
            ResultSet resultSet = psm.executeQuery();
            while (resultSet.next()) {
                idList.add(resultSet.getLong(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return idList;
    }

    /**
     * 不需要返回结果 无参数 预处理执行
     *
     * @param connection
     * @param sql
     */
    public static void executeForNoResultWithoutParameters(Connection connection, String sql) {
        try (PreparedStatement psm = connection.prepareStatement(sql)) {
            psm.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 不需要返回结果 只有一个参数:参数是Id 预处理执行
     *
     * @param connection
     * @param sql
     * @param id
     */
    public static void executeForNoResultWithParameterId(Connection connection, String sql, Long id) {
        try (PreparedStatement psm = connection.prepareStatement(sql)) {
            psm.setLong(1, id);
            psm.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 不需要返回结果 有多个参数 预处理执行
     *
     * @param connection
     * @param sql
     * @param parameterList
     */
    public static void executeForNoResultWithManyParameters(Connection connection, String sql,
            List<Object> parameterList) {
        try (PreparedStatement psm = connection.prepareStatement(sql)) {
            if (CollectionUtils.isEmpty(parameterList)) {
                executeForNoResultWithoutParameters(connection, sql);
            } else {
                for (int i = 0; i < parameterList.size(); i++) {
                    psm.setObject(i + 1, parameterList.get(i));
                }
                psm.execute();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
