package com.tofly.znb.common.util;

import cn.hutool.extra.pinyin.PinyinUtil;
import org.apache.http.client.utils.DateUtils;
import org.apache.poi.ss.formula.functions.T;
import org.json.JSONObject;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class ToolsUtils {

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


    public static Map<String, Object> toMap(JSONObject object) {
        Map<String, Object> map = new HashMap<String, Object>();
        object.keySet().forEach(key -> {
            String value = object.get(key).toString();
            // 如果值是JSONArray或JSONObject，递归转换
            if (value.startsWith("{")) {
                map.put(key, toMap(new JSONObject(value)));
            } else if (value.startsWith("[")) {
                // 如果是数组，可以进一步处理或转换为List等
            } else {
                map.put(key, value);
            }
        });
        return map;
    }


    public static Map<String, Object> parseJSON2Map(String jsonStr){
        Map<String, Object> map = new HashMap<String, Object>();
        com.alibaba.fastjson.JSONObject json = com.alibaba.fastjson.JSONObject.parseObject(jsonStr);
        for(Object k : json.keySet()){
            Object v = json.get(k);
            if(v instanceof com.alibaba.fastjson.JSONArray){
                List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
                Iterator<Object> it = ((com.alibaba.fastjson.JSONArray)v).iterator();
                while(it.hasNext()){
                    JSONObject json2 = (JSONObject)it.next();
                    list.add(parseJSON2Map(json2.toString()));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }



    /**
     *  转为大写guid
     */
    public static String getGuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "").toUpperCase();
    }

    /**
     * 转为大写Lsbh
     */
    public static String getLsbh() {
        String lsbh = DateUtils.formatDate(new Date(), "yyyyMMddHHmmssssss");
        return lsbh;
    }

    public  static  String GetPjh(long id){

     return id+"-"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }

    public static String getFirstLetters(String input) {
        StringBuilder result = new StringBuilder();
        for (char c : input.toCharArray()) {
            //String[] pinyinArray;
                 String s= PinyinUtil.getPinyin(c);
           // if (pinyinArray != null && pinyinArray.length > 0) {
                // 取拼音的首字母并转为大写
                result.append(Character.toUpperCase(s.charAt(0)));
            //}
        }
        return result.toString();
    }

}
