import datetime

from .dingding import dingding_alert1 , dingding_alert11
from .flightDB import flightDB


def MM_GJJH_LIST(fdb:flightDB,team,data):
    GJlist1 = {}
    GJlist2 = {}
    if data !=[]:
        now=datetime.datetime.now()
        nowDay_str2 = datetime.date.today().strftime("%Y-%m-%d")
        a_str = nowDay_str2 + " 17:00:00"
        time1 = datetime.datetime.strptime(a_str, "%Y-%m-%d %H:%M:%S")
        if time1 < now :
            nowDay = datetime.datetime.now().strftime("%Y%m%d")
            pglist = fdb.queryTabel("pglist%s"%nowDay,"姓名","班次 = 'A'")
        else:
            nowDay = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
            pglist = fdb.queryTabel("pglist%s" % nowDay, "姓名", "班次 = 'B'")
        for row in data:
            phone=fdb.queryTabel("workerinfo","电话","姓名='%s'"%str(row["zlynam"]))
            if len(phone) != 0 and len(phone[0][0]) == 12:
                phonenumber=phone[0][0][1:]
                if str(row["zlynam"]) in str(pglist) and str(row["zlynam"]) in GJlist1.keys():
                    maktx=GJlist1[row["zlynam"]]["工具名"]+","+row["maktx"]
                    zzbh = GJlist1[row["zlynam"]]["编号"] + "," + row["zzbh"]
                    zjcmeng = GJlist1[row["zlynam"]]["数量"] + "," + row["zjcmeng"]
                    a={"工具名":maktx,"编号":zzbh,"数量":zjcmeng,"phonenumber":phonenumber}
                    GJlist1[row["zlynam"]]=a
                elif str(row["zlynam"]) in str(pglist) and str(row["zlynam"]) not in GJlist1.keys():
                    maktx =row["maktx"]
                    zzbh = row["zzbh"]
                    zjcmeng = row["zjcmeng"]
                    a={"工具名":maktx,"编号":zzbh,"数量":zjcmeng,"phonenumber":phonenumber}
                    GJlist1[row["zlynam"]] = a
            else:
                if str(row["zlynam"]) in str(pglist) and str(row["zlynam"]) in GJlist2.keys():
                    maktx = GJlist2[row["zlynam"]]["工具名"] + "," + row["maktx"]
                    zzbh = GJlist2[row["zlynam"]]["编号"] + "," + row["zzbh"]
                    zjcmeng = GJlist2[row["zlynam"]]["数量"] + "," + row["zjcmeng"]
                    a = {"工具名": maktx, "编号": zzbh, "数量": zjcmeng}
                    GJlist2[row["zlynam"]] = a
                elif str(row["zlynam"]) in str(pglist) and str(row["zlynam"]) not in GJlist2.keys():
                    maktx = row["maktx"]
                    zzbh = row["zzbh"]
                    zjcmeng = row["zjcmeng"]
                    a = {"工具名": maktx, "编号": zzbh, "数量": zjcmeng}
                    GJlist2[row["zlynam"]] = a
        if GJlist1:
            msg = "经查询您有以下工具未归还，请注意核实工具归还或交接情况："
            times=5
            telphone=list()
            for i in GJlist1:
                msg =msg+"\n"+"【"+str(i)+"】" + "\n[工具名]：" + str(GJlist1[i]["工具名"]) + "\n[数量]：" + str(GJlist1[i]["数量"]) + "\n[编号]：" + str(GJlist1[i]["编号"])
                telphone.append(str(GJlist1[i]["phonenumber"]))
                #telphone.append("17729693827")
                times+=1
                if times % 5 == 0:
                    if team == "test":
                        dingding_alert1(telphone,msg)
                    elif str(team)=="1":
                        dingding_alert11(telphone, msg)
                    elif str(team)=="2":
                        dingding_alert1(telphone, msg)
                    elif str(team)=="3":
                        dingding_alert1(telphone, msg)
                    elif str(team)=="4":
                        dingding_alert1(telphone, msg)
                    msg = "经查询您有以下工具未归还，请注意核实工具归还或交接情况："
                    telphone = list()
            if msg !="经查询您有以下工具未归还，请注意核实工具归还或交接情况：":
                if team == "test":
                    dingding_alert1(telphone, msg)
                elif str(team) == "1":
                    #print(telphone, msg)
                    #dingding_alert1(telphone, msg)
                    dingding_alert11(telphone, msg)
                elif str(team) == "2":
                    dingding_alert1(telphone, msg)
                elif str(team) == "3":
                    dingding_alert1(telphone, msg)
                elif str(team) == "4":
                    dingding_alert1(telphone, msg)
        if GJlist2:
            msg="经查询您有以下工具未归还，请注意核实工具归还或交接情况："
            a=list()
            for i in GJlist2:
                msg =msg+"\n"+"【"+str(i)+"】" + "\n[工具名]：" + str(GJlist2[i]["工具名"]) + "\n[数量]：" + str(GJlist2[i]["数量"]) + "\n[编号]：" + str(GJlist2[i]["编号"])
            if team == "test":
                dingding_alert1(a, msg)
            elif str(team) == "1":
                #print("联系不上的", msg)
                #dingding_alert1(a, msg)
                dingding_alert11("", msg)
            elif str(team) == "2":
                dingding_alert1(a, msg)
            elif str(team) == "3":
                dingding_alert1(a, msg)
            elif str(team) == "4":
                dingding_alert1(a, msg)
    return