from Functions.DButilsFunction import flightDB
from unitls.LogerinTxt import app_logger
from unitls.StaticDataclass import get_dic

def initFlightDatabase(database:flightDB,workLoad:flightDB, datatime:str):
    try:
        fdb = database
        sortDic={}
        logsDic = {}
        workerinfoDic = {}
        authorityDic = {}
        flightstsDic = {}
        TaskFlightinfoDic={}
        pglistDic={}
        peopleScheduleDic={}
        taskDic={}
        CalllistDic={}
        workjobDic = {}
        displayDic={}
        riskDic={}
        flighsearchDic = {}
        tasklistDic = {}
        workloadDic = {}
        flightplanflightinfoDic = {}
        primaryKey = 'SERIAL PRIMARY KEY'
        primaryKey3 ='TEXT PRIMARY KEY NOT NULL'


        for sortkey in list(get_dic("sortLabel"))[1:]:
            sortDic[sortkey]= 'text not null'
        for logskey in list(get_dic("logsLabel"))[1:]:
            logsDic[logskey]= 'text not null'
        for workerinfokey in list(get_dic("workerinfoLabel"))[1:]:
            workerinfoDic[workerinfokey]= 'text not null'
        for authoritykey in list(get_dic("authorityLabel"))[1:]:
            authorityDic[authoritykey]= 'text not null'
        for flightstskey in list(get_dic('flightstsLabel'))[1:]:
            flightstsDic[flightstskey]= 'text not null'
        for flightstskey in list(get_dic('TaskflightinfoLabel'))[1:]:
            TaskFlightinfoDic[flightstskey]= 'text not null'
        for pglistkey in list(get_dic("pglistLabel"))[1:]:
            pglistDic[pglistkey] = 'text not null'
        for peopleScheduleKey in list(get_dic("peopleScheduleLabel"))[1:]:
            peopleScheduleDic[peopleScheduleKey] = 'text not null'
        for CalllistKey in list(get_dic("CalllistLabel"))[1:]:
            if CalllistKey == '拨号次数':
                CalllistDic[CalllistKey] = 'int'
            else:
                CalllistDic[CalllistKey] = 'text not null'
        for taskkey in list(get_dic("taskLabel"))[1:]:
            taskDic[taskkey]= 'text not null'
        for workjobkey in list(get_dic("workjobkeyLabel"))[1:]:
            workjobDic[workjobkey]= 'text not null'
        for displaykey in list(get_dic("displayLabel"))[1:]:
            displayDic[displaykey]= 'text not null'
        for riskkey in list(get_dic("RiskLabel"))[1:]:
            riskDic[riskkey]= 'text not null'
        for flightkey in list(get_dic("flightsearchLabel"))[1:]:
            flighsearchDic[flightkey]= 'text not null'
        for tasklistkey in list(get_dic("TaskListLabel"))[1:]:
            tasklistDic[tasklistkey]= 'text not null'
        for workloadkey in list(get_dic("newWorkloadLabel"))[1:]:
            workloadDic[workloadkey] = 'text not null'
        for flightplankey in list(get_dic("flightplanflightinfoLabel"))[1:]:
            flightplanflightinfoDic[flightplankey] = 'text not null'

        nowDayStr = datatime
        fdb.initTable('pglist{}'.format(nowDayStr),"姓名", primaryKey3,pglistDic)
        fdb.initTable('sortFlight2{}'.format(nowDayStr), "编号", primaryKey3, sortDic)
        fdb.initTable('sortFlight{}'.format(nowDayStr), "编号",primaryKey3, sortDic)
        fdb.initTable('logs', "编号",primaryKey, logsDic)
        fdb.initTable('taskAuto', "编号", primaryKey3, taskDic)
        fdb.initTable('workerinfo',"工号", primaryKey3, workerinfoDic)
        fdb.initTable('authority',"编号", primaryKey, authorityDic)
        fdb.initTable('flightsts{}'.format(nowDayStr),"航班编号", primaryKey3, flightstsDic)
        fdb.initTable('TaskFlightinfo{}'.format(nowDayStr), "航班编号", primaryKey3, TaskFlightinfoDic)
        fdb.initTable('FlightSearch{}'.format(nowDayStr), "FLIGHT_ID", primaryKey3, flighsearchDic)
        fdb.initTable('peopleSchedule{}'.format(nowDayStr), "编号", primaryKey3, peopleScheduleDic)
        fdb.initTable('Calllist', "航班编号", primaryKey3, CalllistDic)
        fdb.initTable('workjob', "任务编号", primaryKey3, workjobDic)
        fdb.initTable('display', "ID", primaryKey3, displayDic)
        fdb.initTable('RiskTable', "风险编号", primaryKey3, riskDic)
        fdb.initTable('TaskList{}'.format(nowDayStr), "TASKID", primaryKey3, tasklistDic)
        fdb.initTable('flightplaninfo{}'.format(nowDayStr), "flightId", primaryKey3, flightplanflightinfoDic)
        workLoad.initTable('Workload{}'.format(nowDayStr), "编号", primaryKey3, workloadDic)


        if len(fdb.getSingledata("A",'display')) == 0:
            fdb.insertData('display', {'ID': 1, 'A': '{}','B': '{}'})
            fdb.insertData('display', {'ID': 2, 'A': '{}','B': '{}'})
            fdb.insertData('display', {'ID': 3, 'A': '{}','B': '{}'})


    except Exception as e:
        app_logger.log_error(e)