import json
from hashlib import md5
from search import *
from publicSql import *
import numpy as np
def md5Secret(keyword):
    return md5(keyword.encode()).hexdigest()[8:-8]

def compartResult(keyword):
    codes=[]

def getFeaturesCode():
    filename='/Users/dm/Downloads/量化派/需求文档/hash_dic_key.txt'
    codes=[]
    with open(filename,'r',encoding='utf-8') as file:
        temp=json.load(file)
        for k in temp.keys():
            if k.startswith('query'):## 只验证query特征
                codes.append(k.split('#')[0])
    return list(set(codes)),temp

def getHashValue():
    filename='/Users/dm/Downloads/量化派/需求文档/hash_dic_val.txt'
    with open(filename,'r',encoding='utf-8') as file:
        return json.load(file)

def skuinfos(skuno):
    sql=concatSql(skuinfo_sql,**{"sku_no":skuno})
    df=execmysl(119,sql)
    print(sql,df)
    if df.empty:
        raise Exception('该skuno[{}]不存在'.format(skuno))
    cid1_redis=cid1_rediskey.format(df['cid1'].to_list()[0])
    cid2_redis=cid2_rediskey.format(df['cid2'].to_list()[0])
    cid3_redis=cid3_rediskey.format(df['cid3'].to_list()[0])
    brand_name_redis=brandname_rediskey.format(md5Secret(df['brand_name'].to_list()[0]))
    skuno_redis=sku_rediskey.format(skuno)
    return df,cid1_redis,cid2_redis,cid3_redis,brand_name_redis,skuno_redis

def getSkuIndex(queryword,skuno):
    df,cid1_redis,cid2_redis,cid3_redis,brand_name_redis,skuno_redis=skuinfos(skuno)
    codes,keyfile=getFeaturesCode()
    real_values=defaultdict()
    queryword_redis=FM_rediskey.format(md5Secret(queryword))

    cid1_cid2_cid3_brandname_sku=defaultdict()
    FM_df=getRedisValue(queryword_redis).get(queryword_redis)

    for i in codes:
        if 'cid1' in i:
            tv=json.loads(FM_df[cid1_redis])
            ttt=jsonpath.jsonpath(tv,'$..{}'.format(i))
            #print(i,'===',v)
            real_values[i]=ttt[0] if ttt else 0
        if 'cid2' in i :
            tv=json.loads(FM_df[cid2_redis])
            ttt=jsonpath.jsonpath(tv,'$..{}'.format(i))
            #print(i,'===',v)
            real_values[i]=ttt[0] if ttt else 0
        if 'brand_name' in i :
            tv=json.loads(FM_df[brand_name_redis])
            ttt=jsonpath.jsonpath(tv,'$..{}'.format(i))
            #print(i,'===',v)
            real_values[i]=ttt[0] if ttt else 0
        if 'sku' in i :
            tv=json.loads(FM_df[skuno_redis])
            ttt=jsonpath.jsonpath(tv,'$..{}'.format(i))
            #print(i,'===',v)
            real_values[i]=ttt[0] if ttt else 0

    return dict(real_values)

def result():
    ces_values=getSkuIndex('伊丽莎白雅顿','100002751095')
    codes,keyfile=getFeaturesCode()
    print(codes)
    tt=defaultdict()
    for i in codes:
        t=keyfile[i]
        for index,ii in enumerate(t):
            tces_values= ces_values.get(i) or 0
            if float(tces_values)<=ii :#and i=='query_brand_name_pv_click_rate_1d':
                tt[i]=index-1 if index-1>0 else 0
                #temp=index-1
                print(ii,i,"获取的特征数据值",tces_values,'组结果',tt[i],'-->组数据',t)
                break
            elif float(tces_values)>=t[-1]:
                tt[i]=len(t)-2
                print(ii,i,"获取的特征数据值",tces_values,'组结果',tt[i],'-->组数据',t)

    return tt


