import datetime

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


def qtPeopleLoad(fdb:flightDB,workLoad:flightDB,bc:str,nowDaystr:str):
    try:
        qtPeopleLoad={}
        aa=[]
        selectdaystr = str(nowDaystr)[0:4] + "-" + str(nowDaystr)[4:6] + "-" + str(nowDaystr)[6:] + " 00:00:00"
        selectday = datetime.datetime.strptime(selectdaystr, "%Y-%m-%d %H:%M:%S")
        selectday_2 = datetime.datetime.strptime(selectdaystr, "%Y-%m-%d %H:%M:%S") + datetime.timedelta(days=1)
        nowDay = datetime.date.today().strftime("%Y%m%d")
        nowDay_1 = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
        nowDay_2 = (datetime.date.today() + datetime.timedelta(days=1)).strftime("%Y%m%d")
        nowtime = datetime.datetime.now()
        nowDay_str2 = datetime.date.today().strftime("%Y-%m-%d")
        a_str = nowDay_str2 + " 00:00:00"
        b_str = nowDay_str2 + " 17:00:00"
        a = datetime.datetime.strptime(a_str, "%Y-%m-%d %H:%M:%S")
        b = datetime.datetime.strptime(b_str, "%Y-%m-%d %H:%M:%S")
        nowDayStr_search = datetime.date.today().strftime("%Y-%m-%d %H:%M:%S")
        tomorrow_search = (datetime.date.today() + datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
        yesterday_search = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
        if bc == "A":
            pglist_str = fdb.queryTabel("pglist{}".format(nowDaystr), "*", "班次='%s'" % bc)
        elif bc == "B" and nowDaystr == nowDay and a < nowtime < b:
            pglist_str = fdb.queryTabel("pglist{}".format(nowDay_1), "*", "班次='%s'" % bc)
        elif bc == "B" and nowDaystr == nowDay and b < nowtime:
            pglist_str = fdb.queryTabel("pglist{}".format(nowDay), "*", "班次='%s'" % bc)
        elif bc == "B" and nowDaystr == nowDay_1:
            pglist_str = fdb.queryTabel("pglist{}".format(nowDay_1), "*", "班次='%s'" % bc)
        elif bc == "B" and nowDaystr < nowDay_1:
            pglist_str = fdb.queryTabel("pglist{}".format(nowDaystr), "*", "班次='%s'" % bc)
        else:
            pglist_str = []

        diplaylist = pglist_str
        allpeopleinfos=fdb.queryTabel('workerinfo', "*", "姓名!=''")
        rowNum = 0
        JS = ""
        for row in diplaylist:
            singleinfo= baseFunction.TuplefindInList3(allpeopleinfos , row[0 ] , 1 , 8).replace("*" , "")
            sq=singleinfo if singleinfo else "未录入"
            if row[2] == "WX":
                JS = "维修员"
            elif row[2] == "FX":
                JS ="放行"
            col={"序号":rowNum,"姓名":row[0],"角色":JS,"授权":sq}
            aa.append(col)
            rowNum += 1
        if len(aa) != 0:
            aa.sort(key=lambda x: (x["角色"], x['授权']))
            rowNum1=0
            for i in aa:
                i["序号"]=rowNum1
                qtPeopleLoad[rowNum1]=i
                rowNum1 += 1
        return qtPeopleLoad
    except Exception as e:
        app_logger.log_error(e)