package easydo.technology.util;

import com.baomidou.dynamic.datasource.annotation.DS;
import easydo.technology.utils.ResultSetUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OracleClient {
    @Resource
    DataSource dataSource;

    /**
     * 处理连接
     *
     * @param connection
     * @param statements
     */
    public void finallyExecute(Connection connection, Statement... statements) {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (statements.length > 0) {
            for (Statement statement : statements) {
                try {
                    statement.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void finallyExecute(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 单表分页查询,含实体类
     *
     * @param map
     * @param clazz
     * @param <T>
     * @return
     * @throws Exception
     */
    public <T> Map<String, Object> getJdbcPage(Map<String, Object> map, Class<T> clazz, Connection... connections) throws Exception {
        String tableName = OracleResultSetUtil.humpToLine(clazz.getSimpleName());
        Map<String, Object> result = new HashMap<>();
        Connection connection = null;
        Statement statement = null;
        String sql;
        try {
            if (connections.length > 0) {
                connection = connections[0];
            } else {
                connection = dataSource.getConnection();
            }
            statement = connection.createStatement();

            sql = OracleResultSetUtil.formatPageSqlCondition(map, tableName);
            ResultSet resultSet = statement.executeQuery(sql);
            List<T> resultList = OracleResultSetUtil.formatResultSetToList(resultSet, clazz);

            String condition = OracleResultSetUtil.formatSqlCondition(map, false);
            sql = "select count(1) from " + tableName + condition;
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                result.put("total", resultSet.getInt(1));
            }
            result.put("records", resultList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (connections.length == 0) {
                finallyExecute(connection, statement);
            } else {
                finallyExecute(statement);
            }
        }
    }




}
