import uuid
import os
import platform
from datetime import datetime
import base64


def getConnectionStr(connstr, index):
    str = connstr[index]
    strlist = str.split("=")
    return strlist[1].replace("'", "")


# 获取系统默认下载路径
def get_download_dir():
    if platform.system() == "Windows":
        return os.path.join(os.path.expanduser("~"), "Downloads")
    elif platform.system() == "Darwin":
        return os.path.join(os.path.expanduser("~"), "Downloads")
    elif platform.system() == "Linux":
        return os.path.join(os.path.expanduser("~"), "Downloads")
    else:
        return None

def get_temp_dir():
    return f'D:/temp'

# dict转换str 并进行拼接
def dict_to_str_with_equals(d):
    # 使用列表推导式生成键值对字符串列表
    pairs = [f"{key}='{value}'" for key, value in d.items()]
    # 使用 join 方法将列表中的字符串合并为一个字符串
    return ' '.join(pairs)

# postgis数据库连接转字符串
def sourceToDBConfig(source):
    result = {
        "mode": 2
    }
    arr = source.split(" ")
    for attr in arr:
        if attr.__contains__("srid") or attr.__contains__("sslmode"):
            continue
        if attr.__contains__("="):
            cur = attr.split("=")
            key = cur[0].replace("'", "")
            value = cur[1].replace("'", "").replace("\"", "")
            if key == "table":
                result[key] = value.split(".")[1]
                result["schema"] = value.split(".")[0]
            else:
                result[key] = value
    return result

# 获取UUID
def getUUID():
    id = uuid.uuid4().__str__()
    id = id.replace("-", "")
    return id

# 获取当前时间
def getNow():
    # 获取当前时间
    current_time = datetime.now()
    # 将当前时间转换为 'YYYY-MM-DD HH:MM:SS' 格式
    formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S')
    return formatted_time

# base64加密
def base64str(string):
    # 将字符串编码为字节
    string_bytes = string.encode('utf-8')
    # 使用 base64 编码
    base64_bytes = base64.b64encode(string_bytes)
    # 将编码后的字节转换为字符串
    base64_string = base64_bytes.decode('utf-8')
    # 输出结果
    return base64_string

#base64解密
def enbase64str(base64_string):
    # 将 Base64 字符串转换为字节
    base64_bytes = base64_string.encode('utf-8')
    # 使用 base64 解码
    decoded_bytes = base64.b64decode(base64_bytes)
    # 将字节解码为原始字符串
    decoded_string = decoded_bytes.decode('utf-8')
    return decoded_string