import concurrent.futures

from unitls.LogerinTxt import app_logger
from unitls.baseFunction import TuplefindInList , TuplefindInList11 , TuplefindInList3
from .flightDB import flightDB
from .getSortFlightdata import getSortFlightdata
from .waringMessageID import waringMessageID


def tableWidgetDispaly(fdb:flightDB,selectedtime,date):#1,昨天。2今天3明天4其他
    try:
        tableWidgetDispalydata = {}
        nowDayStr = selectedtime
        sorttable = getSortFlightdata(fdb, selectedtime, "sortFlight")
        TaskFlightinfoAlldata = fdb.getAlldata('TaskFlightinfo{}'.format(nowDayStr))
        flightStsDataAll = fdb.getAlldata('flightsts{}'.format(nowDayStr))
        peopleScheduleDataAll = fdb.getAlldata("peopleSchedule{}".format(nowDayStr))
        workjobDataAll = fdb.getAlldata('workjob')
        tasklistall = fdb.getAlldata('TaskList{}'.format(nowDayStr))
        flightSearchALl =  fdb.sort_queryTable2('*','FlightSearch{}'.format(nowDayStr),"FLIGHT_ID !=''",'ACNO','ASC','STD','ASC')
        flightplan= fdb.getAlldata("flightplaninfo{}".format(nowDayStr))
        waringMessageIDs = waringMessageID(fdb)
        executor = concurrent.futures.ThreadPoolExecutor()
        all_task = []
        for i in range(len(sorttable)):
            all_task.append(executor.submit(functionDisaplay, sorttable[i],i, TaskFlightinfoAlldata,workjobDataAll, flightStsDataAll,peopleScheduleDataAll,waringMessageIDs,tasklistall,flightSearchALl,flightplan))

        for future in concurrent.futures.as_completed(all_task):
            data = future.result()

            tableWidgetDispalydata.update(data)
        #t8 = datetime.datetime.now()
        #print(f'【航班刷新】【处理数据】:{t8 - t7}')
        newdic = {"A": '"{}"'.format(tableWidgetDispalydata)}
        if date == "1":
            fdb.upDateItem("display", newdic, "ID = '1'")
        elif date == "2":
            fdb.upDateItem("display", newdic, "ID = '2'")
        elif date == "3":
            fdb.upDateItem("display", newdic, "ID = '3'")
        #t9 = datetime.datetime.now()
        #print(f'【航班刷新】【保存数据】:{t9 - t8}')
        #print(f'【航班刷新】【总时间】:{t9 - t1}')
        return tableWidgetDispalydata
    except Exception as e:
        #dingding_alert(traceback.format_exc())
        app_logger.log_error(e)

