import datetime

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

APZBZ, JJZBZ, ESZBZ, APGR, JJGR, ESGR,APPG,JJPG,ESPG=MesgTime()

def WarningInsert(database, taskid, mesg, types):
    newdic = {"航班编号": "%s" % taskid, "警告信息": mesg, "信息状态": "",
              "提示信息": "", "产生时间": "%s" % datetime.datetime.now(), "处理人": "", "处理时间": "", "变更字段": types, "显示对象": "", "提示内容": ""}
    database.lazyInsertData('logs', newdic)

def noteInsert(database, taskid, mesg, types):
    newdic = {"航班编号": "%s" % taskid, "警告信息": '', "信息状态": "",
              "提示信息": mesg, "产生时间": "%s" % datetime.datetime.now(), "处理人": "", "处理时间": "", "变更字段": types, "显示对象": "", "提示内容": ""}
    database.lazyInsertData('logs', newdic)

def judgeFunction(datas,key,numb): #numb 警告时间间隔
        now=datetime.datetime.now()
        time_str = baseFunction.TuplefindInList3(datas, key, 8, 5) #变更字段为key的最近的一次时间
        if time_str == "":
            time1 = now - datetime.timedelta(minutes=numb)
        else:
            time1 = datetime.datetime.strptime(time_str.split(".")[0], "%Y-%m-%d %H:%M:%S") + datetime.timedelta(minutes=numb)
        if now > time1:
            return True
        else:
            return False
def getpeopleinfo(names,data,data1):
    res = []
    success = []
    fail = []
    names = names.replace("√","")
    names_list = names.split(",")
    for name in names_list:
        worknum = baseFunction.TuplefindInList3(data, name, 0, 1) if data else ""
        if worknum == "":
            worknum=baseFunction.TuplefindInList3(data1, name, 0, 1)
        if worknum != "":
            res.append(worknum)
            success.append(name)
        else:
            fail.append(name)
    #int("success",success)
    #print("fail",fail)
    #print("res",res)
    return {"success":success,"fail":fail,"worknums":res}
