import datetime

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


def TaskListInsert(database: flightDB, datatime: str, jsonData):
    nowDayStr = datatime
    flightinfo_flight_ids_arr = []
    flightinfo_flight_ids = database.queryTabel('TaskList{}'.format(nowDayStr), '*', "TASKID !=''")#确认一下要不要取整张表
    if len(flightinfo_flight_ids) != 0:
        for i in flightinfo_flight_ids:
            flightinfo_flight_ids_arr.append(i[0])
    flight_id_now = []
    keys = get_dic('TaskListLabel')
    if jsonData != "" and jsonData != None and jsonData != {} and len(jsonData) != 0:
        for row in jsonData:
            flight_id_now.append(row["TASKID"])
            if str(row["TASKID"]) not in str(flightinfo_flight_ids_arr):
                newDic={}
                for key in keys:
                    if key in row.keys():
                        newDic[key] = str(row[key])
                    else:
                        if key != "STA":
                            newDic[key] = ""
                        elif row["TASKTYPE"] == "航前":
                            newDic[key] = datetime.datetime.strptime(row["STARTDATE_PL"],"%Y-%m-%d %H:%M:%S") + datetime.timedelta(hours=2)
                        else:
                            newDic[key] = datetime.datetime.strptime(row["STARTDATE_PL"],"%Y-%m-%d %H:%M:%S") + datetime.timedelta(minutes=15)

                database.lazyInsertData('TaskList{}'.format(nowDayStr), newDic)
            elif str(row["TASKID"]) in str(flightinfo_flight_ids_arr):
                for key in keys:
                    if key in row.keys():
                        b = baseFunction.TuplefindInList3(flightinfo_flight_ids , row[ "TASKID" ] , 0 , keys.index(key))
                        if b != row[key]:
                            newdic = {"%s"%key: "'%s'" % row[key]}
                            database.lazyUpdateItem('TaskList{}'.format(nowDayStr), newdic,"TASKID='%s'" % row["TASKID"])
                    else:
                        if key != "STA":
                            newdata = ""
                        elif row["TASKTYPE"] == "航前":
                            newdata = datetime.datetime.strptime(row["STARTDATE_PL"],"%Y-%m-%d %H:%M:%S") + datetime.timedelta(hours=2)
                        else:
                            newdata = datetime.datetime.strptime(row["STARTDATE_PL"],"%Y-%m-%d %H:%M:%S") + datetime.timedelta(minutes=15)

                        newdic = {"%s"%key: "'%s'"%newdata}
                        database.lazyUpdateItem('TaskList{}'.format(nowDayStr), newdic,"TASKID='%s'" % row["TASKID"])

        database.FunctionCommit()
    else:
        print(datetime.datetime.now(), "[数据库操作]TaskList航班数据为空")
        app_logger.log_error("[数据库操作]TaskList航班数据为空")
        pass
    if jsonData !="" and jsonData !=None and jsonData != {} and len(flight_id_now) != 0 and len(jsonData)!=0:
        flightinfo_flight_ids = database.queryTabel('TaskList{}'.format(nowDayStr),'*',"TASKID !=''")
        if len(flightinfo_flight_ids) != 0:
            for iii in flightinfo_flight_ids:
                if str(iii[0]) not in flight_id_now :
                    database.deleteSingledata("TaskList{}".format(nowDayStr), "TASKID='%s'" % str(iii[0]) )
        database.FunctionCommit()


