import concurrent.futures

from unitls.LogerinTxt import app_logger
from unitls.baseFunction import TuplefindInList
from .flightDB import flightDB
from .getSortFlightdata import getSortFlightdata
from .waringMessageID import waringMessageID


def tableWidgetDispaly(fdb:flightDB,selectedtime,date):#1,昨天。2今天3明天4其他
    try:
        #t1 = datetime.datetime.now()
        tableWidgetDispalydata = {}
        nowDayStr = selectedtime
        sorttable = getSortFlightdata(fdb, selectedtime, "sortFlight")
        #t2 = datetime.datetime.now()
        #print(f'【航班刷新】【数据库1】:{t2 - t1}')
        TaskFlightinfoAlldata = fdb.getAlldata('TaskFlightinfo{}'.format(nowDayStr))
        #t3 = datetime.datetime.now()
        #print(f'【航班刷新】【数据库2】:{t3 - t2}')
        #flightInfoDataAll = fdb.getAlldata('flightinfo{}'.format(nowDayStr))
        #t4 = datetime.datetime.now()
        #print(f'【航班刷新】【数据库3】:{t4 - t3}')
        flightStsDataAll = fdb.getAlldata('flightsts{}'.format(nowDayStr))
        #t5 = datetime.datetime.now()
        #print(f'【航班刷新】【数据库4】:{t5 - t4}')
        peopleScheduleDataAll = fdb.getAlldata("peopleSchedule{}".format(nowDayStr))
        #t6 = datetime.datetime.now()
        #print(f'【航班刷新】【数据库5】:{t6 - t5}')
        workjobDataAll = fdb.getAlldata('workjob')
        #t7 = datetime.datetime.now()
        #print(f'【航班刷新】【数据库6】:{t7 - t6}')
        #print(f'【航班刷新】【数据库ALL】:{t7 - t1}')
        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,flightInfoDataAll, flightStsDataAll,peopleScheduleDataAll,waringMessageIDs))
            all_task.append(executor.submit(functionDisaplay, sorttable[i],i, TaskFlightinfoAlldata,workjobDataAll, flightStsDataAll,peopleScheduleDataAll,waringMessageIDs))

        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):
    #print("kaishi")
    TaskFlightinfo = TuplefindInList(TaskFlightinfoAlldata,row[1])
    #print(TaskFlightinfo)
    # print(row)

    e = ""
    F = ""
    g = ""

    if TaskFlightinfo != None and TaskFlightinfo[16] != "":
        e = "保留"
    if TaskFlightinfo != None :
        takinfo = TuplefindInList(workjobDataAll, TaskFlightinfo[21])
        JWA = TaskFlightinfo[10]
        JWB = TaskFlightinfo[11]
        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 =""
    if TaskFlightinfo != None and TaskFlightinfo[21] != "":
        g = TaskFlightinfo[21]
    #print("tttt:{}".format(g))
    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]
    # print(row[1])
    # print(fdb.queryTabel('flightinfo{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1])))
    #flightinfodata = TuplefindInList(flightInfoDataAll,row[1])
    #flightinfodata = fdb.queryTabel('flightinfo{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))[0]
    flightstsdata = TuplefindInList(flightStsDataAll, row[1]) if TuplefindInList(flightStsDataAll, row[1]) != None else ["", "","", "", "", "","", "", "", "","", "", "", ""]
    # flightstsdata = fdb.queryTabel('flightsts{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))[0] if \
    #     len(fdb.queryTabel('flightsts{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))) != 0 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>天府","")
    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 = ''
    d = TaskFlightinfo[15] if "随机" in str(TaskFlightinfo[15]) else ""

    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[1]
        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[1]
        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[1]
        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 ="--"
    if TaskFlightinfo[3] == "AF":
        item_dic = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": a,
                    "6": b,
                    "7": "--",
                    "8": JWA,
                    "9": row[3],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "16": jjdw,
                    "17": "--",
                    "18": "--",
                    "19": d,
                    "20": "--",
                    "21": e,
                    "22": F,
                    "23": postion,
                    "24": wx_people,  # flightinfodata[22],
                    "25": fx_people,  # flightinfodata[23],
                    "26": row[1],
                    "27": g,
                    "28": ta_td,
                    "29": type31,
                    "30": fj,
                    "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 = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": "--",
                    "5": a,
                    "6": "--",
                    "7": "--",
                    "8": TaskFlightinfo[10],
                    "9": row[3],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "16": "--",
                    "17": "--",
                    "18": "--",
                    "19": "--",
                    "20": "--",
                    "21": "--",
                    "22": "--",
                    "23": "--",
                    "24": "--",  # flightinfodata[22],
                    "25": "--",  # flightinfodata[23],
                    "26": row[1],
                    "27": "TSK",
                    "28": "--",
                    "29": "--",
                    "30": fj,
                    "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 = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": "",
                    "6": "",
                    "7": c,
                    "8": JWB,
                    "9": row[3],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "16": jjdw,
                    "17": FX,
                    "18": XJ,
                    "19": d,
                    "20": SJDW,
                    "21": e,
                    "22": F,
                    "23": postion,
                    "24": wx_people,  # flightinfodata[22],
                    "25": fx_people,  # flightinfodata[23],
                    "26": row[1],
                    "27": g,
                    "28": ta_td,
                    "29": type31,
                    "30": fj,
                    "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 = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": a,
                    "6": b,
                    "7": c,
                    "8": JWA,
                    "9": row[3],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "16": jjdw,
                    "17": FX,
                    "18": "--",
                    "19": d,
                    "20": SJDW,
                    "21": e,
                    "22": F,
                    "23": postion,
                    "24": wx_people,  # flightinfodata[22],
                    "25": fx_people,  # flightinfodata[23],
                    "26": row[1],
                    "27": g,
                    "28": ta_td,
                    "29": type31,
                    "30": fj,
                    "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 = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": a,
                    "6": b,
                    "7": c,
                    "8": JWA,
                    "9": row[3],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "16": ESDW,
                    "17": FX,
                    "18": "--",
                    "19": d,
                    "20": SJDW,
                    "21": e,
                    "22": F,
                    "23": postion,
                    "24": ecsj_people,  # flightinfodata[22],
                    "25": fx_people,  # flightinfodata[23],
                    "26": row[1],
                    "27": g,
                    "28": ta_td,
                    "29": type31,
                    "30": fj,
                    "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 = {"0": rowNum,
                    "1": TaskFlightinfo[1],
                    "2": TaskFlightinfo[26],
                    "3": TaskFlightinfo[25],
                    "4": TaskFlightinfo[21],
                    "5": a,
                    "6": b,
                    "7": c,
                    "8": JWA,
                    "9": row[3],
                    "10": fx,
                    "11": qw1,
                    "12": qw2,
                    "13": g1,
                    "14": g2,
                    "15": g3,
                    "16": jjdw,
                    "17": FX,
                    "18": "--",
                    "19": d,
                    "20": SJDW,
                    "21": e,
                    "22": F,
                    "23": postion,
                    "24": wx_people,  # flightinfodata[22],
                    "25": fx_people,  # flightinfodata[23],
                    "26": row[1],
                    "27": g,
                    "28": ta_td,
                    "29": type31,
                    "30": fj,
                    "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编号
                    }
    #tableWidgetDispalydata[rowNum] = item_dic
    #rowNum += 1
    # print(rowNum)
    # print(item_dic)
    # print({rowNum:item_dic})
    return {rowNum:item_dic}