import datetime

import requests

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


def dingdingFlightsts(database:flightDB):
    try:
        flightsts = {}
        sever_code={"JJDW":"接机到位","FX":"放行","SJDW":"随机到位","XJ":"销子夹板","ESDW":"二送到位"}
        sever_code1 = {"JJDW": "已到位", "FX": "已放行", "SJDW": "已到位", "XJ": "已取下", "ESDW": "已到位"}
        code2 = {"JJDW": "接机已到位", "FX": "已放行", "SJDW": "随机已到位", "XJ": "销子夹板已取下", "ESDW": "二送已到位"}
        code3 = {"JJDW": "接机已到位提示", "FX": "已放行提示", "SJDW": "随机已到位提示", "XJ": "销子夹板已提示", "ESDW": "二送已到位提示"}
        code4 = {"JJDW": 8, "FX": 9, "SJDW": 11, "XJ": 10, "ESDW": 12}
        try:
            #'''
            resfwq = requests.get(url="http://124.223.185.200:888/search_status/", timeout=10)  # 自己服务器
            if resfwq.json() != "":
                flightsts = resfwq.json()
                #print(flightsts)
            '''
            #测试数据
            flightsts={'325F': {'JH': '325F', 'JWH': '', 'JJDW': 0, 'FX': 0, 'SJDW': 0, 'XJ': 1, 'ESDW': 0, 'YW': ' 325F 销夹'}}
            '''
        except Exception as e:
            app_logger.log_error(e)
            pass
            # QT输出弹窗警告,服务器链接失效

        nowDay = datetime.datetime.now().strftime("%Y%m%d")
        nowDay_1 = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
        now = datetime.datetime.now()
        now11=str(now).split(".")[0]
        day = [nowDay_1, nowDay]
        tfac_acs=[]
        for nowDayStr in day:
            dmacs = database.queryTabel('sortFlight{}'.format(nowDayStr),"航班编号","CAST(级别 AS integer)=1")
            datas = database.queryTabel('flightsts{}'.format(nowDayStr), "*", "航班编号!=''")
            if dmacs:
                for i in dmacs:
                    dmac = baseFunction.TuplefindInList3(datas , i[0 ] , 0 , 1)
                    if str(dmac) not in str(tfac_acs):
                        tfac_acs.append(dmac)

        if len(tfac_acs) != 0:
            tfudatas=database.queryTabel('flightsts{}'.format(nowDay), "*", "天府飞机 != ''")
            for i in tfac_acs:
                #print(i[0])
                if i in flightsts.keys():
                    for j,k in sever_code.items():
                        if flightsts[i][j] == 1:
                            newdic={"%s"%k:"'%s'"%sever_code1[j]}
                            #print(newdic)
                            database.lazyUpdateItem('flightsts{}'.format(nowDay),newdic,"机号='%s' and 天府飞机!=''"%i)
                            newdic={}
                            sts = baseFunction.TuplefindInList3(tfudatas , i , 1 , code4[j ])
                            taskid = baseFunction.TuplefindInList3(tfudatas , i , 1 , 0)
                            a=sts if sts != "" else "0"
                            b=taskid if taskid != "" else "未显示机号"
                            if a !="1":
                                newdic={"%s"%code3[j]:1}
                                database.lazyUpdateItem('flightsts{}'.format(nowDay), newdic, "机号='%s' and 天府飞机!=''" % i)
                                newdic={"航班编号":"%s"%b,"警告信息":"","信息状态":"","提示信息":"钉钉:%s%s!%s"%(i,code2[j],now11),"产生时间":"%s"%now,"处理人":"","处理时间":"","变更字段":"","显示对象":"","提示内容":""}
                                database.lazyInsertData('logs',newdic)
            database.FunctionCommit()

            #########################################
            #引入钉钉显示
            ########################################
    except Exception as e:
        app_logger.log_error(e)
        database.FunctionCommit()