# -*- coding: utf-8 -*-
from recommend import *

def write_similarity(product_id1):
    sql="""
    select product_id1, product_id2, similarity_rank
    from xyqb_recommender_system.mix_products_similarity
    where similarity_rank <= 100   and product_id1='{}'
     limit 1000
    """
    product_id1='2304543819777'
    sql=sql.format(product_id1)

    df=execmysl('89',sql)
    #df=df.groupby('product_id1').agg(lambda x:aa(x)).reset_index()#.apply(lambda x:x.sort_values('similarity_rank')).reset_index()
    product_id2s=df['product_id2'].to_list()
    return product_id2s

def write_correlation(product_id1):
    sql="""
    select product_id1, product_id2, correlation_rank
from xyqb_recommender_system.mix_products_correlation
where correlation_rank <= 100  and product_id1='{}' limit 10;
    """
    sql=sql.format(product_id1)
    redis_key="product_correlation:"+product_id1
    df=execmysl('89',sql)
    #df=df.groupby('product_id1').agg(lambda x:aa(x)).reset_index()#.apply(lambda x:x.sort_values('similarity_rank')).reset_index()
    product_id2s=df['product_id2'].to_list()
    return product_id2s


def main():
    product_id1='306834'
    redis_db=redis_conn('89')
    product_id2s=write_similarity(product_id1)
    if product_id2s:
        redis_key="product_similarity:"+product_id1
        if not getRedisValue('89',redis_key):
            print(redis_key,"insert succ")
            redis_db.lpush(redis_key,*product_id2s)

    product_id2s=write_correlation(product_id1)
    if product_id2s:
        redis_key="product_correlation:"+product_id1
        if not getRedisValue('89',redis_key):
            print(redis_key,"insert succ")
            redis_db.lpush(redis_key,*product_id2s)

if __name__ == '__main__':
    main()



# redis_key="product_correlation:{sku}"
#


#print(temp)