import datetime

import unitls.baseFunction as baseFunction
from unitls.LogerinTxt import app_logger
from .flightDB import flightDB


def checkTFUairplane(database:flightDB):
    try:
        code = {"接机已到位提示","已放行提示","二送已到位提示"}
        code2num = {"接机已到位提示":8,"已放行提示":9,"二送已到位提示":12}
        code2 = {"接机已到位提示", "已放行提示"}
        code22num = {"接机已到位提示":8, "已放行提示":9}
        #需要建立昨日初始化
        nowDay = datetime.datetime.now().strftime("%Y%m%d")
        nowDay_1 = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
        now = datetime.datetime.now()
        now11=str(now).split(".")[0]
        day = [nowDay, nowDay_1]
        for nowDayStr in day:
            AFinsky_arr = []
            AFongroud_arr = []
            TRinsky_arr = []
            TRongroud_arr = []
            THQinsky_arr = []
            THQongroud_arr = []
            APongroud_arr = []
            flightsts_flight_ids_arr=[]
            AFinsky=database.queryTabel('TaskFlightinfo{}'.format(nowDayStr),'航班编号',"航班类型='AF' and 非航前预计到达!='' and 非航前实际到达=''and 上段起飞时间!=''")
            AFongroud=database.queryTabel('TaskFlightinfo{}'.format(nowDayStr),'航班编号',"航班类型='AF' and 非航前实际到达!=''")
            TRinsky=database.queryTabel('TaskFlightinfo{}'.format(nowDayStr),'航班编号',"航班类型='TR' and 非航前预计到达!='' and 非航前实际到达='' and 上段起飞时间!=''")
            TRongroud = database.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '航班编号', "航班类型='TR' and 非航前实际到达!='' and 实际起飞 ='' ")
            THQinsky = database.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '航班编号',"航班类型='TAF' and 非航前预计到达!='' and 非航前实际到达=''and 上段起飞时间!=''")
            THQongroud=database.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '航班编号', "航班类型='TAF'  and 非航前实际到达!='' and 实际起飞 =''")
            APongroud=database.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '航班编号', "航班类型='AP' and 实际起飞 =''")


            ##################################
            #缺个未显示的航班筛选
            if len(AFinsky) != 0:
                for i in AFinsky:
                    AFinsky_arr.append(i[0])
            if len(AFongroud) != 0:
                for i in AFongroud:
                    AFongroud_arr.append(i[0])
            if len(TRinsky) != 0:
                for i in TRinsky:
                    TRinsky_arr.append(i[0])
            if len(TRongroud) != 0:
                for i in TRongroud:
                    TRongroud_arr.append(i[0])
            if len(THQinsky) != 0:
                for i in THQinsky:
                    THQinsky_arr.append(i[0])
            if len(THQongroud) != 0:
                for i in THQongroud:
                    THQongroud_arr.append(i[0])
            if len(APongroud) != 0:
                for i in APongroud:
                    APongroud_arr.append(i[0])
            flightsts_flight_ids=database.queryTabel('flightsts{}'.format(nowDayStr),'*',"航班编号 != ''")
            flightinfo_flight_ids = database.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '*', "航班编号 != ''")
            if len(flightsts_flight_ids) != 0:
                for flightstskey in flightsts_flight_ids:
                    newdic={"天府飞机":"''"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%flightstskey[0])
                    if flightstskey[0] not in flightinfo_flight_ids:
                        flightsts_flight_ids_arr.append(flightstskey[0])
                    else:
                        database.deleteSingledata('flightsts{}'.format(nowDayStr),"航班编号='%s'"%flightstskey[0])

            for AFinsky_arr_key in AFinsky_arr:
                if AFinsky_arr_key in flightsts_flight_ids_arr:
                    newdic = {"天府飞机":"'空中航后'"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%AFinsky_arr_key)
                else:
                    newdic={"航班编号":"%s"%AFinsky_arr_key,"接机到位":"","放行":"","销子夹板":"","随机到位":"","二送到位":"","接机已到位提示":"","已放行提示":"","销子夹板已提示":"","随机已到位提示":"","二送已到位提示":"","天府飞机":'空中航后',"机号":"","机位":""}
                    database.lazyInsertData('flightsts{}'.format(nowDayStr),newdic)
            for AFongroud_arr_key in AFongroud_arr:
                if AFongroud_arr_key in flightsts_flight_ids_arr:
                    newdic = {"天府飞机": "'地面航后'"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%AFongroud_arr_key)
                else:
                    newdic = {"航班编号": "%s" % AFongroud_arr_key, "接机到位": "", "放行": "", "销子夹板": "", "随机到位": "", "二送到位": "",
                              "接机已到位提示": "", "已放行提示": "", "销子夹板已提示": "", "随机已到位提示": "", "二送已到位提示": "", "天府飞机": '地面航后',"机号":"","机位":""}
                    database.lazyInsertData('flightsts{}'.format(nowDayStr),newdic)

            for TRinsky_arr_key in TRinsky_arr:
                if TRinsky_arr_key in flightsts_flight_ids_arr:
                    newdic = {"天府飞机": "'空中短停'"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%TRinsky_arr_key)
                else:
                    newdic = {"航班编号": "%s" % TRinsky_arr_key, "接机到位": "", "放行": "", "销子夹板": "", "随机到位": "", "二送到位": "",
                              "接机已到位提示": "", "已放行提示": "", "销子夹板已提示": "", "随机已到位提示": "", "二送已到位提示": "", "天府飞机": '空中短停',"机号":"","机位":""}
                    database.lazyInsertData('flightsts{}'.format(nowDayStr),newdic)

            for TRongroud_arr_key in TRongroud_arr:
                if TRongroud_arr_key in flightsts_flight_ids_arr:
                    newdic = {"天府飞机": "'地面短停'"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%TRongroud_arr_key)
                else:
                    newdic = {"航班编号": "%s" % TRongroud_arr_key, "接机到位": "", "放行": "", "销子夹板": "", "随机到位": "", "二送到位": "",
                              "接机已到位提示": "", "已放行提示": "", "销子夹板已提示": "", "随机已到位提示": "", "二送已到位提示": "", "天府飞机": '地面短停',"机号":"","机位":""}
                    database.lazyInsertData('flightsts{}'.format(nowDayStr),newdic)

            for THQinsky_arr_key in THQinsky_arr:
                if THQinsky_arr_key in flightsts_flight_ids_arr:
                    newdic = {"天府飞机": "'空中特后前'"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%THQinsky_arr_key)
                else:
                    newdic = {"航班编号": "%s" % THQinsky_arr_key, "接机到位": "", "放行": "", "销子夹板": "", "随机到位": "", "二送到位": "",
                              "接机已到位提示": "", "已放行提示": "", "销子夹板已提示": "", "随机已到位提示": "", "二送已到位提示": "", "天府飞机": '空中特后前',"机号":"","机位":""}
                    database.lazyInsertData('flightsts{}'.format(nowDayStr),newdic)

            for THQongroud_arr_key in THQongroud_arr:
                if THQongroud_arr_key in flightsts_flight_ids_arr:
                    newdic = {"天府飞机": "'地面特后前'"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%THQongroud_arr_key)
                else:
                    newdic = {"航班编号": "%s" % THQongroud_arr_key, "接机到位": "", "放行": "", "销子夹板": "", "随机到位": "", "二送到位": "",
                              "接机已到位提示": "", "已放行提示": "", "销子夹板已提示": "", "随机已到位提示": "", "二送已到位提示": "", "天府飞机": '地面特后前',"机号":"","机位":""}
                    database.lazyInsertData('flightsts{}'.format(nowDayStr),newdic)

            for APongroud_arr_key in APongroud_arr:
                #print(APongroud_arr_key)
                if APongroud_arr_key in flightsts_flight_ids_arr:
                    newdic = {"天府飞机":"'地面航前'"}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr),newdic,"航班编号='%s'"%APongroud_arr_key)
                else:
                    newdic = {"航班编号": "%s" % APongroud_arr_key, "接机到位": "", "放行": "", "销子夹板": "", "随机到位": "", "二送到位": "",
                              "接机已到位提示": "", "已放行提示": "", "销子夹板已提示": "", "随机已到位提示": "", "二送已到位提示": "", "天府飞机": '地面航前',"机号":"","机位":""}
                    database.lazyInsertData('flightsts{}'.format(nowDayStr),newdic)

            for flightstskey in flightinfo_flight_ids:
                acno_str = baseFunction.TuplefindInList3(flightinfo_flight_ids , flightstskey[0 ] , 0 , 1)
                if baseFunction.TuplefindInList3(flightinfo_flight_ids, flightstskey[0 ], 0, 3) != "AP":
                    bay_str = baseFunction.TuplefindInList3(flightinfo_flight_ids , flightstskey[0 ] , 0 , 11)
                else:
                    bay_str = baseFunction.TuplefindInList3(flightinfo_flight_ids , flightstskey[0 ] , 0 , 10)
                if bay_str != baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 2) or acno_str != baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 1):
                    newdic = {"机位":"'%s'"%bay_str, "机号":"'%s'"%acno_str}
                    database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                worksts_str= baseFunction.TuplefindInList3(flightinfo_flight_ids , flightstskey[0 ] , 0 , 14)
                worksts = int(worksts_str) if worksts_str != "" else 0
                a= baseFunction.TuplefindInList3(flightsts_flight_ids , flightstskey[0 ] , 0 , 2) if baseFunction.TuplefindInList3(flightsts_flight_ids , flightstskey[0 ] , 0 , 2) != "" else "未显示机号"

                if worksts >= 7 and str(flightstskey[0]) in str(flightsts_flight_ids_arr):
                    if baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 3) != "已到位" or baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 4) != "已放行":
                        newdic={"接机到位":"'已到位'", "放行": "'已放行'"}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                    for i in code2:
                        if "1" not in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, code22num[i ]):
                            newdic={"航班编号":"%s"% flightstskey[0],"警告信息":"","信息状态":"","提示信息":"AMRO:%s%s!%s"%(a,i,now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                            database.lazyInsertData('logs', newdic)
                            newdic = {"%s"%i: 1}
                            database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                elif worksts >= 4 and str(flightstskey[0]) in str(flightsts_flight_ids_arr):
                    if baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 3) != "已到位" :
                        newdic = {"接机到位": "'已到位'"}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                    if "1" not in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 8):
                        newdic={"航班编号":"%s"% flightstskey[0],"警告信息":"","信息状态":"","提示信息":"AMRO:%s接机已到位提示!%s"%(a,now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                        database.lazyInsertData('logs', newdic)
                        newdic = {"接机已到位提示": 1}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'"% flightstskey[0])
                    if "1" in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 9) and "人工" not in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 4):
                        newdic = {"放行": "''"}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                        newdic={"航班编号":"%s"% flightstskey[0],"警告信息":"","信息状态":"","提示信息":"AMRO:%s放行退回至到位!%s"%(a,now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                        database.lazyInsertData('logs', newdic)
                        newdic = {"已放行提示": 0}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'"% flightstskey[0])
                elif  str(flightstskey[0]) in str(flightsts_flight_ids_arr):
                    if "1" in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 8) and "人工" not in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 3):
                        newdic = {"接机到位": "''"}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                        newdic={"航班编号":"%s"% flightstskey[0],"警告信息":"","信息状态":"","提示信息":"AMRO:%s状态退回至初始状态!%s"%(a,now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                        database.lazyInsertData('logs', newdic)
                        newdic = {"接机已到位提示": 0}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'"% flightstskey[0])
                    if "1" in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 9) and "人工" not in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 4):
                        newdic = {"放行": "''"}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                        newdic={"航班编号":"%s"% flightstskey[0],"警告信息":"","信息状态":"","提示信息":"AMRO:%s状态退回至初始状态!%s"%(a,now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                        database.lazyInsertData('logs', newdic)
                        newdic = {"已放行提示": 0}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'"% flightstskey[0])

                ECSJ_worksts_str = baseFunction.TuplefindInList3(flightinfo_flight_ids , flightstskey[0 ] , 0 , 20)
                ECSJ_worksts_STR = ECSJ_worksts_str if len(ECSJ_worksts_str) != 0 else 0
                ECSJ_worksts = int(ECSJ_worksts_STR) if ECSJ_worksts_STR != "" else 0
                if ECSJ_worksts >= 15 and str(flightstskey[0]) in str(flightsts_flight_ids_arr):
                    if baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 7) != "已到位" :
                        #newdic = {"接机到位": "'已到位'", "放行": "'已放行'", "二送到位": "'已到位'"}
                        newdic = {"二送到位": "'已到位'"}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                    if"1" not in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 12):
                        newdic={"航班编号":"%s"% flightstskey[0],"警告信息":"","信息状态":"","提示信息":"AMRO:%s二送已到位提示!%s"%(a,now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                        database.lazyInsertData('logs', newdic)
                        newdic = {"二送已到位提示": 1}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                elif str(flightstskey[0]) in str(flightsts_flight_ids_arr) and "人工" not in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 7):
                    if baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 7) != "" :
                        #newdic = {"接机到位": "'已到位'", "放行": "'已放行'", "二送到位": "'已到位'"}
                        newdic = {"二送到位": "''"}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
                    if "1" in baseFunction.TuplefindInList3(flightsts_flight_ids, flightstskey[0 ], 0, 12):
                        newdic={"航班编号":"%s"% flightstskey[0],"警告信息":"","信息状态":"","提示信息":"AMRO:%s二送到位取消到位!%s"%(a,now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                        database.lazyInsertData('logs', newdic)
                        newdic = {"二送已到位提示": 0}
                        database.lazyUpdateItem('flightsts{}'.format(nowDayStr), newdic, "航班编号='%s'" % flightstskey[0])
        database.FunctionCommit()

    except Exception as e:
        app_logger.log_error(e)
        database.FunctionCommit()