package com.search.supersearch.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.sql.*;
import java.util.*;

/**
 * @author 喜欢番茄火锅
 * @date 2023/11/21 16:37
 */
public class Demo {
    final static String JDBC_URL = "jdbc:postgresql://121.204.249.37:5432/postgres?currentSchema=public", USER = "postgres", PASSWORD = "Platomix@pgsql123", SCHEMA = "dws_7tzly_test";

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put("user", USER);
        props.put("password", PASSWORD);
        props.put("loginTimeout", 10000);
        props.put("connectTimeout", 10000);
        props.put("socketTimeout", 10000);
        props.put("remarks", true);
        Connection connection = null;
        connection = DriverManager.getConnection(JDBC_URL, props);
        DatabaseMetaData metaData = connection.getMetaData();

        ResultSet rs = metaData.getColumns(null, SCHEMA, "tz_pojjxmlxgdtmst_i", "%");
        HashMap<String, String> map = new HashMap<>();

        while (rs.next()) {
            map.put(rs.getString("COLUMN_NAME"), rs.getString("REMARKS"));
        }
        ResultSet rs2 = metaData.getTables(null, SCHEMA, "tz_pojjxmlxgdtmst_i", new String[]{"TABLE", "VIEW"});

        while (rs2.next()) {
            System.out.println(rs2.getString("REMARKS"));        }
//        System.out.println(map);
        // 偏移量初始化为0
        int pageNum = 0;
        // 每页大小初始化为500
        int size = 1;
        Statement statement = connection.createStatement();
        String sc = "set search_path to dws_7tzly_test;";
        statement.execute(sc);
        List<Map<String, String>> list = new ArrayList<>();
        while (true) {
            String sql = "SELECT * from tz_pojjxmlxgdtmst_i  limit " + size + " offset " + pageNum + ";";
            //  Statement 对象的 executeQuery(sql)
            ResultSet resultSet = statement.executeQuery(sql);

            convertList(list, resultSet);
            if (list.size() == 0) {
                break;
            }

            // (2)判断userList的大小是否小于每页大小，如果小于则跳出循环
            if (list.size() < size) {
                break;
            }

            pageNum++;
            break;
        }

        for (int i = 0; i < list.size(); i++) {
            Map<String, String> jmDatum = list.get(i);

            ArrayList<Map> objects = constructDetailedData(map, jmDatum);

        }
//        System.out.println(JSONUtil.toJsonStr(sourceCloumn));
//        System.out.println("=========");
        rs.close();
    }

    private static ArrayList<Map> constructDetailedData(HashMap<String, String> sourceCloumn, Map<String, String> jmDatum) {
        ArrayList<Map> objects = Lists.newArrayList();

        for (Object key : jmDatum.keySet()) {


            HashMap<@Nullable Object, @Nullable Object> map = Maps.newHashMap();
            map.put("col", key);
            map.put("commmit", sourceCloumn.get(key));
            map.put("value", jmDatum.get(key));
            objects.add(map);
        }
        return objects;
    }

    private static List<Map<String, String>> convertList(List list, ResultSet rs) throws SQLException {
        ResultSetMetaData md = rs.getMetaData();//获取键名
        int columnCount = md.getColumnCount();//获取行的数量
        while (rs.next()) {
            Map<String, String> rowData = new HashMap();//声明Map
            for (int i = 1; i <= columnCount; i++) {
                if ("pro_address".equalsIgnoreCase(md.getColumnName(i))) {
                    continue;
                }
                rowData.put(md.getColumnName(i), rs.getObject(i) + "");//获取键名及值
            }
            list.add(rowData);
        }
        return list;
    }
}