def checkFlightAlert(database:flightDB, singlePeopleNotice):
    try:
        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=120)).strftime("%Y%m%d")
        if now_20 == nowDay:
            day=[nowDay,nowDay_1]
        else:
            day = [nowDay,nowDay_1,nowDay_2]
        for nowDayStr in day:
            flightcheck=database.queryTabel("sortFlight{}".format(nowDayStr),"航班编号","CAST(级别 AS integer) = 1" )
            flightcheck2 = database.queryTabel("sortFlight{}".format(nowDayStr), "航班编号", "CAST(级别 AS integer) != 4")
            datas=database.queryTabel("TaskFlightinfo{}".format(nowDayStr),"*","航班编号 !=''")
            flightsts=database.queryTabel("flightsts{}".format(nowDayStr),"*","航班编号 !=''")
            logs=database.queryTabel("logs","*","处理时间= '' and 警告信息 !=''")
            logsall=database.queryTabel("logs","*","警告信息 !=''")
            logsall1=database.sort_queryTable("*","logs", "提示信息 !=''", "产生时间", "desc")  #所有提示信息
            peopleschedule=database.queryTabel("peopleSchedule{}".format(nowDayStr),"*","航班编号 !=''")
            peoplesinfo_today=database.queryTabel("pglist{}".format(nowDay),"*","姓名!=''")
            peoplesinfo_yes=database.queryTabel("pglist{}".format(nowDay_1),"*","姓名!=''")

            if datas and flightcheck:
                for i in flightcheck:
                    data=baseFunction.TuplefindInList1(datas, i[0], 0)
                    if data and data[0][3] != "停场":
                        a = data[0][1]
                        b = get_dic("taskType")[data[0][3]]
                        std=data[0][5]
                        singlelogs=baseFunction.TuplefindInList33(logs,i[0],1,8)#未处理的警告信息

                        singlelogstext=baseFunction.TuplefindInList33(logs,i[0],1,2)#未处理的警告信息
                        singlelogsall=baseFunction.TuplefindInList33(logsall, i[0], 1, 2) #所有警告信息
                        singlelogsall1=baseFunction.TuplefindInList11(logsall1, i[0], 1) #所有和这个id相关的提示信息
                        jjdw=baseFunction.TuplefindInList3(flightsts,i[0],0,3)
                        fx=baseFunction.TuplefindInList3(flightsts,i[0],0,4)
                        eta=data[0][8]
                        flying=data[0][30]
                        BAY_A=data[0][10]#进港机位
                        wxry=data[0][12]
                        fxry=data[0][13]
                        ecsj=data[0][19]
                        fj=baseFunction.TuplefindInList3(peopleschedule, i[0], 1, 9)
                        #航前90min 检查到位情况，推送准备组，推送个人没2min，直到点击到位
                        #起飞前40min 检查放行情况，推送个人，3min间隔，起飞前30min推送准备组，直到放行点击
                        #起飞前50min 检查销子夹板取下情况，推送个人，3min间隔，起飞前40min推送准备组，直到上报销子夹板
                        if b == "航前":
                            c = baseFunction.TuplefindInList3(datas, i[0], 0, 11)
                        else:
                            c = baseFunction.TuplefindInList3(datas, i[0], 0, 10)
                        try:
                            if b == "航前" and std != "":
                                STD_90 = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=90)
                                STD_50 = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=50)
                                STD_40 = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=40)
                                STD_30 = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=30)
                                if STD_90 < now and "已到位" not in str(jjdw) and "到位警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s到位情况!"%(a,b,c),"到位警告")
                                if STD_90 < now and "已到位" not in str(jjdw) and judgeFunction(singlelogsall1,"到位个人提示推送",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"航前推送个人到位信息")  ###要加维修人员是否为空
                                    getpeopleinfo(wxry,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s航前推送个人到位信息"%(a,b,c),"到位个人提示推送")

                                if STD_30 < now and "已放行" not in str(fx) and "放行警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s放行情况!"%(a,b,c),"放行警告")
                                if STD_40 < now and "已放行" not in str(fx) and judgeFunction(singlelogsall1,"放行个人提示推送",3) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"航前推送放行确认信息")
                                    getpeopleinfo(fxry, peoplesinfo_today, peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s航前推送放行确认信息"%(a,b,c),"放行个人提示推送")

                                if STD_40 < now and "已取下" not in str(baseFunction.TuplefindInList3(flightsts,i[0],0,5)) and "销夹警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s销夹取下情况!"%(a,b,c), "销夹警告")
                                if STD_50 < now and "已取下" not in str(baseFunction.TuplefindInList3(flightsts,i[0],0,5)) and judgeFunction(singlelogsall1,"销夹个人提示推送",3) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"航前推送销夹取下信息")
                                    noteInsert(database,i[0],"%s/%s/%s航前推送销夹取下信息"%(a,b,c),"销夹个人提示推送")
                            # 航后15min 检查到位情况，推送准备组，推送个人每2min，直到点击到位
                            elif b == "航后" and eta != "" and flying != "":
                                ETA_15_str = eta
                                ETA_15 = datetime.datetime.strptime(ETA_15_str, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes= 15)
                                ETA_40 = datetime.datetime.strptime(ETA_15_str, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes= 55)
                                ETA = datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")
                                NXTSTD = datetime.datetime.strptime(data[0][32], "%Y-%m-%d %H:%M:%S") if data[0][32] != "" else ""
                                if NXTSTD != "" and abs((NXTSTD-ETA).total_seconds()) < 3 * 3600 and "特后前保障提示" not in str(singlelogsall):
                                    WarningInsert(database, i[0], "%s/%s/%s预计落地和预计起飞时间相差小于3H,请结合手册判断是否符合特后前签署条件，如需签署特后前注意通报72!"%(a, b, c), "特后前保障提示")

                                if ETA_15 < now and "已到位" not in str(jjdw) and "到位警告" not in str(singlelogs):
                                    WarningInsert(database, i[0], "请确认%s/%s/%s到位情况!"%(a, b, c), "到位警告")
                                if ETA_15 < now and "已到位" not in str(jjdw) and judgeFunction(singlelogsall1,"到位个人提示推送",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"航后推送个人到位信息")
                                    getpeopleinfo(wxry,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s航后推送个人到位信息"%(a,b,c),"到位个人提示推送")

                                if ETA_40 < now and BAY_A == "" and "机位未出提醒" not in str(singlelogsall):
                                    WarningInsert(database,i[0],"[机位未出提醒]请确认%s/%s机位情况!"%(a,b),"机位警告")
                                elif BAY_A != "" and "机位未出提醒" in str(singlelogstext):
                                    mesg="[机位未出提醒]请确认%s/%s机位情况!"%(a,b)
                                    newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                    database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))

                            # 短停15min 检查到位情况，推送准备组，推送个人每2min，直到点击到位
                            # 短停落地30min，且满足距离起飞时间小于50min检查放行情况，推送个人，3min间隔，落地30min，且满足距离起飞时间小于40min，推送准备组，直到点击放行
                            # 短停落地25min，且满足距离起飞时间小于25min，推送个人，2min间隔，落地30min，且满足距离起飞时间小于25min，推送准备组，直到点击二送到位

                            elif b == "短停" and eta != "" and flying != "" and std != "":
                                ETA_15_str=eta
                                ETA_15 = datetime.datetime.strptime(ETA_15_str, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=15)
                                ETA_40 = datetime.datetime.strptime(ETA_15_str, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=55)
                                ATA = baseFunction.TuplefindInList3(datas, i[0], 0, 6)
                                if ATA != "":
                                    ATA_time_25 = datetime.datetime.strptime(ATA, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(minutes=25)
                                    ATA_time_30 = datetime.datetime.strptime(ATA, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(minutes=30)
                                    ATA_time_3H = datetime.datetime.strptime(ATA, "%Y-%m-%d %H:%M:%S")+datetime.timedelta(minutes=180)
                                else:
                                    ATA_time_30 = ""
                                    ATA_time_25 = ""
                                    ATA_time_3H = ""
                                TD = std
                                TD_time_50 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S")- datetime.timedelta(minutes=50)
                                TD_time_40 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S")- datetime.timedelta(minutes=40)
                                TD_time_25 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S")- datetime.timedelta(minutes=25)
                                TD_time_120 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=120)
                                TD_time = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S")
                                if ETA_15 < now and "已到位" not in str(jjdw) and  "到位警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s到位情况!"%(a,b,c),"到位警告")
                                if ETA_15 < now and "已到位" not in str(jjdw) and judgeFunction(singlelogsall1,"到位个人提示推送",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"短停推送个人到位信息")
                                    getpeopleinfo(wxry,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s短停推送个人到位信息"%(a,b,c),"到位个人提示推送")


                                if ATA_time_30 != "" and ATA_time_30 < now and TD_time_40 < now and "已放行" not in str(fx) and "放行警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s放行情况!"%(a,b,c), "放行警告")
                                if ATA_time_30 != "" and ATA_time_30 < now and TD_time_50 < now and "已放行" not in str(fx) and judgeFunction(singlelogsall1,"放行个人提示推送",3) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"短停放行个人提示推送")
                                    getpeopleinfo(fxry,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s短停放行个人提示推送"%(a,b,c),"放行个人提示推送")

                                if ATA_time_30 != "" and ATA_time_30 < now and TD_time_25 < now and "已到位" not in str(baseFunction.TuplefindInList3(flightsts,i[0],0,7)) and "二送警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s二送到位情况!"%(a,b,c), "二送警告")
                                if ATA_time_25 != "" and ATA_time_25 < now and TD_time_25 < now and "已到位" not in str(baseFunction.TuplefindInList3(flightsts,i[0],0,7)) and judgeFunction(singlelogsall1,"二送到位个人提示推送",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"短停二送到位个人提示推送")
                                    getpeopleinfo(ecsj,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s短停二送到位个人提示推送"%(a,b,c),"二送到位个人提示推送")

                                if ATA_time_3H !="" and ATA_time_3H < TD_time and TD_time_120 < now and "长短停到位提示" not in str(baseFunction.TuplefindInList33(logsall, i[0], 1, 8)):
                                    WarningInsert(database,i[0],"请确认安排%s/%s/%s长短停上电人员!" % (a, b, c),"长短停到位提示")
                                if ATA_time_3H !="" and ATA_time_3H < TD_time and "长短停值守提示" not in str(baseFunction.TuplefindInList33(logsall, i[0], 1, 8)):
                                    WarningInsert(database,i[0],"请注意安排%s %s长短停值守，如无需则忽略!" % (a,c),"长短停值守提示")
                                if ETA_40 < now and BAY_A == "" and "机位未出提醒" not in str(singlelogsall):
                                    WarningInsert(database,i[0],"[机位未出提醒]请确认%s/%s机位情况!"%(a,b),"机位警告")
                                elif BAY_A != "" and "机位未出提醒" in str(singlelogstext):
                                    mesg="[机位未出提醒]请确认%s/%s机位情况!"%(a,b)
                                    newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                    database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))

                            # 特后前15min 检查到位情况，推送准备组，推送个人每2min，直到点击到位
                            # 特后前落地30min，且满足距离起飞时间小于50min检查放行情况，推送个人，3min间隔，落地30min，且满足距离起飞时间小于40min，推送准备组，直到点击放行
                            # 特后前落地25min，且满足距离起飞时间小于25min，推送个人，2min间隔，落地30min，且满足距离起飞时间小于25min，推送准备组，直到点击二送到位

                            elif b!="短停" and b!="航后" and b!="航前" and b!="未显示" and  eta != ""and std != "":
                                ETA_15_str =  eta
                                ETA_15 = datetime.datetime.strptime(ETA_15_str, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes = 15)
                                ETA_40 = datetime.datetime.strptime(ETA_15_str, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes = 55)
                                ATA =  baseFunction.TuplefindInList3(datas, i[0], 0, 6)

                                if ATA!= "":
                                    ATA_time_25 = datetime.datetime.strptime(ATA, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(minutes=25)
                                    ATA_time_30 = datetime.datetime.strptime(ATA, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(minutes=30)
                                else:
                                    ATA_time_25=""
                                    ATA_time_30 = ""
                                TD =  std
                                TD_time_50 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S")- datetime.timedelta(minutes=50)
                                TD_time_40 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S")  - datetime.timedelta(minutes=40)
                                TD_time_25 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S")  - datetime.timedelta(minutes=25)

                                if ETA_15 < now and "已到位" not in str(jjdw) and "到位警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s到位情况!"%(a,b,c),"到位警告")
                                if ETA_15 < now and "已到位" not in str(jjdw)  and judgeFunction(singlelogsall1,"到位个人提示推送",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"短停推送个人到位信息")
                                    getpeopleinfo(wxry,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s短停推送个人到位信息"%(a,b,c),"到位个人提示推送")

                                if ATA_time_30 != "" and ATA_time_30 < now and TD_time_40 < now and "已放行" not in str(fx) and  "放行警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s放行情况!"%(a,b,c),"放行警告")
                                if ATA_time_30 != "" and ATA_time_30 < now and TD_time_50 < now and "已放行" not in str(fx) and judgeFunction(singlelogsall1,"放行个人提示推送",3) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"短停放行个人提示推送")
                                    getpeopleinfo(fxry,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s短停放行个人提示推送"%(a,b,c),"放行个人提示推送")


                                if ATA_time_30 != "" and ATA_time_30 < now and TD_time_25 < now and "已到位" not in str(baseFunction.TuplefindInList3(flightsts,i[0],0,7)) and  "二送警告" not in str(singlelogs):
                                    WarningInsert(database,i[0],"请确认%s/%s/%s二送到位情况!"%(a,b,c), "二送警告")
                                if ATA_time_25 != "" and ATA_time_25 < now and TD_time_25 < now and "已到位" not in str(baseFunction.TuplefindInList3(flightsts,i[0],0,7)) and judgeFunction(singlelogsall1,"二送到位个人提示推送",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                    print(datetime.datetime.now(),"短停二送到位个人提示推送")
                                    getpeopleinfo(ecsj,peoplesinfo_today,peoplesinfo_yes)
                                    noteInsert(database,i[0],"%s/%s/%s短停二送到位个人提示推送"%(a,b,c),"二送到位个人提示推送")

                                if ETA_40 < now and BAY_A == "" and "机位未出提醒" not in str(singlelogsall):
                                    WarningInsert(database,i[0],"[机位未出提醒]请确认%s/%s机位情况!"%(a,b),"机位警告")
                                elif BAY_A != "" and "机位未出提醒" in str(singlelogstext):
                                    mesg="[机位未出提醒]请确认%s/%s机位情况!"%(a,b)
                                    newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                    database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))
                        except Exception as e:
                            app_logger.log_error(e)
                            pass

                        try:
                            #航前 起飞前 100min，没有确认任务，推送准备组， 二送起飞前40min没有确认任务推送准备组  105 和 45 min  2min 个人推送一次？
                            #短停 特后前 预计到达前40min没有确认任务推送准备组，短停特后前 预计起飞前40min没有确认任务推送准备组  45min 2min 个人推送一次？
                            #航后 预计到达前40min没有确认任务推送准备组  45min 2min 个人推送一次？
                            if b == "航前":
                                bztime=datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=APZBZ)
                                esbztime=datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=ESZBZ)
                                bztztime=datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=APGR)
                                estzbztime=datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=ESGR)
                                pgtime = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=APPG)
                                espgtime = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=ESPG)

                            elif b == "短停" and std != '':
                                bztime=datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJZBZ)#40
                                esbztime = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=ESZBZ)#40
                                bztztime=datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJGR)
                                estzbztime = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=ESGR)#45通知
                                pgtime = datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJPG)
                                espgtime=datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=ESPG)
                            elif b == "特后前":
                                bztime=datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJZBZ)
                                esbztime = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=ESZBZ)
                                bztztime=datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJGR)
                                estzbztime = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=ESGR)
                                pgtime = datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJPG)
                                espgtime=datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=ESPG)
                            else:
                                bztime=datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJZBZ)
                                esbztime=""
                                bztztime=datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJGR)
                                estzbztime=""
                                pgtime = datetime.datetime.strptime(eta, "%Y-%m-%d %H:%M:%S")-datetime.timedelta(minutes=JJPG)
                                espgtime=""
                        except Exception as e:
                            app_logger.log_error(e)
                            pass
                        try:
                            if (wxry != "" or fxry != "") and bztztime < now and judgeFunction(singlelogsall1,"航前或接机任务接送通知",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                peoples = ""
                                res = wxry.split(",") + fxry.split(",")
                                for l in res:
                                    if "√" not in l:
                                        peoples=peoples+","+l if peoples != "" else l

                                if peoples != "":
                                    getpeopleinfo(peoples,peoplesinfo_today,peoplesinfo_yes)
                                    print(datetime.datetime.now(),"航前或接机任务接送通知")
                                    noteInsert(database,i[0],"%s/%s/%s航前或接机任务接送通知"%(a,b,c),"航前或接机任务接送通知")
                        except Exception as e:
                            app_logger.log_error(e)
                            pass
                        try:

                            if (wxry != "" or fxry != "") and bztime < now:
                                res = wxry.split(",")+fxry.split(",")
                                for l in res:
                                    if "√" not in l:
                                        mesg = "请确认%s/%s/%s/%s任务接收情况!" % (a, b, c, l)
                                        if mesg not in str(singlelogsall):
                                            WarningInsert(database, i[0], mesg, "任务接收监控")
                                    else:
                                        mesg = "请确认%s/%s/%s/%s任务接收情况!" % (a, b, c, l.replace("√",""))
                                        if mesg in str(singlelogstext):
                                            newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                            database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))
                        except Exception as e:
                            app_logger.log_error(e)
                            pass

                        try:

                            if ecsj != "" and estzbztime != "" and estzbztime < now and judgeFunction(singlelogsall1,"航前或接机任务接送通知",2) and singlePeopleNotice and "【交班航班】" not in fj:
                                peoples = ""
                                res = ecsj.split(",")
                                for l in res:
                                    if "√" not in l:
                                        peoples = peoples + "," + l if peoples != "" else l
                                if peoples != "":
                                    getpeopleinfo(peoples, peoplesinfo_today, peoplesinfo_yes)
                                    print(datetime.datetime.now(), "二送任务接送通知")
                                    noteInsert(database, i[0], "%s/%s/%s二送任务接送通知" % (a, b, c), "二送任务接送通知")
                        except Exception as e:
                            app_logger.log_error(e)
                            pass
                        try:
                            if ecsj != "" and esbztime != "" and esbztime < now: #and "任务执行情况" not in str(singlelogsall):
                                res = ecsj.split(",")
                                for l in res:
                                    if "√" not in l:
                                        mesg = "请确认%s/%s/%s/%s二送任务接收情况!" % (a, b, c, l)
                                        if mesg not in str(singlelogsall):
                                            WarningInsert(database, i[0], mesg, "任务接收监控")
                                    else:
                                        mesg = "请确认%s/%s/%s/%s二送任务接收情况!" % (a, b, c, l.replace("√",""))
                                        if mesg in str(singlelogstext):
                                            newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                            database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))
                        except Exception as e:
                            app_logger.log_error(e)
                            pass
                        try:
                            if "System" not in str(i[0]) and wxry == "" and pgtime < now:
                                mesg = "请及时安排%s/%s/%s保障人员并派工!" % (a, b, c)
                                if mesg not in str(singlelogsall):
                                    WarningInsert(database, i[0], mesg, "任务安排监控")
                            elif wxry != "":
                                mesg = "请及时安排%s/%s/%s保障人员并派工!" % (a, b, c)
                                if mesg in str(singlelogstext):
                                    newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                    database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))
                        except Exception as e:
                            app_logger.log_error(e)
                            pass
                        try:
                            if "System" not in str(i[0]) and fxry == "" and pgtime < now:
                                mesg = "请及时安排%s/%s/%s放行人员并派工!" % (a, b, c)
                                if mesg not in str(singlelogsall):
                                    WarningInsert(database, i[0], mesg, "任务安排监控")
                            elif fxry != "":
                                mesg = "请及时安排%s/%s/%s放行人员并派工!" % (a, b, c)
                                if mesg in str(singlelogstext):
                                    newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                    database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))
                        except Exception as e:
                            app_logger.log_error(e)
                            pass
                        try:
                            if "System" not in str(i[0]) and ecsj == "" and b != "航前" and espgtime != "" and espgtime < now:
                                mesg = "请及时安排%s/%s/%s二送保障人员并派工!" % (a, b, c)
                                if mesg not in str(singlelogsall):
                                    WarningInsert(database, i[0], mesg, "任务安排监控")
                            elif ecsj != "" and b != "航前" :
                                mesg = "请及时安排%s/%s/%s二送保障人员并派工!" % (a, b, c)
                                if mesg in str(singlelogstext):
                                    newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                                    database.lazyUpdateItem('logs', newdic, "航班编号='%s' and 警告信息='%s'" % (i[0],mesg))
                        except Exception as e:
                            app_logger.log_error(e)
                            pass



                for i in flightcheck2:
                    data2 = baseFunction.TuplefindInList1(datas, i[0], 0)
                    if data2 and data2[0][3] != "停场":
                        a = data2[0][1]
                        b = get_dic("taskType")[data2[0][3]]
                        std= data2[0][5]
                        fj=baseFunction.TuplefindInList3(peopleschedule, i[0], 1, 9)
                        sjry_str = data2[0][15]
                        flightno = data2[0][21]
                        singlelogsall=baseFunction.TuplefindInList33(logsall, i[0], 1, 2) #所有警告信息
                        singlelogs=baseFunction.TuplefindInList33(logs, i[0], 1, 2) #未确认所有警告信息
                        sjdw=baseFunction.TuplefindInList3(flightsts, i[0], 0, 6)
                        if b == "航前":
                            c = data2[0][11]
                        else:
                            c = data2[0][10]
                        try:
                            if b=="航前" and std != "":
                                STD = std
                                STD_120 = datetime.datetime.strptime(STD, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes= 120)
                                STD_60 = datetime.datetime.strptime(STD, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes= 60)

                                if "随机离港" in str(fj) and "随机2H提醒" not in str(singlelogsall) and STD_120 < now:
                                    sjry = sjry_str.split(" ")[1]
                                    WarningInsert(database, i[0], "[随机2H提醒]%s/%s/%s/%s"% (a, flightno, c, sjry),"随机2H提醒")
                                if "随机离港" in str(fj) and "随机到位确认" not in str(singlelogs) and STD_60 < now and "已到位" not in str(sjdw):
                                    sjry = sjry_str.split("随机离港 ")[1]
                                    WarningInsert(database, i[0], "[随机到位确认]%s/%s/%s/%s"% (a, flightno, c, sjry),"随机到位警告")

                            elif b=="短停" and std != "":
                                TD = std
                                TD_time_120 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=120)
                                TD_time_60 = datetime.datetime.strptime(TD, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=60)
                                if "随机离港" in str(fj) and "随机2H提醒" not in str(singlelogsall) and TD_time_120 < now and sjry_str != "":
                                    sjry = sjry_str.split(" ")[1]
                                    WarningInsert(database,i[0],"[随机2H提醒]%s/%s/%s/%s"%(a,flightno,c,sjry),"随机2H提醒")
                                if "随机离港" in str(fj) and "随机到位确认" not in str(singlelogs) and TD_time_60 < now and "已到位" not in str(sjdw):
                                    sjry = sjry_str.split(" ")[1]
                                    WarningInsert(database,i[0], "[随机到位确认]%s/%s/%s/%s"%(a,flightno,c,sjry),"随机到位警告")


                            elif b != "短停" and b != "航后" and b != "航前" and b != "未显示" and b != "停场" :
                                TD_time_120= datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=120)
                                TD_time_60 = datetime.datetime.strptime(std, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=60)
                                if "随机离港" in str(fj) and "随机2H提醒" not in str(singlelogsall) and TD_time_120 < now:
                                    sjry=sjry_str.split(" ")[1]
                                    WarningInsert(database,i[0],"[随机2H提醒]%s/%s/%s/%s"%(a,flightno,c,sjry),"随机2H提醒")
                                if "随机离港" in str(fj) and "随机到位确认" not in str(singlelogs) and TD_time_60 < now and "已到位" not in str(sjdw):
                                    sjry=sjry_str.split("随机离港 ")[1]
                                    WarningInsert(database,i[0],"[随机到位确认]%s/%s/%s/%s"%(a,flightno,c,sjry),"随机到位警告")
                        except Exception as e:
                            app_logger.log_error(e)
                            pass

                database.FunctionCommit()

            waringIDs=database.queryTabel('logs',"*","变更字段 like '%警告%' and 处理时间 =''")
            flightstsids=database.getSingledata("航班编号",'flightsts{}'.format(nowDayStr))
            for waringID in waringIDs:
                if str(waringID[1]) in str(flightstsids):
                    waringIDstsNow=database.queryTabel('flightsts{}'.format(nowDayStr),"*","航班编号 ='%s'"%waringID[1])
                    if waringID[8] == "到位警告" and waringIDstsNow[0][3] == "已到位":
                        newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                        database.lazyUpdateItem('logs', newdic, "编号='%s'" % waringID[0])
                    if waringID[8] == "放行警告" and waringIDstsNow[0][4] == "已放行":
                        newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                        database.lazyUpdateItem('logs', newdic, "编号='%s'" % waringID[0])
                    if waringID[8] == "二送警告" and waringIDstsNow[0][7] == "已到位":
                        newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                        database.lazyUpdateItem('logs', newdic, "编号='%s'" % waringID[0])
                    if waringID[8] == "销夹警告" and waringIDstsNow[0][5] == "已取下":
                        newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                        database.lazyUpdateItem('logs', newdic, "编号='%s'" % waringID[0])
                    if waringID[8] == "随机到位警告" and waringIDstsNow[0][6] == "已到位":
                        newdic = {"处理人": "'系统确认状态'", "处理时间": "'%s'" % now}
                        database.lazyUpdateItem('logs', newdic, "编号='%s'" % waringID[0])
        database.FunctionCommit()
    except Exception as e:
        database.FunctionCommit()
        app_logger.log_error(e)