import datetime
import json

import requests

import unitls.baseFunction as baseFunction
from unitls.LogerinTxt import app_logger
from unitls.StaticDataclass import get_dic
from .flightDB import flightDB


def dingdingDelete_flight(database:flightDB):
    try:
        dmac={}
        # 需要建立昨日初始化
        nowDay = datetime.datetime.now().strftime("%Y%m%d")
        nowDay_1 = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
        day = [nowDay_1,nowDay]
        for nowDayStr in day:
            tfac_ids=database.queryTabel('sortFlight{}'.format(nowDayStr),"航班编号","CAST(级别 AS integer)=1")
            datas=database.queryTabel('TaskFlightinfo{}'.format(nowDayStr),"*","航班编号!=''")
            if len(tfac_ids) != 0:
                for i in tfac_ids:
                    acno= baseFunction.TuplefindInList3(datas , i[0 ] , 0 , 1)
                    type=get_dic("taskType")[ baseFunction.TuplefindInList3(datas , i[0 ] , 0 , 3) ]
                    if acno in dmac.keys() and type !="航前":
                        dmac[acno]=i[0]
                    elif acno not in dmac.keys():
                        dmac[acno] = i[0]
        try:
            res = requests.post(url="http://124.223.185.200:888/delete_flight/", data=json.dumps(dmac),timeout=5)  # 自己服务器
            #flightsts = res.json()
            #print(flightsts)
        except Exception as e:
            app_logger.log_error(e)
            pass
            #QT输出弹窗警告,服务器链接失效
    except Exception as e:
        app_logger.log_error(e)