from unitls.LogerinTxt import app_logger
from .flightDB import flightDB
from .judge_bc import judge_bc


def get_empNos(database:flightDB,dict:dict):
    try:
        date=judge_bc()["date"]
        date_str=judge_bc()["date"][0:4]+"-"+judge_bc()["date"][4:6]+"-"+judge_bc()["date"][6:]
        bc = judge_bc()["bc"]
        faillist=[]
        WXempNos = ""
        FXempNos = ""
        WX = ""
        FX = ""
        wxkey = ["工1", "工2", "工3", "工4"]
        for i in wxkey:
            if dict[i] != "":
                aa = []
                if bc == "A":
                    aa = database.queryTabel("pglist{}".format(date), "工号", "班次='A' and 姓名='%s'" % dict[i])
                elif bc == "B":
                    aa = database.queryTabel("pglist{}".format(date), "工号", "班次='B' and 姓名='%s'" % dict[i])
                if len(aa) != 0 and WXempNos == "":
                    if aa[0][0] !="":
                        WXempNos = str(aa[0][0])
                    else:
                       faillist.append(dict[i])
                elif len(aa) != 0 and WXempNos != "":
                    if aa[0][0] != "":
                        WXempNos =WXempNos+","+str(aa[0][0])
                    else:
                        faillist.append(dict[i])
                else:
                    faillist.append(dict[i])
                if dict[i] != "" and WX == "":
                    WX=dict[i]
                elif dict[i] != "" and WX != "":
                    WX=WX+","+dict[i]


        if dict["放行"] != "":
            aa = []
            if bc == "A":
                aa=database.queryTabel("pglist{}".format(date),"工号","班次='A' and 姓名='%s'"%dict["放行"])
            elif bc == "B":
                aa=database.queryTabel("pglist{}".format(date),"工号","班次='B' and 姓名='%s'"%dict["放行"])
            if len(aa) != 0 and FXempNos == "":
                if aa[0][0] != "":
                    FXempNos = str(aa[0][0])
                else:
                    faillist.append(dict["放行"])
            elif len(aa) != 0 and FXempNos != "":
                if aa[0][0] != "":
                    FXempNos = FXempNos + "," + str(aa[0][0])
                else:
                    faillist.append(dict["放行"])
            elif len(aa) == 0:
                faillist.append(dict["放行"])
            FX=dict["放行"]
        if (WXempNos != "" or FXempNos != "") and faillist=="":
            dict1 = {"code": "ok", "WXempNos": WXempNos, "FXempNos": FXempNos, "ESempNos": WXempNos, "faillist":faillist,"date":date_str,"bc":bc,"WX":WX,"FX":FX,"ES":WX}
        elif (WXempNos != "" or FXempNos != "") and faillist !="":
            dict1 = {"code": "justok", "WXempNos": WXempNos, "FXempNos": FXempNos, "ESempNos": WXempNos, "faillist":faillist,"date":date_str,"bc":bc,"WX":WX,"FX":FX,"ES":WX}
        else:
            dict1 = { "code" : "fail" , "WXempNos" : "" , "FXempNos" : "" , "ESempNos" : "" , "faillist" : faillist , "date" : date_str , "bc" : bc , "WX" : WX , "FX" : FX , "ES" : WX }
        return dict1
    except Exception as e:
        app_logger.log_error(e)
        return {"code":"error:{}".format(e)}