def devData():
    a={"if_query_brand":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":0},"if_query_cid2":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":2},"if_query_cid3":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":4},"sale_price":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":12},"profit":{"fea_origin_val":"190.0","hash_dic_key":13,"hash_dic_val":27},"sale_count":{"fea_origin_val":"24","hash_dic_key":10,"hash_dic_val":57},"ranking_points":{"fea_origin_val":"1.8739182465997146","hash_dic_key":0,"hash_dic_val":72},"skuid_brand_name_click_times_user_24h":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":84},"skuid_cid1_click_times_user_24h":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":87},"skuid_brand_name_click_times_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":96},"skuid_cid3_click_times_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":100},"skuid_cid1_click_times_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":107},"query_brand_name_click_times_7d":{"fea_origin_val":"8","hash_dic_key":4,"hash_dic_val":132},"query_brand_name_click_users_7d":{"fea_origin_val":"3","hash_dic_key":1,"hash_dic_val":139},"query_brand_name_click_times_1d":{"fea_origin_val":"1","hash_dic_key":0,"hash_dic_val":151},"query_brand_name_click_users_1d":{"fea_origin_val":"1","hash_dic_key":0,"hash_dic_val":162},"query_cid3_click_times_7d":{"fea_origin_val":"7","hash_dic_key":2,"hash_dic_val":172},"query_cid3_click_users_7d":{"fea_origin_val":"2","hash_dic_key":1,"hash_dic_val":190},"query_cid3_click_times_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":207},"query_cid3_click_users_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":220},"query_cid2_click_times_7d":{"fea_origin_val":"7","hash_dic_key":1,"hash_dic_val":234},"query_cid2_click_users_7d":{"fea_origin_val":"2","hash_dic_key":1,"hash_dic_val":252},"query_cid2_click_times_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":270},"query_cid2_click_users_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":284},"query_cid1_click_times_7d":{"fea_origin_val":"7","hash_dic_key":1,"hash_dic_val":300},"query_cid1_click_users_7d":{"fea_origin_val":"2","hash_dic_key":0,"hash_dic_val":323},"query_cid1_click_times_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":336},"query_cid1_click_users_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":350},"query_skuid_click_times_7d":{"fea_origin_val":"2","hash_dic_key":1,"hash_dic_val":368},"query_skuid_click_users_7d":{"fea_origin_val":"1","hash_dic_key":0,"hash_dic_val":375},"query_skuid_click_times_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":384},"query_skuid_click_users_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":389},"query_brand_name_pv_click_rate_7d":{"fea_origin_val":"0.111111","hash_dic_key":15,"hash_dic_val":403},"query_brand_name_uv_click_rate_7d":{"fea_origin_val":"1.0","hash_dic_key":15,"hash_dic_val":424},"query_brand_name_pv_click_rate_1d":{"fea_origin_val":"1.0","hash_dic_key":10,"hash_dic_val":433},"query_brand_name_uv_click_rate_1d":{"fea_origin_val":"1.0","hash_dic_key":10,"hash_dic_val":440},"query_cid3_pv_click_rate_7d":{"fea_origin_val":"0.28","hash_dic_key":16,"hash_dic_val":455},"query_cid3_uv_click_rate_7d":{"fea_origin_val":"0.4","hash_dic_key":7,"hash_dic_val":466},"query_cid3_pv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":483},"query_cid3_uv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":497},"query_cid2_pv_click_rate_7d":{"fea_origin_val":"0.107692","hash_dic_key":17,"hash_dic_val":518},"query_cid2_uv_click_rate_7d":{"fea_origin_val":"0.4","hash_dic_key":4,"hash_dic_val":529},"query_cid2_pv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":547},"query_cid2_uv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":562},"query_cid1_pv_click_rate_7d":{"fea_origin_val":"0.090909","hash_dic_key":17,"hash_dic_val":582},"query_cid1_uv_click_rate_7d":{"fea_origin_val":"0.4","hash_dic_key":3,"hash_dic_val":596},"query_cid1_pv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":616},"query_cid1_uv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":631},"query_skuid_pv_click_rate_7d":{"fea_origin_val":"0.4","hash_dic_key":11,"hash_dic_val":648},"query_skuid_uv_click_rate_7d":{"fea_origin_val":"0.2","hash_dic_key":11,"hash_dic_val":658},"query_skuid_pv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":666},"query_skuid_uv_click_rate_1d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":672},"uv_click_rate_1h":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":677},"uv_click_rate_24h":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":683},"uv_click_rate_7d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":698},"uv_click_rate_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":725},"pv_click_rate_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":744},"click_rate_gender_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":763},"click_rate_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":772},"brand_name_click_rate_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":774},"cid3_click_rate_gender_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":792},"cid3_click_rate_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":798},"cid2_click_rate_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":805},"cid1_click_rate_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":815},"skuid_brand_name_totalclick_times_user_7d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":827},"skuid_cid1_totalclick_times_user_7d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":831},"skuid_totalclick_times_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":841},"skuid_cid1_totalclick_times_user_40d":{"fea_origin_val":"0","hash_dic_key":0,"hash_dic_val":844}}
    return a




