import datetime

from unitls.LogerinTxt import app_logger
from .flightDB import flightDB


def manChangests(database:flightDB,flight_id:str,changests_id:str,selectedtime,username):
    try:
        nowDayStr = selectedtime
        now = datetime.datetime.now()
        now11=str(now).split(".")[0]
        code = {"接机到位": "人工已到位", "放行": "人工已放行", "随机到位": "人工已到位", "销子夹板": "人工已取下", "二送到位": "人工已到位"}
        code2 = {"接机到位": "接机已到位提示", "放行": "已放行提示", "随机到位": "随机已到位提示", "销子夹板": "销子夹板已提示", "二送到位": "二送已到位提示"}
        flightsts_search=database.queryTabel("flightsts{}".format(nowDayStr),"%s"%changests_id,"航班编号 = '%s'"%flight_id)
        waringsts=database.queryTabel("logs","*","航班编号='%s' and 处理时间 =''"%flight_id)
        if len(flightsts_search) != 0:
            if flightsts_search[0][0] != "":
                newdic={"%s"%changests_id:"''","%s"%code2[changests_id]:"''"}
                database.upDateItem("flightsts{}".format(nowDayStr),newdic,"航班编号='%s'"%flight_id)
                a =database.queryTabel('flightsts{}'.format(nowDayStr), "机号", "航班编号='%s'" % flight_id)[0][0] if len(database.queryTabel('flightsts{}'.format(nowDayStr), "机号", "航班编号='%s'" % flight_id)) != 0 else "未显示机号"
                newdic = {"航班编号": "%s" % flight_id, "警告信息": "", "信息状态": "",
                          "提示信息": "人工处理:%s取消%s状态!%s" % (a,changests_id,now11),
                          "产生时间": "%s" % now, "处理人": "%s"%username, "处理时间": "%s" % now, "变更字段": "","显示对象":"","提示内容":""}
                database.insertData('logs', newdic)

            else:
                newdic={"%s"%changests_id:"'%s'"%code[changests_id],"%s"%code2[changests_id]:"'1'"}
                database.upDateItem("flightsts{}".format(nowDayStr),newdic,"航班编号 = '%s'"%flight_id)
                a = database.queryTabel('flightsts{}'.format(nowDayStr), "机号", "航班编号='%s'" % flight_id)[0][0] if len(database.queryTabel('flightsts{}'.format(nowDayStr), "机号", "航班编号='%s'" % flight_id)) != 0 else "未显示机号"
                newdic = {"航班编号": "%s" % flight_id, "警告信息": "", "信息状态": "",
                          "提示信息": "人工处理:%s确认%s已完成!%s" % (a, changests_id,now11),
                          "产生时间": "%s" % now, "处理人": "%s"%username, "处理时间": "%s" % now, "变更字段": "","显示对象":"","提示内容":""}
                database.insertData('logs', newdic)
        else:
            newdic = {"航班编号": "%s" % flight_id, "%s"%changests_id:"%s"%code[changests_id],"%s"%code2[changests_id]:"1"}
            for i in ["航班编号","接机到位","放行","销子夹板","随机到位","二送到位","接机已到位提示","已放行提示","销子夹板已提示","随机已到位提示","二送已到位提示","天府飞机","机号","机位"]:
                if i not in newdic.keys():
                    newdic[i]=''

            database.insertData('flightsts{}'.format(nowDayStr), newdic)
            newdic = {"航班编号": "%s" % flight_id, "警告信息": "", "信息状态": "","提示信息": "人工处理[首次新建]:%s确认%s已完成!%s" % (flight_id, changests_id, now11),
                      "产生时间": "%s" % now, "处理人": "%s" % username, "处理时间": "%s" % now, "变更字段": "", "显示对象": "","提示内容": ""}
            database.insertData('logs', newdic)

        if len(waringsts) != 0:
                for i in waringsts:
                    if changests_id == "接机到位" and i[8]=="到位警告":
                        newdic = {"处理人": "'%s'" % username, "处理时间": "'%s'" % now}
                        database.upDateItem('logs', newdic, "编号='%s'" % i[0])
                    elif changests_id == "放行" and i[8]=="放行警告":
                        newdic = {"处理人": "'%s'" % username, "处理时间": "'%s'" % now}
                        database.upDateItem('logs', newdic, "编号='%s'" % i[0])
                    elif changests_id == "二送到位" and i[8]=="二送警告":
                        newdic = {"处理人": "'%s'" % username, "处理时间": "'%s'" % now}
                        database.upDateItem('logs', newdic, "编号='%s'" % i[0])
                    elif changests_id == "销子夹板" and i[8]=="销夹警告":
                        newdic = {"处理人": "'%s'" % username, "处理时间": "'%s'" % now}
                        database.upDateItem('logs', newdic, "编号='%s'" % i[0])
                    elif changests_id == "随机到位" and i[8] == "随机到位警告":
                        newdic = {"处理人": "'%s'" % username, "处理时间": "'%s'" % now}
                        database.upDateItem('logs', newdic, "编号='%s'" % i[0])
    except Exception as e:
        app_logger.log_error(e)