import os.path

import pandas as pd

if __name__ == '__main__':
    read_file = r'C:\Users\wzl\Desktop\中广核104测点\12CZYC1.csv'
    all_df = pd.read_csv(read_file, encoding='gbk')

    df = all_df[['遥测名称', 'Unnamed: 1', '遥测别名']]
    df.rename(columns={'Unnamed: 1': '标准化中文'}, inplace=True)
    df_2404 = pd.read_csv(r'C:\Users\wzl\Desktop\中广核104测点\2404\2404测点.csv', encoding='utf8')
    df_2405 = pd.read_csv(r'C:\Users\wzl\Desktop\中广核104测点\2405\2405测点.csv', encoding='utf8')
    df_2404 = df_2404[['遥测ID号', '标准化中文', 'en_name']]
    df_2405 = df_2405[['遥测ID号', '标准化中文', 'en_name']]

    biaozhunhua_str = """
舱内温度	机舱内温度	cabin_temperature
舱外温度	环境温度	outside_cabin_temperature
齿轮箱油池温度	齿轮箱油温	gearbox_oil_temperature
齿轮箱中速轴非驱动端轴承温度	齿轮箱中速轴轴承温度	gearboxmedium_speed_shaftbearing_temperature
发电机定子U相线圈温度	发电机绕组1温度	generator_winding1_temperature
发电机定子V相线圈温度	发电机绕组2温度	generator_winding2_temperature
发电机定子W相线圈温度	发电机绕组3温度	generator_winding3_temperature
发电机非驱动端轴承温度	发电机非驱动端轴承温度	generatornon_drive_end_bearing_temperature
发电机驱动端轴承温度	发电机驱动端轴承温度	generatordrive_end_bearing_temperature
发电机转速	发电机转速	generator_speed
机舱侧向振动(已滤波)	机舱左右振动	side_to_side_vibration_of_the_cabin
机舱角度	机舱位置	cabin_position
机舱中轴线与风向夹角	对风角度	yaw_error1
机舱轴向振动(已滤波)	机舱前后振动	front_back_vibration_of_the_cabin
主轴转速	风轮转速	rotor_speed
    """

    biaozhuan_datas = [i.strip().split('\t') for i in biaozhunhua_str.split('\n') if i.strip()]
    biaozhun_cn = {}
    for i in biaozhuan_datas:
        biaozhun_cn[i[0]] = i[1]

    df['标准化中文'] = df['标准化中文'].map(biaozhun_cn).fillna(df['标准化中文'])
    df_24port = pd.concat([df_2404, df_2405])
    df_24port.rename(columns={'遥测ID号': '遥测名称'}, inplace=True)
    biaozhun_cn_map = {}
    biaozhun_en_map = {}
    for k, c, e in zip(df_24port['遥测名称'], df_24port['标准化中文'], df_24port['en_name']):
        biaozhun_cn_map[k] = c
        biaozhun_en_map[c] = e

    df['标准化中文'] = df['遥测名称'].map(biaozhun_cn_map).fillna(df['标准化中文'])
    df['en_name'] = df['标准化中文'].map(biaozhun_en_map)
    df.dropna(subset='标准化中文', inplace=True)
    df.dropna(subset='遥测别名', inplace=True)
    df['场站'] = df['遥测别名'].apply(lambda x: x.split('.')[0])
    df['风机号'] = df['遥测别名'].apply(lambda x: x.split('.')[2])
    df['测点'] = df['遥测别名'].apply(lambda x: '.'.join(x.split('.')[3:]))

    changzhan_map = {
        'FFFD': '富风',
        'MJLFD': '马家梁',
        'PLPFD': '坡底',
        'SLRY': '石楼',
        'SSLF20219': '贺家沟',
        'SXGJ': '古交',
        'SXRC': '芮城',
        'SXYX': '盂县',
        'SXYY': '右玉',
        'YCPL': '平陆',
        'YNJS': '阳曲',
        'YQXP': '西潘'
    }

    port_map = {
        '富风': 2405,
        '马家梁': 2405,
        '坡底': 2405,
        '石楼': 2404,
        '贺家沟': 2405,
        '古交': 2404,
        '芮城': 2404,
        '盂县': 2404,
        '右玉': 2404,
        '平陆': 2404,
        '阳曲': 2405,
        '西潘': 2405
    }

    df['场站'] = df['场站'].map(changzhan_map)
    df['端口'] = df['场站'].map(port_map)

    df = df[~((df['场站'] == '马家梁') &
              (df['遥测别名'].str.contains('WNAC.TemNacelleCab'))
              & (df['风机号'].isin(['WTG' + str(i).zfill(3) for i in range(26, 50)])))]

    df = df[~((df['场站'] == '盂县') &
              (df['遥测别名'].str.contains('WNAC.WindDirectionInstant'))
              & (df['风机号'].isin(['WTG' + str(i).zfill(3) for i in range(1, 57)])))]

    min_table_path = r'C:\Users\wzl\Desktop\中广核104测点\分钟级表-字段.csv'
    min_df = pd.read_csv(min_table_path, encoding='gbk', usecols=['遥测ID', '历史采样表名', '历史采样域名'])

    min_df['遥测ID'] = min_df['遥测ID'].apply(lambda x: x.replace('遥测定义表 ', '').replace('遥测值', '').strip())
    min_df.rename(columns={'遥测ID': '遥测名称'}, inplace=True)

    df = df.merge(min_df, on='遥测名称', how='left')

    df.to_excel('最终测点配置.xlsx', index=False)

    wind_names = df['场站'].unique()

    for wind_name in wind_names:
        now_df = df[df['场站'] == wind_name]
        port = now_df['端口'].unique()[0]
        os.makedirs(str(port), exist_ok=True)
        now_df.sort_values(by=['风机号', '测点'], inplace=True)
        now_df.to_csv(os.path.join(str(port), wind_name + '.csv'))