def functionDisaplay(row,rowNum,TaskFlightinfoAlldata, workjobDataAll, flightStsDataAll,peopleScheduleDataAll, waringMessageID,TaskListAll,flightSearchALl,flightplan):
    dic=["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23",
         "24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44",
         "45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65"]
    TaskFlightinfo = TuplefindInList(TaskFlightinfoAlldata,row[1])
    infos="仅支持查询川航航班"
    dq = "--"
    dqxq = "电签工卡不适用"
    if TaskFlightinfo[21] and ('3U' in TaskFlightinfo[21] or 'CSC' in TaskFlightinfo[21]):
        singleflightsearch = TuplefindInList11(flightSearchALl, TaskFlightinfo[1],2)
        for info in singleflightsearch:
            if info[8] != "":
                time1="[实]"+info[8][-8:-3]
            elif info[7] != "":
                time1="[预]"+info[7][-8:-3]
            else:
                time1="[计]"+info[6][-8:-3]
            if info[11] !="":
                time2="[实]"+info[11][-8:-3]
            elif info[10] != "":
                time2="[预]"+info[10][-8:-3]
            else:
                time2="[计]"+info[9][-8:-3]
            mesg=info[4]+time1+"--"+time2+info[5]
            infos = mesg if infos == "仅支持查询川航航班" else infos+"\n"+mesg

        tasksts = TuplefindInList3(TaskListAll, TaskFlightinfo[0], 0, 8)
        if TaskFlightinfo[3] == "AP":
            dq = "√" if 'P-PRE-FLIGHT' in str(tasksts) and 'DEPARTURE-SERVICE' in str(tasksts) else ""
            if 'P-PRE-FLIGHT' in str(tasksts) and 'DEPARTURE-SERVICE' in str(tasksts):
                dqxq="航前工卡√\n送机勤务工卡√"
            elif 'P-PRE-FLIGHT' in str(tasksts):
                dqxq="航前工卡√\n送机勤务工卡X"
            elif 'DEPARTURE-SERVICE' in str(tasksts):
                dqxq="航前工卡X\n送机勤务工卡√"
            else:
                dqxq = "航前工卡X\n送机勤务工卡X"
        elif TaskFlightinfo[3] == "TR" and "接" in row[3]:
            dq = "√" if 'ARRIVAL-SERVICE' in str(tasksts) else ""
            dqxq  = "接机勤务工卡√" if 'ARRIVAL-SERVICE' in str(tasksts) else "接机勤务工卡X"
        elif TaskFlightinfo[3] in ["TAF","停场"]:
            dq = "--"
            dqxq= "暂无完成的电签工卡"
        elif TaskFlightinfo[3] == "TR" and "送" in row[3]:
            dq = "√" if 'P-TRANSIT' in str(tasksts) and 'DEPARTURE-SERVICE' in str(tasksts) else ""
            if 'P-TRANSIT' in str(tasksts) and 'DEPARTURE-SERVICE' in str(tasksts):
                dqxq = "短停工卡√\n送机勤务工卡√"
            elif 'P-TRANSIT' in str(tasksts):
                dqxq = "短停工卡√\n送机勤务工卡X"
            elif 'DEPARTURE-SERVICE' in str(tasksts):
                dqxq ="短停工卡X\n送机勤务工卡√"
            else:
                dqxq = "短停工卡X\n送机勤务工卡X"
        elif TaskFlightinfo[3] == "AF":
            dq = "√" if 'P-POST-FLIGHT' in str(tasksts) and 'ARRIVAL-SERVICE' in str(tasksts) else ""
            if 'P-POST-FLIGHT' in str(tasksts) and 'ARRIVAL-SERVICE' in str(tasksts):
                dqxq = "航后工卡√\n接机勤务工卡√"
            elif 'P-POST-FLIGHT' in str(tasksts):
                dqxq = "航后工卡√\n接机勤务工卡X"
            elif 'ARRIVAL-SERVICE' in str(tasksts):
                dqxq = "航后工卡X\n接机勤务工卡√"
            else:
                dqxq = "航后工卡X\n接机勤务工卡X"

        #print(tasksts)
        #print(singleflightsearch)

    e = ""
    F = ""
    if TaskFlightinfo != None and TaskFlightinfo[16] != "":
        e = "保留"
    if TaskFlightinfo != None :
        takinfo = TuplefindInList(workjobDataAll, TaskFlightinfo[0])
        JWA = TaskFlightinfo[10]
        JWB = TaskFlightinfo[11]
        bayofflightplan_A = "飞机排班系统机位:" + TuplefindInList3(flightplan, TaskFlightinfo[39].replace("PBCZ", ""), 0, 7)
        bayofflightplan_B = "飞机排班系统机位:" + TuplefindInList3(flightplan, TaskFlightinfo[40].replace("PBCZ", ""), 0, 8)
        bayofflightplan_C = "飞机排班系统机位:N/A"
        if JWA =="" and JWB !="":
            JWA=JWB
        elif JWB =="" and JWA !="":
            JWB=JWA
        if TaskFlightinfo[18] != "":
            #takinfo = fdb.queryTabel('workjob', '*', "任务编号= '%s'" % TaskFlightinfo[0][21])
            F = "工作包无法正常获取请通过AMRO人工查询"
            if takinfo != None and takinfo[4] != "":
                F = takinfo[4] + "\n<+++++++++++++++++++++++++++++++++++++++++>\n" + takinfo[6] + "\n<+++++++++++++++++++++++++++++++++++++++++>\n" + takinfo[7]
            elif takinfo != None:
                F = "工作包暂未获取请点击右键工作包查询后再查看"
    else:
        JWA =""
        JWB =""
    wx_people = ""
    fx_people = ""
    ecsj_people = ""
    ta_td = ""
    type31 = ""
    if TaskFlightinfo != None:
        wx_people = TaskFlightinfo[12]
        fx_people = TaskFlightinfo[13]
        ecsj_people = TaskFlightinfo[19]
        ta_td = TaskFlightinfo[9]
        type31 = TaskFlightinfo[3]
    flightstsdata = TuplefindInList(flightStsDataAll, row[1]) if TuplefindInList(flightStsDataAll, row[1]) != None else ["", "","", "", "", "","", "", "", "","", "", "", ""]
    jjdw = "√" if flightstsdata[3] != "" else ""
    FX = "√" if flightstsdata[4] != "" else ""
    XJ = "√" if flightstsdata[5] != "" else ""
    ESDW = "√" if flightstsdata[7] != "" else ""

    if TaskFlightinfo[3] == "AF":
        postion = str(TaskFlightinfo[23]).split("</br>")[0]
    elif TaskFlightinfo[3] == "TR" or TaskFlightinfo[3] == "TAF":
        postion = str(TaskFlightinfo[23]).replace("</br>天府","").replace("</br>成都天府","")
    elif "</br>" in str(TaskFlightinfo[23]):
        postion = str(TaskFlightinfo[23]).split("</br>")[1]
    else:
        postion = "无信息"


    if TaskFlightinfo[4] != "" and (TaskFlightinfo[8] == "" or TaskFlightinfo[30] == ""):  #如果sta不为空，且eta或flying为空，显示sta
        a = TaskFlightinfo[4][-8:-3]
    else:
        a=""
    if TaskFlightinfo[6] != "" :   #当ata不为空显示ata，eta不为空显示eta否则为空
        b = TaskFlightinfo[6][-8:-3]
    elif TaskFlightinfo[8] != "":
        b = TaskFlightinfo[8][-8:-3]
    else:
        b = ''
    if TaskFlightinfo[7] != "" :
        c = '[实]' + TaskFlightinfo[7][-8:-3]
    elif TaskFlightinfo[5] != "":
        c = '[预]' + TaskFlightinfo[5][-8:-3]
    else:
        c = ''

    QColorList = ['black', 'white', '#F3ECD9', '#BAD1D1', '#DDE2E3', '#647277', '#00FFFF', 'blue']
    #QColorList = ['black', 'white', '#F3ECD9', '#BAD1D1', '#DDE2E3', '#647277', 'red', 'blue']
    waringMessageIDs = waringMessageID
    if str(row[1]) in str(waringMessageIDs["通用警告"]):
        color_code = QColorList[6]
        color_code1 = QColorList[0]
        color_code2 = QColorList[1]
        color_code3 = QColorList[1]
    elif row[3] == "短停接" and (str(row[1]) in str(waringMessageIDs["到位警告"]) or str(row[1]) in str(waringMessageIDs["ETA"]) or str(row[1]) in str(waringMessageIDs["BAY_2"])):
        color_code = QColorList[6]
        color_code1 = QColorList[0]
        color_code2 = QColorList[1]
        color_code3 = QColorList[1]
    elif row[3] == "短停送" and (str(row[1]) in str(waringMessageIDs["二送警告"]) or str(row[1]) in str(waringMessageIDs["TD"])):
        color_code = QColorList[6]
        color_code1 = QColorList[0]
        color_code2 = QColorList[1]
        color_code3 = QColorList[1]
    elif row[5] == "4":
        color_code = QColorList[5]
        color_code1 = QColorList[1]
        color_code2 = QColorList[1]
        color_code3 = QColorList[1]
    elif row[5] == "3":
        color_code = QColorList[3]
        color_code1 = QColorList[0]
        color_code2 = QColorList[0]
        color_code3 = QColorList[0]
    elif row[5] == "2":
        color_code = QColorList[2]
        color_code1 = QColorList[0]
        color_code2 = QColorList[0]
        color_code3 = QColorList[0]
    elif row[5] == "1":
        if (rowNum + 2) % 2 == 0:
            color_code = QColorList[4]
        else:
            color_code = QColorList[1]
        color_code1 = QColorList[0]
        color_code2 = QColorList[6]
        color_code3 = QColorList[7]
    findid = row[0] + "-A"
    peopleSchedule = TuplefindInList(peopleScheduleDataAll, findid)
    if peopleSchedule != None:
        fx = peopleSchedule[3]
        qw1 = peopleSchedule[4]
        qw2 = peopleSchedule[5]
        g1 = peopleSchedule[6]
        g2 = peopleSchedule[7]
        g3 = peopleSchedule[8]
        fj = str(peopleSchedule[9])
    else:
        fx = ""
        qw1 = ""
        qw2 = ""
        g1 = ""
        g2 = ""
        g3 = ""
        fj = ""
    if "随机离港" in str(fj):
        SJDW = "√" if flightstsdata[6] != "" else ""
    else:
        SJDW ="--"
    ggy = "【高高原】" if TaskFlightinfo[29] == "Y" else ""
    JBRY = "交班人员:" + TaskFlightinfo[37] if TaskFlightinfo[37] != "" else "无交班人员"
    if TaskFlightinfo[3] == "AF":
        item_dic = {"1": rowNum,
                    "3": TaskFlightinfo[1],
                    "4": TaskFlightinfo[1]+"\n"+TaskFlightinfo[21],
                    "5": TaskFlightinfo[26],
                    "6": TaskFlightinfo[26]+"\n"+TaskFlightinfo[25],
                    "7": TaskFlightinfo[25],
                    "8": JWA,
                    "9": TaskFlightinfo[21],
                    "11": a,
                    "13": b,
                    "15": "--",
                    "17": TaskFlightinfo[24],
                    "18": JWA,
                    "19": row[3],
                    "21": fx,
                    "23": qw1,
                    "25": qw2,
                    "27": g1,
                    "29": g2,
                    "31": g3,
                    "33": jjdw,
                    "35": "--",
                    "37": "--",
                    "38":dq,
                    "62":infos,
                    "40":dqxq,
                    "41": "--",
                    "43": e,
                    "45": F,
                    "47": postion + ggy,
                    "49": wx_people,  # flightinfodata[22],
                    "50":JBRY,
                    "51": fx_people,  # flightinfodata[23],
                    "53": row[1],
                    "55": row[1],
                    "57": ta_td,
                    "59": type31,
                    "61": fj,
                    "88": bayofflightplan_A,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5],
                    "119": row[2], #保障时间
                    "129": row[4],#航日期
                    "139": row[0]+"-A",#sor编号

                    }
    elif TaskFlightinfo[3] == "停场":
        item_dic = {"1": rowNum,
                    "3": TaskFlightinfo[1],
                    "4": TaskFlightinfo[1]+"\n"+TaskFlightinfo[21],
                    "5": TaskFlightinfo[26],
                    "6": TaskFlightinfo[26]+"\n"+TaskFlightinfo[25],
                    "7": TaskFlightinfo[25],
                    "8": TaskFlightinfo[10],
                    "9": "--",
                    "11": a,
                    "13": "--",
                    "15": "--",
                    "17": "--",
                    "18": TaskFlightinfo[10],
                    "19": row[3],
                    "21": fx,
                    "23": qw1,
                    "25": qw2,
                    "27": g1,
                    "29": g2,
                    "31": g3,
                    "33": "--",
                    "35": "--",
                    "37": "--",
                    "39": "--",
                    "41": "--",
                    "43": "--",
                    "45": "--",
                    "47": "停场",
                    "49": "--",  # flightinfodata[22],
                    "50":JBRY,
                    "51": "--",  # flightinfodata[23],
                    "53": row[1],
                    "55": "TSK",
                    "57": "--",
                    "59": "--",
                    "61": fj,
                    "88": bayofflightplan_C,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5],
                    "119": row[2], #保障时间
                    "129": row[4],#航日期
                    "139": row[0]+"-A",#sor编号

                    }

    elif TaskFlightinfo[3] == "AP":
        item_dic = {"1": rowNum,
                    "3": TaskFlightinfo[1],
                    "4": TaskFlightinfo[1]+"\n"+TaskFlightinfo[21],
                    "5": TaskFlightinfo[26],
                    "6": TaskFlightinfo[26]+"\n"+TaskFlightinfo[25],
                    "7": TaskFlightinfo[25],
                    "8": JWB,
                    "9": TaskFlightinfo[21],
                    "11": "",
                    "13": "",
                    "15": c,
                    "17": TaskFlightinfo[24],
                    "18": JWB,
                    "19": row[3],
                    "21": fx,
                    "23": qw1,
                    "25": qw2,
                    "27": g1,
                    "29": g2,
                    "31": g3,
                    "33": jjdw,
                    "35": FX,
                    "37": XJ,
                    "38":dq,
                    "62":infos,
                    "40":dqxq,
                    "41": SJDW,
                    "43": e,
                    "45": F,
                    "47": postion + ggy,
                    "49": wx_people,  # flightinfodata[22],
                    "50":JBRY,
                    "51": fx_people,  # flightinfodata[23],
                    "53": row[1],
                    "55": row[1],
                    "57": ta_td,
                    "59": type31,
                    "61": fj,
                    "88": bayofflightplan_B,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5],
                    "119": row[2], #保障时间
                    "129": row[4],#航日期
                    "139": row[0]+"-A",#sor编号
                    }

    elif (TaskFlightinfo[3] == "TR" or TaskFlightinfo[3] == "TAF") and (row[3] == "短停接" or row[3] == "特后前接"):
        item_dic = {"1": rowNum,
                    "3": TaskFlightinfo[1],
                    "4": TaskFlightinfo[1]+"\n"+TaskFlightinfo[21],
                    "5": TaskFlightinfo[26],
                    "6": TaskFlightinfo[26]+"\n"+TaskFlightinfo[25],
                    "7": TaskFlightinfo[25],
                    "8": JWA,
                    "9": TaskFlightinfo[21],
                    "11": a,
                    "13": b,
                    "15": c,
                    "17": TaskFlightinfo[24],
                    "18": JWA,
                    "19": row[3],
                    "21": fx,
                    "23": qw1,
                    "25": qw2,
                    "27": g1,
                    "29": g2,
                    "31": g3,
                    "33": jjdw,
                    "35": FX,
                    "37": "--",
                    "38":dq,
                    "62":infos,
                    "40":dqxq,
                    "41": SJDW,
                    "43": e,
                    "45": F,
                    "47": postion + ggy,
                    "49": wx_people,  # flightinfodata[22],
                    "50":JBRY,
                    "51": fx_people,  # flightinfodata[23],
                    "53": row[1],
                    "55": row[1],
                    "57": ta_td,
                    "59": type31,
                    "61": fj,
                    "88": bayofflightplan_A,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5],
                    "119": row[2], #保障时间
                    "129": row[4],#航日期
                    "139": row[0]+"-A",#sor编号
                    }
    elif (TaskFlightinfo[3] == "TR" or TaskFlightinfo[3] == "TAF") and (row[3] == "短停送" or row[3] == "特后前送"):
        item_dic = {"1": rowNum,
                    "3": TaskFlightinfo[1],
                    "4": TaskFlightinfo[1]+"\n"+TaskFlightinfo[21],
                    "5": TaskFlightinfo[26],
                    "6": TaskFlightinfo[26]+"\n"+TaskFlightinfo[25],
                    "7": TaskFlightinfo[25],
                    "8": JWA,
                    "9": TaskFlightinfo[21],
                    "11": a,
                    "13": b,
                    "15": c,
                    "17": TaskFlightinfo[24],
                    "18": JWA,
                    "19": row[3],
                    "21": fx,
                    "23": qw1,
                    "25": qw2,
                    "27": g1,
                    "29": g2,
                    "31": g3,
                    "33": ESDW,
                    "35": FX,
                    "37": "--",
                    "38":dq,
                    "62":infos,
                    "40":dqxq,
                    "41": SJDW,
                    "43": e,
                    "45": F,
                    "47": postion + ggy,
                    "49": ecsj_people,  # flightinfodata[22],
                    "50":JBRY,
                    "51": fx_people,  # flightinfodata[23],
                    "53": row[1],
                    "55": row[1],
                    "57": ta_td,
                    "59": type31,
                    "61": fj,
                    "88": bayofflightplan_B,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5],
                    "119": row[2], #保障时间
                    "129": row[4],#航日期
                    "139": row[0]+"-A",#sor编号
                    }
    else:
        item_dic = {"1": rowNum,
                    "3": TaskFlightinfo[1],
                    "4": TaskFlightinfo[1]+"\n"+TaskFlightinfo[21],
                    "5": TaskFlightinfo[26],
                    "6": TaskFlightinfo[26]+"\n"+TaskFlightinfo[25],
                    "7": TaskFlightinfo[25],
                    "8": JWA,
                    "9": TaskFlightinfo[21],
                    "11": a,
                    "13": b,
                    "15": c,
                    "17": TaskFlightinfo[24],
                    "18": JWA,
                    "19": row[3],
                    "21": fx,
                    "23": qw1,
                    "25": qw2,
                    "27": g1,
                    "29": g2,
                    "31": g3,
                    "33": jjdw,
                    "35": FX,
                    "37": "--",
                    "38":dq,
                    "62":infos,
                    "40": dqxq,
                    "41": SJDW,
                    "43": e,
                    "45": F,
                    "47": postion + ggy,
                    "49": wx_people,  # flightinfodata[22],
                    "50": JBRY,
                    "51": fx_people,  # flightinfodata[23],
                    "53": row[1],
                    "55": row[1],
                    "57": ta_td,
                    "59": type31,
                    "61": fj,
                    "88": bayofflightplan_A,
                    "99": color_code,
                    "101": color_code1,
                    "102": color_code2,
                    "103": color_code3,
                    "109": row[5],
                    "119": row[2], #保障时间
                    "129": row[4],#航日期
                    "139": row[0]+"-A",#sor编号
                    }
    for i in dic:
        if i not in item_dic.keys():
            item_dic[i] = ''

    #tableWidgetDispalydata[rowNum] = item_dic
    #rowNum += 1
    # print(rowNum)
    # print(item_dic)
    # print({rowNum:item_dic})
    return {rowNum:item_dic}