import pandas as pd

fengchang_map = {
    '平陆': 'WOF35900004', '阳曲': 'WOF35100072', '古交': 'WOF35100073', '马家梁': 'WOF35200074',
    '太谷': 'WOF35400075', '坡底': 'WOF35900076', '西潘': 'WOF35300077', '芮城': 'WOF35900078',
    '右玉': 'WOF34900079', '贺家沟': 'WOF35800080', '石楼': 'WOF35800081', '盂县': 'WOF35800082',
    '富风': 'WOF35400075'
}

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


def generate_sacada_conf(all_df, cedian_df):
    cedian_df['场站标准化编号'] = cedian_df['场站'].map(fengchang_map)
    cedian_df['场站'] = cedian_df['场站标准化编号'].map(wind_farms)

    cedian_df['风机号'] = cedian_df['风机号'].apply(lambda x: int(x[-3:]))

    df2404 = all_df[all_df['转发逻辑设备ID号'] == '转发到二区2404 ']
    df2405 = all_df[all_df['转发逻辑设备ID号'] == '转发到二区2405 ']

    df2404 = pd.merge(df2404, cedian_df, left_on='遥测ID号', right_on='遥测名称', how='inner')
    df2405 = pd.merge(df2405, cedian_df, left_on='遥测ID号', right_on='遥测名称', how='inner')

    del df2404['Unnamed: 0']
    del df2405['Unnamed: 0']

    ## 顺序号,场站,场站标准化编号,风机号,遥测别名,标准化中文,标准化英文

    name_dict = {
        "转发顺序号": "顺序号",
        "en_name": "标准化英文"
    }

    df2404.rename(columns=name_dict, inplace=True)
    df2405.rename(columns=name_dict, inplace=True)

    select_cols = ["顺序号", "遥测ID号", "场站", "场站标准化编号", "风机号", "遥测别名", "标准化中文", "标准化英文"]

    df2404[select_cols].to_excel('../conf/测点表-2404.xlsx', index=False)
    df2405[select_cols].to_excel('../conf/测点表-2405.xlsx', index=False)


def generate_warn_fault_conf(all_df: pd.DataFrame, warn_fault_df: pd.DataFrame):
    del warn_fault_df['顺序号']
    df2406 = all_df[all_df['转发逻辑设备ID号'] == '转发到二区2406 ']
    df2406['顺序号'] = df2406['转发顺序号']
    df2406.drop_duplicates(inplace=True)
    df2406 = pd.merge(df2406, warn_fault_df, left_on='遥测ID号', right_on='遥测名称', how='inner')
    select_cols = ["顺序号", "场站", "场站标准化编号", "风机号", "遥测名称", "遥测别名"]
    df2406[select_cols].to_excel('../conf/故障报警测点-2406.xlsx', index=False)


def generate_warn_fault_mc_version(mc_version_df):
    mc_version_df['场站标准化编号'] = mc_version_df['场站'].map(fengchang_map)
    mc_version_df['场站'] = mc_version_df['场站标准化编号'].map(wind_farms)
    mc_version_df.to_excel('../conf/主控版本-2406.xlsx', index=False)


if __name__ == '__main__':
    all_df = pd.read_excel(r'C:\Users\wzl\Desktop\fault_20240423.xlsx')
    all_df['遥测ID号'] = all_df['遥测ID号'].apply(lambda x: x.replace('遥测定义表', '').replace('遥测值', '').strip())
    all_df = all_df[['遥测ID号', '转发逻辑设备ID号', '转发顺序号']]

    # ## 配置scada 配置文件
    # cedian_df = pd.read_excel(r'C:\Users\wzl\Desktop\中广核104测点\0416部署需要\最终测点配置.xlsx')
    # generate_sacada_conf(all_df, cedian_df)

    ## 配置故障报警 配置文件
    warn_fault_df = pd.read_excel(r'D:\project\energy-online-data\conf\故障报警测点.xlsx')
    generate_warn_fault_conf(all_df, warn_fault_df)

    ## 主控版本处理
    # mc_version_df = pd.read_excel(r'D:\project\energy-online-data\conf\主控版本.xlsx')
    # generate_warn_fault_mc_version(mc_version_df)
