import pandas as pd
import requests

from feature import *

#codes=["user_consumption_map_whitelist"]

def devResult(uuid,codes,codetype='pre'):
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'rc_auth_key': 'rc_offline',
        'rc_auth_secret': '9d9}bc24!e1z1x3`(x~4r29d$+45n3)\'zb696b$85e>_]p2&4f{,a3~8b3e_ldt^'
    }
    temp={'uuid':uuid}
    body={
        "codes":codes
    }
    if codetype=='pre':
        body["user_uuid"]=uuid
        url=pre_loan_url
    else:
        url=mid_loan_url
        body['tradeNo']=0
        body['userUuid']=uuid
    t=requests.post(url,data=body,headers=headers)
    print(t.json())

    temp[code+'_dev']=jsonpath.jsonpath(t.json(),'$..{}.value'.format(code))[0]
    return temp

def baseinfo(num=10):
    feature_user_df=execmysl('feature_aliyun',feature_user_sql.format(num=num))
    feature_user_df=feature_user_df.astype(str)
    feature_user_df['phone_no']=feature_user_df['phone_no'].apply(lambda x:md5jiami(x.encode()))
    return feature_user_df

def getwhite(code,num=10):
    feature_user_df=baseinfo(num)
    try:
        #return feature_user_df.head(1)
        map_white_change_sql=concatSql(map_white_sql,**{"phone":feature_user_df['phone_no'].to_list()})
        map_white_df=execmysl('feature_aliyun',map_white_change_sql)
        #print(map_white_df['phone'].to_list)
        #feature_user_df['iswhite']=feature_user_df[~(feature_user_df['phone_no'].isin(map_white_df['phone'].to_list()))]
        newcolumes=code+'_test'
        feature_user_df[newcolumes]=feature_user_df['phone_no'].apply(lambda x:1 if x in map_white_df['phone'].to_list() else 0)
        feature_user_df[newcolumes]=feature_user_df['phone_no'].apply(lambda x:1 if x in map_white_df['phone'].to_list() else 0)
        reportname=filePath(feature_report,newcolumes)
        feature_user_df[['uuid',newcolumes]].to_excel(reportname,index=0,encoding = 'utf-8',float_format = str)
        #devResult(code,reportname)
        #return feature_user_df
        return feature_user_df
    except:
        traceback.print_exc()

def dev_compare(code,codetype='pre',filepath=None):
    temp=[]
    filepath=os.path.join(feature_path,'tempFile','map_white_result_1656582391.xlsx')
    test_df=readFile(filepath)
    test_df=test_df.rename(columns={'user_consumption_map_whitelist_test':'userConsumptionMapWhitelistCalc#isWhite_test'})
    # print(test_df.columns)
    # return 0
    for i in test_df['uuid'].to_list():
        t=devResult(i,code,codetype)
        temp.append(t)
    dev_df=pd.DataFrame(temp)
    df=pd.merge(test_df,dev_df,on='uuid',how='left')
    #return df.head(2)
    print(df.columns)
    df['结果是否正确']=df[code+'_test']==df[code+'_dev']
    df=df.astype(str)
    reportname=filePath(feature_report,code)
    df.to_excel(reportname,index=0,encoding = 'utf-8',float_format = str)






if __name__ == '__main__':
    code='user_consumption_map_whitelist'
    code='userConsumptionMapWhitelistCalc#isWhite'
    print(dev_compare(code,codetype='mid'))
    #print(getwhite(code,1000))


    #print(devResult('58f23a26-df3e-4d3e-80c2-8a4fece16c0e'))




