import pandas as pd

from service.common_connect import trans

wind_farms = {
    "WOF35900004": "平陆风电场",
    "WOF35100072": "阳曲风电场",
    "WOF35100073": "古交风电场",
    "WOF35200074": "马家梁风电场",
    "WOF35400075": "太谷风电场",
    "WOF35900076": "坡底风电场",
    "WOF35300077": "西潘风电场",
    "WOF35900078": "芮城风电场",
    "WOF34900079": "右玉风光互补电站",
    "WOF35800080": "贺家沟",
    "WOF35800081": "石楼风电场",
    "WOF35800082": "盂县风光互补电站"
}

# types = ['minute', 'second', 'warn', 'fault']

types = ['warn', 'fault']


def check_exist_table(table_name):
    sql = f"select count(1) as count from information_schema.tables where table_name= '{table_name}'"
    print(sql)
    data = trans.execute(sql)[0]['count']

    return int(data) > 0


file_dir = r'C:\Users\wzl\Desktop\中广核104测点\20250513\data'

def get_data(table_name, name, type):
    col = 'begin_time'
    if type in ['minute', 'second']:
        col = 'time_stamp'

    sql = f"select * from {table_name} where {col} > '2025-05-10 00:00:00'"
    df = pd.read_sql_query(sql, con=trans.get_engine())
    df.to_csv(f'{file_dir}\\{name}_{type}.csv', index=False, encoding='utf-8')


for wind, name in wind_farms.items():
    for now_type in types:
        table_name = f'{wind}_{now_type}'
        if check_exist_table(table_name):
            get_data(table_name, name, now_type)
