import datetime

from unitls.LogerinTxt import app_logger
from .flightDB import flightDB


def calledChecked(fdb: flightDB):
    try:
        now=datetime.datetime.now()
        now_5 = now - datetime.timedelta(minutes=8)
        callist = fdb.queryTabel("Calllist", "*", "接通时间 ='' and (创建时间<'%s' or CAST(拨号次数 AS integer)>3)"%now_5)
        if len(callist) != 0:
            for i in callist:
                if len(fdb.queryTabel("logs", "航班编号", "航班编号 ='%s' and 处理时间=''"%i[0])) == 0:
                    newdic = {"航班编号": "%s" %i[0], "警告信息": "%s %s超过自动联系超过8min或连续拨号超过3次无响应，请人工处理！！"%(i[4],i[3]), "信息状态": "", "提示信息": "",
                              "产生时间": "%s" % now, "处理人": "", "处理时间": "", "变更字段": "无法联系", "显示对象": "", "提示内容": ""}
                    fdb.lazyInsertData('logs', newdic)
                newdic1={"接通时间":"'%s推送人工处理'"%now}
                fdb.lazyUpdateItem("Calllist",newdic1,"航班编号='%s'"%i[0])
            fdb.FunctionCommit()
    except Exception as e:
        fdb.FunctionCommit()
        app_logger.log_error(e)