from recommend import *
from recommend.publicFunc import skuinfo,dapanBu

def preconditions(uuid,sku_no):
    '''
    :param uuid:
    :return: 判断上批次是否为加购topic,是则不触发，不是才触发
    '''
    try:
        eventtype= 'orderTopic'
        back_batch_change_redis=back_batch_redis.format(uuid=uuid,sku_no=sku_no)
        ttt=getRedisValue(back_batch_change_redis).get(back_batch_change_redis)
        cur_btach_change_sql=concatSql(cur_batch_sql,**{"recall_batch_uuid":ttt})
        cur_batch_df=execmysl(119,cur_batch_sql.format(uuid))
        is_order_topic= 0 if cur_batch_df.empty or not  cur_batch_df['event_type'].to_list()[0]==eventtype else 1
        return is_order_topic
    except:
        return 0

def recall_J1(skuno):
    '''
    :param skuno:
    :return: 将所有的数据都召回，然后进行去重并选取100
    '''
    # graph_change_sql=concatSql(graph_relation_sql,**{"sku1_id":skuno})
    # graph_df=execmysl('7438_27',graph_change_sql)
    # temp= graph_df.sort_values(by='rank')['sku2_id'].to_list()[:100]
    # if len(set(100))>100:
    #     return removeRepeat(temp)[:100]
    # als_change_sql1=concatSql(als_sql,**{"product_id1":skuno})
    # als_df1=execmysl('7438_27',als_change_sql1)
    # temp+=als_df1.sort_values(by='rank')['product_id2'].to_list()
    # als_change_sql2=concatSql(als_sql,**{"product_id2":skuno})
    # als_df2=execmysl('7438_27',als_change_sql1)
    # temp+=als_df2.sort_values(by='rank')['product_id1'].to_list()
    # return removeRepeat(temp)[:100]
    temp=[]
    graph_change_redis=graph_redis.format(sku_no=skuno)
    graph_date=getRedisValue(graph_change_redis).get(graph_change_redis)
    als_change_redis=als_redis.format(sku_no=skuno)
    als_date=getRedisValue(als_change_redis).get(als_change_redis)
    temp+=graph_date
    temp+=als_date
    temp+=supply_J1(skuno)
    return temp


def supply_J1(skuno):
    sku_info=skuinfo(skuno)
    dapan_change_sql=concatSql(dapan_sql,**{"cid3":sku_info.get('cid3')})
    dapan_df=execmysl('7438_27',dapan_change_sql)
    temp=dapan_df[dapan_df['brand_name']!=sku_info.get('brand_name')]['sku_no'].to_list()
    temp=removeRepeat(temp)[:50]
    dapan_change1_sql=concatSql(dapan_sql,**{"brand_name":sku_info.get('brand_name')})
    dapan1_df=execmysl('7438_27',dapan_change1_sql)
    temp+=dapan_df[dapan_df['cid3']!=sku_info.get('cid3')]['sku_no'].to_list()
    return removeRepeat(temp)[:100]

def recall_J2():
    pass

def recall_J3():
    return dapanBu()


if __name__ == '__main__':
    #print(recall_J1('12312'))
    t=preconditions(1,2)
    print(t)
