from unitls.settings import DBServer, flightDB4
host, port, user1, password,online_host, online_port, online_user,online_password = DBServer()
databaseflightDB4=flightDB4()
from .flightDB import flightDB
import datetime
from .sortFlighttime import sortFlighttime
from .sortFlighttime2 import sortFlighttime2
def manAddFlight(fdb:flightDB,flighttype,acno,eng,actype,flightno,flightdate,bay,eta,etd,user):
    tasktype={"短停":"TR", "特后前":"TAF", "航前":"AP", "航后":"AF"}
    starttime = flightdate + " 00:00:00"
    flightdate1 = datetime.datetime.strptime(starttime, "%Y-%m-%d %H:%M:%S")
    flightdate2 =flightdate.replace("-", "")
    flightids=fdb.getSingledata("航班编号","TaskFlightinfo%s"%flightdate2)
    ETD2=etd
    flightid = "MAN" + str(acno) + str(flightno)+str(flightdate2)
    if flightid not in str(flightids):
        if flighttype =="航前":
            newdic={"航班编号": "%s" % flightid,"机号":acno,"航班日期":flightdate1,"航班类型":tasktype[flighttype],"非航前计划到达":"","航前预计起飞":ETD2,"非航前实际到达":"",
                "实际起飞":"","非航前预计到达":"","预飞预达":"","非航前进港机位":"","航前离港机位":bay,"维修人员":"","放行人员":"","维修状态":"",
                "随机":"","保留":"","维修状态时间":"","工作包":"","二次送机":"","二送状态":"",
                "进出港航班号":flightno,"附加工作状态":"","进出港城市":"","APU型号":"","发动机":eng,"机型":actype,"机型综合信息":"","详细机型":"",
                "高高原":"","上段起飞时间":datetime.datetime.now(),"后续类型":"","后续预计起飞":"","航班来源":"人工添加","备降返航":"",
                "取消标志":"","备1":"","备2":"","备3":"","备4":"","备5":"","备6":"","备7":"","备8":"","备9":"","备10":""}
        elif flighttype =="短停" or flighttype =="特后前":
            newdic={"航班编号": "%s" % flightid,"机号":acno,"航班日期":flightdate1,"航班类型":tasktype[flighttype],"非航前计划到达":"","航前预计起飞":ETD2,"非航前实际到达":eta,
                "实际起飞":"","非航前预计到达":eta,"预飞预达":"","非航前进港机位":bay,"航前离港机位":"","维修人员":"","放行人员":"","维修状态":"",
                "随机":"","保留":"","维修状态时间":"","工作包":"","二次送机":"","二送状态":"",
                "进出港航班号":flightno,"附加工作状态":"","进出港城市":"","APU型号":"","发动机":eng,"机型":actype,"机型综合信息":"","详细机型":"",
                "高高原":"","上段起飞时间":datetime.datetime.now(),"后续类型":"","后续预计起飞":"","航班来源":"人工添加","备降返航":"",
                "取消标志":"","备1":"","备2":"","备3":"","备4":"","备5":"","备6":"","备7":"","备8":"","备9":"","备10":""}


        else:
            newdic={"航班编号": "%s" % flightid,"机号":acno,"航班日期":flightdate1,"航班类型":tasktype[flighttype],"非航前计划到达":"","航前预计起飞":"","非航前实际到达":eta,
                "实际起飞":"","非航前预计到达":eta,"预飞预达":"","非航前进港机位":bay,"航前离港机位":"","维修人员":"","放行人员":"","维修状态":"",
                "随机":"","保留":"","维修状态时间":"","工作包":"","二次送机":"","二送状态":"",
                "进出港航班号":flightno,"附加工作状态":"","进出港城市":"","APU型号":"","发动机":eng,"机型":actype,"机型综合信息":"","详细机型":"",
                "高高原":"","上段起飞时间":datetime.datetime.now(),"后续类型":"","后续预计起飞":"","航班来源":"人工添加","备降返航":"",
                "取消标志":"","备1":"","备2":"","备3":"","备4":"","备5":"","备6":"","备7":"","备8":"","备9":"","备10":""}
        fdb.insertData("TaskFlightinfo%s"%flightdate2,newdic)
    else:
        if flighttype =="航前":
            newdic={"航班编号":"'%s'"%flightid,"机号":"'%s'"%acno,"发动机":"'%s'"%eng, "机型":"'%s'"%actype, "进出港航班号":"'%s'"%flightno, "航班日期":"'%s'"%flightdate1,"航班类型":"'%s'"%tasktype[flighttype], "航前预计起飞":"'%s'"%etd, "航前离港机位":"'%s'"%bay}
        elif flighttype =="短停" or flighttype =="特后前":
            newdic = {"航班编号": "'%s'" % flightid, "机号":"'%s'"% acno, "发动机":"'%s'"% eng, "机型":"'%s'"% actype, "进出港航班号":"'%s'"% flightno , "航班日期":"'%s'"% flightdate1, "航班类型":"'%s'"% tasktype[flighttype],"非航前预计到达":"'%s'"% eta,"非航前实际到达": "'%s'"% eta, "上段起飞时间": "'%s'"% eta, "航前预计起飞":"'%s'"% ETD2, "非航前进港机位":"'%s'"% bay}
        else:
            newdic = {"航班编号": "'%s'" % flightid,"机号":"'%s'"% acno, "发动机":"'%s'"% eng, "机型":"'%s'"% actype, "进出港航班号":"'%s'"% flightno, "航班日期":"'%s'"% flightdate1, "航班类型":"'%s'"% tasktype[flighttype], "非航前预计到达":"'%s'"% eta,"上段起飞时间": "'%s'"% eta, "非航前进港机位":"'%s'"% bay}
        fdb.upDateItem("TaskFlightinfo%s"%flightdate2,newdic,"航班编号='%s'"%flightid)
    sortFlighttime(flightDB(host=host,
                    port=port,
                    user=user1,
                    password=password,
                    database=databaseflightDB4
                   ))
    sortFlighttime2(flightDB(host=host,
                    port=port,
                    user=user1,
                    password=password,
                    database=databaseflightDB4
                   ))
    res={"返回值":"ok"}
    return res