import os
import traceback
import ast
chList = ['机号', '机型', '发动机', '任务类型', '进港时间', '出港时间', '航班号', '进港起飞', '离港到达', '放行人员', '维修人员', '备注']
chList2 = ['序号', '机位', '机号', '机型', '发动机', '任务类型', '进港时间', '出港时间', '航班号', '进港起飞', '离港到达', '放行人员', '维修人员', '备注']
def getData():
    with open('flightlist1.txt','r', encoding='utf-8') as f:
        text = f.read()

        data = ast.literal_eval(text)
        #print(data)
        dic = {}
        for key in data:
            #print(data[key])
            #print("B-{}, 机位：{}".format(data[key]['ACNO'], data[key]['BAY'].replace('S', "")))
            #print("B-{}, 机位：{}, 放行：{}， 工作者：{}, 机型：{}, " .format(data[key]['ACNO'], data[key]['BAY'], data[key]['FXRY'], data[key]['WXRY'],
            #                                                  data[key]['ACTYPE']))
            if data[key]['BAY']!="":
                if len(data[key]['BAY'])>2:
                    if data[key]['BAY'][0]=="2":
                        if data[key]['BAY'][1]=="0":
                            # print("B-{}, 机位：{}, 放行：{}， 工作者：{}, 机型：{}, " .format(data[key]['ACNO'], data[key]['BAY'], data[key]['FXRY'], data[key]['WXRY'],
                            #                                                 data[key]['ACTYPE']))
                            dic["{}".format(data[key]['BAY'])] = {"bnum":"{}".format(data[key]['ACNO']),  "FX":"{}".format(data[key]['FXRY']), "WX":"{}".format(data[key]['WXRY'])}
            # for i in data[key]:
            #     print(i)
            #     print(data[key][i])
        #print(dic)
        return dic

def getData2():
    keyList = ['WXRY','TA','TD','FLIGHT_NO','ACTYPE','ARR_CH1','DEP_CH1','BAY','FXRY','ENG_TYPE','ACNO','TASKTYPE','REMARK_BUSINESS']
    tasktype = {"AP":"航前", "TR":"短停", "AF":"航后"}
    dic = {}
    with open('hbdt.txt','r', encoding='utf-8') as f:
        text = f.read()
        data = ast.literal_eval(text)
        uselly = data['data']
        for key in uselly:
            # for i in key:
            #     print(i)
            #     print(key[i])
            # print(key['ACNO'])
            # print(tasktype[key['TASKTYPE']])
            # print(key['TASKSTS'])
            try:
                bay = 'BAY'
                for kl in keyList:

                    if kl not in key:

                        if kl == bay:
                            #print(key)
                            bay = 'BAY_2'
                            if key[bay]=='':
                                bay = 'BAY_1'
                            #print(bay)
                        else:
                            key[kl] = ''
                            #print(key[kl])
                dic["{}".format(key[bay])]={'bnum':'{}'.format(key['ACNO']), 'actype':'{}'.format(key['ACTYPE']), 'engtype':'{}'.format(key['ENG_TYPE']), 'tasktype':'{}'.format(tasktype[key['TASKTYPE']]),
                                            'timein':'{}'.format(key['TA']), 'timeout':'{}'.format(key['TD']), 'ftnum':'{}'.format(key['ARR_DEP']), 'pos1':'{}'.format(key['ARR_CH1']), 'pos2':'{}'.format(key['DEP_CH1']),
                                            'FX':'{}'.format(key['FXRY']),'WX':'{}'.format(key['WXRY']), 'remark':'{}'.format(key['REMARK_BUSINESS'])}
                # print("工作者：{}, 进港时间：{}，离港时间：{}, 航班号：{}, 机型：{}, 进港起飞：{}，离港到达：{}, 机位：{}, 放行：{}, 发动机类型：{}, 机号：{}, 任务类型：{}, 备注信息：{}"
                #      .format(key['WXRY'], key['TA'], key['TD'],key['ARR_DEP'], key['ACTYPE'], key['ARR_CH1'], key['DEP_CH1'], key[bay], key['FXRY'], key['ENG_TYPE'], key['ACNO'], tasktype[key['TASKTYPE']], key['REMARK_BUSINESS']))

            except KeyError as E:
                print("错误：{}".format(E))
                print(key)
                # if str(E)=="'BAY'":
                #     print(
                #         "工作者：{}, 进港时间：{}，离港时间：{}, 航班号：{}, 机型：{}, 进港起飞：{}，离港到达：{}, 机位：{}, 放行：{}, 发动机类型：{}, 机号：{}, 任务类型：{}, 备注信息：{}"
                #         .format(key['WXRY'], key['TA'], key['TD'], key['FLIGHT_NO'], key['ACTYPE'], key['ARR_CH1'],
                #                 key['DEP_CH1'], key['BAY_2'], key['FXRY'], key['ENG_TYPE'], key['ACNO'], key['TASKTYPE'], key['REMARK_BUSINESS']))

        #print(dic)
        return dic

def getAllData():
    tasktype = {"AP": "航前", "TR": "短停", "AF": "航后"}
    keyList = ['WXRY', 'TA', 'TD', 'FLIGHT_NO', 'ACTYPE', 'ARR_CH1', 'DEP_CH1', 'BAY', 'FXRY', 'ENG_TYPE', 'ACNO',
               'TASKTYPE', 'REMARK_BUSINESS']
    with open('hbdt.txt','r', encoding='utf-8') as f:
        text = f.read()
        data = ast.literal_eval(text)
        uselly = data['data']

        dic = {}
        number = 0
        for key in uselly:
            number +=1
            bay = 'BAY'
            for kl in keyList:

                if kl not in key:

                    if kl == bay:
                        # print(key)
                        bay = 'BAY_2'
                        if key[bay] == '':
                            bay = 'BAY_1'
                        # print(bay)
                    else:
                        key[kl] = ''
                        # print(key[kl])
            dic[number] = {'bay':'{}'.format(key[bay]),'bnum':'{}'.format(key['ACNO']), 'actype':'{}'.format(key['ACTYPE']), 'engtype':'{}'.format(key['ENG_TYPE']), 'tasktype':'{}'.format(tasktype[key['TASKTYPE']]),
                                            'timein':'{}'.format(key['TA']), 'timeout':'{}'.format(key['TD']), 'ftnum':'{}'.format(key['ARR_DEP']), 'pos1':'{}'.format(key['ARR_CH1']), 'pos2':'{}'.format(key['DEP_CH1']),
                                            'FX':'{}'.format(key['FXRY']),'WX':'{}'.format(key['WXRY']), 'remark':'{}'.format(key['REMARK_BUSINESS'])}
        #print(dic)
        return dic



if __name__ == '__main__':
    dic = getData2()
    print(dic)
    getAllData()
