import datetime

from unitls.LogerinTxt import app_logger
from .flightDB import flightDB
from .initFlightDatabase import initFlightDatabase


def getSortFlightdata(fdb:flightDB, datatime:str,tablename:str):
    try:
        nowDayStr = datatime
        sortData = fdb.sortTable('{}{}'.format(tablename,nowDayStr), '级别,保障时间','asc')
        return sortData
    except Exception as e:
        app_logger.log_error(e)
def sortFlighttime2(database:flightDB,wokload:flightDB):
    try:
        sortLabel = ["编号", "航班编号", "保障时间", "航班类型", "航班日期", "级别"]
        nowDay = datetime.datetime.now().strftime("%Y%m%d")
        nowDay_1 = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
        nowDay_2 = (datetime.date.today() + datetime.timedelta(days=1)).strftime("%Y%m%d")
        now = datetime.datetime.now()
        #now_20 = (datetime.datetime.now() + datetime.timedelta(minutes=200)).strftime("%Y%m%d")
        now_20 = (datetime.datetime.now() + datetime.timedelta(hours=5)).strftime("%Y%m%d")
        initFlightDatabase(database,  wokload, now_20)
        initFlightDatabase(database,  wokload, nowDay_1)
        initFlightDatabase(database,  wokload, nowDay_2)
        initFlightDatabase(database,  wokload, nowDay)

        if now_20 == nowDay:
            day = [nowDay, nowDay_1]
        else:
            day = [nowDay, nowDay_1, nowDay_2]

        for selectedtime in day:
            sort2NOs_arr=[]
            sort2NOs = database.getSingledata("编号","sortFlight2{}".format(selectedtime))
            sortNOs = database.getSingledata("编号", "sortFlight{}".format(selectedtime))
            if sort2NOs !=None and len(sort2NOs)!=0:
                for i in sort2NOs:
                    if str(i) in str(sortNOs):
                        sort2NOs_arr.append(i[0])
                    else:
                        database.deleteSingledata("sortFlight2{}".format(selectedtime),"编号 = '%s'"%i[0])

            sorttable = getSortFlightdata(database, selectedtime,"sortFlight")
            for row in sorttable:
                if row[3] == "短停接" or row[3] == "特后前接":
                    if str(row[5]) == "4":
                        find_id = row[1] + "-2"
                        for row2 in sorttable:
                            if row2[0] == find_id and row2[5] == "4":
                                jb = row[5]
                            elif row2[0] == find_id:
                                jb = "1"
                    else:
                        jb = row[5]
                    newdic = {}
                    newdic2={}
                    for col in range(0,6):
                        if col == 0:
                            newdic[sortLabel[col]]=row[col]
                        elif col != 5:
                            newdic[sortLabel[col]]=row[col]
                            newdic2[sortLabel[col]]="'%s'"%row[col]
                        else:
                            newdic[sortLabel[5]]=jb
                            newdic2[sortLabel[5]] = "'%s'"%jb
                    if str(row[0]) not in str(sort2NOs_arr):
                        database.lazyInsertData('sortFlight2{}'.format(selectedtime),newdic)
                    else:
                        database.lazyUpdateItem('sortFlight2{}'.format(selectedtime),newdic2,"编号 = '%s'"%row[0])
                elif row[3] != "短停送" and row[3] != "特后前送":
                    newdic = {}
                    newdic2 = {}
                    for col in range(0,6):
                        if col == 0:
                            newdic[sortLabel[col]]=row[col]
                        else:
                            newdic[sortLabel[col]]=row[col]
                            newdic2[sortLabel[col]] = "'%s'"%row[col]
                    if str(row[0]) not in str(sort2NOs_arr):
                        database.lazyInsertData('sortFlight2{}'.format(selectedtime), newdic)
                    else:
                        database.lazyUpdateItem('sortFlight2{}'.format(selectedtime), newdic2, "编号 = '%s'" % row[0])
        database.FunctionCommit()
    except Exception as e:
        app_logger.log_error(e)