import datetime

from unitls.LogerinTxt import app_logger
from .flightDB import flightDB


def taskAtuoCheck(fdb:flightDB,time):
    try:
        now = datetime.datetime.now()
        tomorrow=(datetime.date.today() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
        #print(tomorrow)
        if time =="1":
            print(datetime.datetime.now(),"[数据库操作]准备组任务首次登录检查验证")
            log_str = fdb.queryTabel('logs',"编号","处理时间='' and 变更字段='准备组任务'")
            if  len(log_str) == 0:
                getTaskAuto=fdb.getAlldata('taskAuto')
                if len(getTaskAuto) != 0:
                    #print(getTaskAuto)
                    for i in getTaskAuto:
                        #print(i)
                        if i[1] !="None":

                            time1=str(datetime.date.today())+ " "+i[1]
                            time11 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
                            time2 = str(datetime.date.today()+ datetime.timedelta(days=1)) + " " + i[1]
                            time22 = datetime.datetime.strptime(time2, "%Y-%m-%d %H:%M:%S")

                            newdic = {"航班编号": '', "警告信息": i[2], "信息状态": "",
                                      "提示信息": "%s" % time11, "产生时间": "%s" % now, "处理人": "", "处理时间": "", "变更字段": "准备组任务", "显示对象": "",
                                      "提示内容": i[3]}
                            fdb.lazyInsertData("logs", newdic)
                            newdic2 = {"航班编号": '', "警告信息": i[2], "信息状态": "",
                                      "提示信息":  "%s" % time22, "产生时间": "%s" % now, "处理人": "", "处理时间": "", "变更字段": "准备组任务", "显示对象": "",
                                      "提示内容": i[3]}
                            fdb.lazyInsertData("logs", newdic2)
        if time =="2":
            print(datetime.datetime.now(),"[数据库操作]准备组任务登录持续验证")
            log_str2 = fdb.queryTabel('logs', "编号", "提示信息 >'%s' and 处理时间='' and 变更字段='准备组任务'" % tomorrow)
            if len(log_str2) == 0:
                getTaskAuto = fdb.getAlldata('taskAuto')
                if len(getTaskAuto) != 0:
                    #print(getTaskAuto)
                    for i in getTaskAuto:
                        #print(i)
                        if i[1] != "None":
                            time2 = str(datetime.date.today() + datetime.timedelta(days=1)) + " " + i[1]
                            time22 = datetime.datetime.strptime(time2, "%Y-%m-%d %H:%M:%S")
                            newdic2 = {"航班编号": '', "警告信息": i[2], "信息状态": "",
                                       "提示信息": "%s" % time22, "产生时间": "%s" % now, "处理人": "", "处理时间": "", "变更字段": "准备组任务",
                                       "显示对象": "",
                                       "提示内容": i[3]}
                            fdb.lazyInsertData("logs", newdic2)
        if time =="3":
            print(datetime.datetime.now(),"[数据库操作]更新准备组任务抑制前序任务")
            log_str3 = fdb.queryTabel('logs', "编号", "处理时间='' and 变更字段='准备组任务'")
            #print(log_str3)
            if len(log_str3) != 0:
                for i in log_str3:
                    newdic = {"处理人": "'更新准备组任务系统抑制'", "处理时间": "'%s'" % now}
                    fdb.lazyUpdateItem('logs', newdic, "编号='%s'" % i[0])

            getTaskAuto = fdb.getAlldata('taskAuto')
            if len(getTaskAuto) != 0:
                #print(getTaskAuto)
                for i in getTaskAuto:
                    #print(i)
                    if i[1] != "None":
                        time1 = str(datetime.date.today()) + " " + i[1]
                        time11 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
                        time2 = str(datetime.date.today() + datetime.timedelta(days=1)) + " " + i[1]
                        time22 = datetime.datetime.strptime(time2, "%Y-%m-%d %H:%M:%S")

                        newdic = {"航班编号": '', "警告信息": i[2], "信息状态": "",
                                  "提示信息": "%s" % time11, "产生时间": "%s" % now, "处理人": "", "处理时间": "", "变更字段": "准备组任务",
                                  "显示对象": "",
                                  "提示内容": i[3]}
                        fdb.lazyInsertData("logs", newdic)
                        newdic2 = {"航班编号": '', "警告信息": i[2], "信息状态": "",
                                   "提示信息": "%s" % time22, "产生时间": "%s" % now, "处理人": "", "处理时间": "", "变更字段": "准备组任务",
                                   "显示对象": "",
                                   "提示内容": i[3]}
                        fdb.lazyInsertData("logs", newdic2)
        fdb.FunctionCommit()
    except Exception as e:
        fdb.FunctionCommit()
        app_logger.log_error(e)