import datetime
from openpyxl import Workbook
from unitls.LogerinTxt import app_logger
from unitls.StaticDataclass import get_dic
from unitls.baseFunction import TuplefindInList1
from .flightDB import flightDB
from .getSortFlightdata import getSortFlightdata


def bakupdata(fdb :flightDB):
    try:
        # print("5分钟间隔自动备份")
        nowDay = str(datetime.date.today().strftime("%Y%m%d"))
        nowDay_1 = str((datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d"))
        aaa ={nowDay :"今天" ,nowDay_1 :"昨天"}
        tableWidgetDispalydatas ={}
        wb = Workbook()
        for selectedtime in [nowDay ,nowDay_1]:
            wb.create_sheet(title="%s " %selectedtime, index=0)
            ws = wb.active
            ws.title = "%s " %selectedtime
            ws= wb["%s " %selectedtime]
            ws.cell(1, 1).value = "序号"
            ws.cell(1, 2).value = "类型"
            ws.cell(1, 3).value = "航班号"
            ws.cell(1, 4).value = "机型"
            ws.cell(1, 5).value = "发动机"
            ws.cell(1, 6).value = "机号"
            ws.cell(1, 7).value = "计达"
            ws.cell(1, 8).value = "预达"
            ws.cell(1, 9).value = "计飞"
            ws.cell(1, 10).value = "机位"
            ws.cell(1, 11).value = "放行"
            ws.cell(1, 12).value = "工1"
            ws.cell(1, 13).value = "工2"
            ws.cell(1, 14).value = "工3"
            ws.cell(1, 15).value = "工4"
            ws.cell(1, 16).value = "工5"
            ws.cell(1, 17).value = "送1"
            ws.cell(1, 18).value = "送2"
            ws.cell(1, 19).value = "备注"
            ws.cell(1, 20).value = "接机到位"
            ws.cell(1, 21).value = "二送到位"
            ws.cell(1, 22).value = "放行情况"
            ws.cell(1, 23).value = "销子夹板"
            tableWidgetDispalydata = {}
            sorttable = getSortFlightdata(fdb, selectedtime, "sortFlight2")
            nowDayStr = selectedtime
            '''
            col_dic = {"0": "序号", "1": "机号", "2": "机型", "3": "发动机", "4": "航班号", "5": "计达", "6": "预达", "7": "计飞", "8": "机位",
                       "9": "类型", "10": "放行", "11": "勤务1", "12": "勤务2", "13": "工1", "14": "工2", "15": "工3", "16": "到位",
                       "18": "放行","19": "销夹", "17": "二送", "20": "随机", "21": "随机到位", "22": "保留", "23": "工作包", "24": "返航备降", "25": "维修人员",
                       "26": "二送人员", "27": "放行", "28": "航班编号", "29": "任务编号", "30": "TATD", "31": "类型","32":"附加消息"}

            col_dic = {"0": "序号", "1": "机号", "2": "机型", "3": "发动机", "4": "航班号", "5": "计达", "6": "预达", "7": "计飞", "8": "机位",
                       "9": "类型", "10": "放行", "11": "勤务1", "12": "勤务2", "13": "工1", "14": "工2", "15": "工3", "16": "到位",
                       "18": "放行","19": "销夹", "17": "二送","20":"附加消息","21": "送机1", "22": "送机2"}
            '''
            rowNum = 0

            datas = fdb.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '*', "航班编号!=''")
            taskType = get_dic('taskType')
            
            for row in sorttable:
                TaskFlightinfo=TuplefindInList1(datas,row[1],0)[0]
                flightstsdata = fdb.queryTabel('flightsts{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))[0] if len \
                    (fdb.queryTabel('flightsts{}'.format(nowDayStr), '*', "航班编号='{}'".format(row[1]))) != 0 else ["", ""
                                                                                                                 ,"", "", "" ,"", "", ""
                                                                                                                 ,"", "", "" ,"", "", ""]
                jjdw = "√" if flightstsdata[3] != "" else ""
                FX = "√" if flightstsdata[4] != "" else ""
                XJ = "√" if flightstsdata[5] != "" else ""
                ESDW = "√" if flightstsdata[7] != "" else ""

                flighttype =taskType[TaskFlightinfo[3]] if "3U" in str(TaskFlightinfo[21]) or "CSC"  in str(TaskFlightinfo[21]) else "外 " +str(taskType[TaskFlightinfo[3]])

                if TaskFlightinfo[4] != "" and (TaskFlightinfo[8] == "" or TaskFlightinfo[30] == ""):  # 如果sta不为空，且eta或flying为空，显示sta
                    a = TaskFlightinfo[4][-8:-3].replace(":","")
                else:
                    a = ""
                if TaskFlightinfo[6] != "":  # 当ata不为空显示ata，eta不为空显示eta否则为空
                    b = TaskFlightinfo[6][-8:-3].replace(":","")
                elif TaskFlightinfo[8] != "":
                    b = TaskFlightinfo[8][-8:-3].replace(":","")
                else:
                    b = ''
                if TaskFlightinfo[7] != "":
                    c =TaskFlightinfo[7][-8:-3].replace(":","")
                elif TaskFlightinfo[5] != "":
                    c =TaskFlightinfo[5][-8:-3].replace(":","")
                else:
                    c = ''

                fx = ""
                g1 = ""
                g2 = ""
                g3 = ""
                g4 = ""
                g5 = ""
                fj1 = ""
                s1 = ""
                s2 = ""
                fj2 = ""
                fj =""
                peopleSchedules = fdb.queryTabel('peopleSchedule{}'.format(nowDayStr), '*',
                                                 "航班编号='{}'".format(row[1])) if \
                    len(fdb.queryTabel('peopleSchedule{}'.format(nowDayStr), '*',
                                       "航班编号='{}'".format(row[1]))) != 0 else [
                    ("000", "", "", "", "", "", "", "", "", "")]
                # print(peopleSchedules)
                for i in peopleSchedules:
                    if i[0][-3] == "1":
                        fx = i[3]
                        g1 = i[4]
                        g2 = i[5]
                        g3 = i[6]
                        g4 = i[7]
                        g5 = i[8]
                        fj1 = i[9]
                    elif i[0][-3] == "2":
                        s1 = i[7]
                        s2 = i[8]
                        fj2 = i[9]
                    fj = fj1 + fj2

                if taskType[TaskFlightinfo[3]] == "航后":
                    ws.cell(rowNum + 2, 1).value = rowNum + 1
                    ws.cell(rowNum + 2, 2).value = flighttype
                    ws.cell(rowNum + 2, 3).value = TaskFlightinfo[21]
                    ws.cell(rowNum + 2, 4).value = TaskFlightinfo[26]
                    ws.cell(rowNum + 2, 5).value = TaskFlightinfo[25]
                    ws.cell(rowNum + 2, 6).value = TaskFlightinfo[1]
                    ws.cell(rowNum + 2, 7).value = a
                    ws.cell(rowNum + 2, 8).value = b
                    ws.cell(rowNum + 2, 9).value = ""
                    ws.cell(rowNum + 2, 10).value = TaskFlightinfo[10]
                    ws.cell(rowNum + 2, 11).value = fx
                    ws.cell(rowNum + 2, 12).value = g1
                    ws.cell(rowNum + 2, 13).value = g2
                    ws.cell(rowNum + 2, 14).value = g3
                    ws.cell(rowNum + 2, 15).value = g4
                    ws.cell(rowNum + 2, 16).value = g5
                    ws.cell(rowNum + 2, 17).value = s1
                    ws.cell(rowNum + 2, 18).value = s2
                    ws.cell(rowNum + 2, 19).value = fj
                    ws.cell(rowNum + 2, 20).value = jjdw
                    ws.cell(rowNum + 2, 21).value = ""
                    ws.cell(rowNum + 2, 22).value = ""
                    ws.cell(rowNum + 2, 23).value = ""
                    rowNum += 1
                elif taskType[TaskFlightinfo[3]] == "停场":
                    ws.cell(rowNum + 2, 1).value = rowNum + 1
                    ws.cell(rowNum + 2, 2).value = flighttype
                    ws.cell(rowNum + 2, 3).value = ""
                    ws.cell(rowNum + 2, 4).value = TaskFlightinfo[26]
                    ws.cell(rowNum + 2, 5).value = TaskFlightinfo[25]
                    ws.cell(rowNum + 2, 6).value = TaskFlightinfo[1]
                    ws.cell(rowNum + 2, 7).value = ""
                    ws.cell(rowNum + 2, 8).value = ""
                    ws.cell(rowNum + 2, 9).value = ""
                    ws.cell(rowNum + 2, 10).value = TaskFlightinfo[10]
                    ws.cell(rowNum + 2, 11).value = fx
                    ws.cell(rowNum + 2, 12).value = g1
                    ws.cell(rowNum + 2, 13).value = g2
                    ws.cell(rowNum + 2, 14).value = g3
                    ws.cell(rowNum + 2, 15).value = g4
                    ws.cell(rowNum + 2, 16).value = g5
                    ws.cell(rowNum + 2, 17).value = s1
                    ws.cell(rowNum + 2, 18).value = s2
                    ws.cell(rowNum + 2, 19).value = fj
                    ws.cell(rowNum + 2, 20).value = ""
                    ws.cell(rowNum + 2, 21).value = ""
                    ws.cell(rowNum + 2, 22).value = ""
                    ws.cell(rowNum + 2, 23).value = ""
                    rowNum += 1
                elif taskType[TaskFlightinfo[3]] == "航前":
                    ws.cell(rowNum + 2, 1).value = rowNum + 1
                    ws.cell(rowNum + 2, 2).value = flighttype
                    ws.cell(rowNum + 2, 3).value = TaskFlightinfo[21]
                    ws.cell(rowNum + 2, 4).value = TaskFlightinfo[26]
                    ws.cell(rowNum + 2, 5).value = TaskFlightinfo[25]
                    ws.cell(rowNum + 2, 6).value = TaskFlightinfo[1]
                    ws.cell(rowNum + 2, 7).value = ""
                    ws.cell(rowNum + 2, 8).value = ""
                    ws.cell(rowNum + 2, 9).value = c
                    ws.cell(rowNum + 2, 10).value = TaskFlightinfo[11]
                    ws.cell(rowNum + 2, 11).value = fx
                    ws.cell(rowNum + 2, 12).value = g1
                    ws.cell(rowNum + 2, 13).value = g2
                    ws.cell(rowNum + 2, 14).value = g3
                    ws.cell(rowNum + 2, 15).value = g4
                    ws.cell(rowNum + 2, 16).value = g5
                    ws.cell(rowNum + 2, 17).value = s1
                    ws.cell(rowNum + 2, 18).value = s2
                    ws.cell(rowNum + 2, 19).value = fj
                    ws.cell(rowNum + 2, 20).value = jjdw
                    ws.cell(rowNum + 2, 21).value = ""
                    ws.cell(rowNum + 2, 22).value = FX
                    ws.cell(rowNum + 2, 23).value = XJ
                    rowNum += 1
                elif row[3] == "短停接":
                    ws.cell(rowNum + 2, 1).value = rowNum + 1
                    ws.cell(rowNum + 2, 2).value = flighttype
                    ws.cell(rowNum + 2, 3).value = TaskFlightinfo[21]
                    ws.cell(rowNum + 2, 4).value = TaskFlightinfo[26]
                    ws.cell(rowNum + 2, 5).value = TaskFlightinfo[25]
                    ws.cell(rowNum + 2, 6).value = TaskFlightinfo[1]
                    ws.cell(rowNum + 2, 7).value = a
                    ws.cell(rowNum + 2, 8).value = b
                    ws.cell(rowNum + 2, 9).value = c
                    ws.cell(rowNum + 2, 10).value = TaskFlightinfo[10]
                    ws.cell(rowNum + 2, 11).value = fx
                    ws.cell(rowNum + 2, 12).value = g1
                    ws.cell(rowNum + 2, 13).value = g2
                    ws.cell(rowNum + 2, 14).value = g3
                    ws.cell(rowNum + 2, 15).value = g4
                    ws.cell(rowNum + 2, 16).value = g5
                    ws.cell(rowNum + 2, 17).value = s1
                    ws.cell(rowNum + 2, 18).value = s2
                    ws.cell(rowNum + 2, 19).value = fj
                    ws.cell(rowNum + 2, 20).value = jjdw
                    ws.cell(rowNum + 2, 21).value = ESDW
                    ws.cell(rowNum + 2, 22).value = FX
                    ws.cell(rowNum + 2, 23).value = ""
                    rowNum += 1
                elif row[3] == "特后前接":
                    ws.cell(rowNum + 2, 1).value = rowNum + 1
                    ws.cell(rowNum + 2, 2).value = flighttype
                    ws.cell(rowNum + 2, 3).value = TaskFlightinfo[21]
                    ws.cell(rowNum + 2, 4).value = TaskFlightinfo[26]
                    ws.cell(rowNum + 2, 5).value = TaskFlightinfo[25]
                    ws.cell(rowNum + 2, 6).value = TaskFlightinfo[1]
                    ws.cell(rowNum + 2, 7).value = a
                    ws.cell(rowNum + 2, 8).value = b
                    ws.cell(rowNum + 2, 9).value = c
                    ws.cell(rowNum + 2, 10).value = TaskFlightinfo[10]
                    ws.cell(rowNum + 2, 11).value = fx
                    ws.cell(rowNum + 2, 12).value = g1
                    ws.cell(rowNum + 2, 13).value = g2
                    ws.cell(rowNum + 2, 14).value = g3
                    ws.cell(rowNum + 2, 15).value = g4
                    ws.cell(rowNum + 2, 16).value = g5
                    ws.cell(rowNum + 2, 17).value = s1
                    ws.cell(rowNum + 2, 18).value = s2
                    ws.cell(rowNum + 2, 19).value = fj
                    ws.cell(rowNum + 2, 20).value = jjdw
                    ws.cell(rowNum + 2, 20).value = jjdw
                    ws.cell(rowNum + 2, 21).value = ESDW
                    ws.cell(rowNum + 2, 22).value = FX
                    ws.cell(rowNum + 2, 23).value = ""
                    rowNum += 1
        wb.save('D:/flightinfo/bakup/航班运行备份数据(勿动).xlsx')
        # wb.save('航班运行备份数据.xlsx')
        res = "ok"
        return res
    except Exception as e:
        app_logger.log_error(e)