if __name__ == '__main__':
    # codevaule=100000
    # a= [0.0, 2.0, 6.0, 12.0, 22.0, 38.0, 58.0, 88.0, 131.0, 183.0, 270.0, 385.0, 495.0, 652.0, 918.0, 1475.0, 3600.0, 9050.0]
    # # t=np.array(a)
    # # print(pd.cut(t,bins=a,labels=False))
    # #tindex=0
    # for index, i in enumerate(a):
    #     if codevaule<i:
    #         tindex=index
    #         break
    #     elif float(codevaule)>=i:
    #         tindex=len(a)-1
    #
    # print(tindex)
    print('获取到的组数据',result())
    print("****"*100)
    #print('--',result())

    a={'query_cid1_click_users_1d': 0, 'query_skuid_uv_click_rate_7d': 11, 'query_cid3_pv_click_rate_1d': 0, 'query_cid3_click_times_1d': 0, 'query_cid1_pv_click_rate_7d': 17, 'query_cid2_click_users_1d': 0, 'query_brand_name_click_times_1d': 0, 'query_cid1_click_users_7d': 0, 'query_cid1_click_times_1d': 0, 'query_cid3_uv_click_rate_1d': 0, 'query_brand_name_click_users_7d': 1, 'query_cid2_uv_click_rate_1d': 0, 'query_cid3_pv_click_rate_7d': 16, 'query_cid3_click_times_7d': 2, 'query_skuid_click_users_1d': 0, 'query_cid2_uv_click_rate_7d': 4, 'query_skuid_pv_click_rate_7d': 11, 'query_cid1_pv_click_rate_1d': 0, 'query_skuid_click_times_7d': 1, 'query_cid3_click_users_7d': 1, 'query_skuid_uv_click_rate_1d': 0, 'query_cid2_click_times_7d': 1, 'query_skuid_pv_click_rate_1d': 0, 'query_brand_name_pv_click_rate_7d': 15, 'query_brand_name_uv_click_rate_7d': 15, 'query_skuid_click_users_7d': 0, 'query_cid1_uv_click_rate_7d': 3, 'query_cid1_uv_click_rate_1d': 0, 'query_cid2_pv_click_rate_7d': 17, 'query_cid2_click_times_1d': 0, 'query_brand_name_click_times_7d': 4, 'query_cid2_pv_click_rate_1d': 0, 'query_brand_name_click_users_1d': 0, 'query_cid3_uv_click_rate_7d': 7, 'query_brand_name_uv_click_rate_1d': 10, 'query_cid3_click_users_1d': 0, 'query_brand_name_pv_click_rate_1d': 10, 'query_cid1_click_times_7d': 1, 'query_cid2_click_users_7d': 1, 'query_skuid_click_times_1d': 0}
    deva=devData()
    base_values=getHashValue()
    for k,v in a.items():
        dv=deva[k]['hash_dic_val']==base_values[k+'_group#'+str(v)]
        print(k,'开发结果:',dv,deva[k]['hash_dic_val'],' 测试结果:',base_values[k+'_group#'+str(v)])

