from recommend import *

def backBatch(gid,isuuid=None):
    temp=defaultdict(list)
    if isuuid:
        brandname_redis="user_recall_mapping_jg_Category:"+gid
        cid_redis="user_recall_mapping_jg_brand:"+gid

    else:
        brandname_redis="device_recall_mapping_jg_Category:"+gid
        cid_redis="device_recall_mapping_jg_brand:"+gid

    brandname_recallBatchUuid=getRedisValue(brandname_redis).get(brandname_redis)
    cid_recallBatchUuid=getRedisValue(cid_redis).get(cid_redis)

    brandname_batch_redis= '1' + ":personal_recall_product_brand:" + brandname_recallBatchUuid

    brandname_recallBatchUuid=getRedisValue(brandname_batch_redis).get(brandname_batch_redis)
    print("brandname的批次数据：",brandname_recallBatchUuid)
    temp['brandname_batch']=brandname_recallBatchUuid

    cid_batch_redis= '2' + ":personal_recall_product_brand:" + cid_recallBatchUuid

    cid_recallBatchUuid=getRedisValue(cid_batch_redis).get(cid_batch_redis)
    temp['cid_batch']=cid_recallBatchUuid
    print("cid3的批次数据：",cid_recallBatchUuid)
    return temp

def buzu():
    '''
    :return: 本次补足的数据和appstart触发的逻辑是一样的
    '''
    return appTopic()

def appTopic():
    temp=defaultdict(list)
    cid3_cate_brand_heat_rank_change_sql=cate_brand_heat_rank_sql+' where show_type=1 order by rank asc limit 30'
    print(cid3_cate_brand_heat_rank_change_sql)
    brandname_cate_brand_heat_rank_change_sql=cate_brand_heat_rank_sql+' where show_type=2 order by rank asc limit 30'
    print(brandname_cate_brand_heat_rank_change_sql)
    cid3_df=execmysl(119,cid3_cate_brand_heat_rank_change_sql)
    brandname_df=execmysl(119,brandname_cate_brand_heat_rank_change_sql)
    temp['apptopic_cid3']=cid3_df['show_id'].to_list() if not cid3_df.empty else []
    temp['apptopic_brandname']=brandname_df['show_id'].to_list() if not cid3_df.empty else []
    return temp

def clickTopic(skuno=None,cid3=None,brandid=None,cid2=None,cid1=None):
    temp=defaultdict()
    from recommend.publicFunc import skuinfo
    if skuno:
        sku_infos=skuinfo(skuno)
    else:
        sku_infos={}
    if cid3 and brandid and cid1 and cid2:
        sku_infos['cid3']=cid3
        sku_infos['brand_id']=int(brandid)
        sku_infos['cid2']=cid2
        sku_infos['cid1']=cid1
    print(sku_infos)
    cid3_intention_change_sql=concatSql(cate_brand_intention_score_sql,
                                        **{"category_id":[sku_infos.get('cid3'),sku_infos.get('cid2'),sku_infos.get('cid1')]})
    cid3_intention_change_sql+=" or brand_id='{}'".format(sku_infos.get('brand_id'))
    print('cicktopic 转换后的sql:',cid3_intention_change_sql)
    cid3brandname_df=execmysl(119,cid3_intention_change_sql)#.sort_values(by=['rank'])['']
    print('cicktopic的结果:',cid3brandname_df.head(1))
    ##cid3+brandid
    temp['clicktopic_cid3']=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
                                             & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
                    .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid2+brandid
    temp['clicktopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid2'))
                                              & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid1+brandid
    temp['clicktopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid1'))
                                              & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid3+无名品牌分
    temp['clicktopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
                                              & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==-1)] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid2+无名品牌分
    temp['clicktopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid2'))
                                              & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==-1)] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid1+无名品牌分
    temp['clicktopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid1'))
                                              & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==-1)] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##无品类分+品牌分
    temp['clicktopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']==-1)
                                              & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []

    temp['clicktopic_cid3']=removeRepeat(temp['clicktopic_cid3'])[:30]
    ##cid3+brandid
    temp['clicktopic_brandname']=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
                                             & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
                                .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid2+brandid
    temp['clicktopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid2'))
                                              & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid1+brandid
    temp['clicktopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid1'))
                                              & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid3+无名品牌分
    temp['clicktopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
                                              & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==-1)] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid2+无名品牌分
    temp['clicktopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid2'))
                                              & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==-1)] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##cid1+无名品牌分
    temp['clicktopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid1'))
                                              & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==-1)] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    ##无品类分+品牌分
    temp['clicktopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']==-1)
                                              & (cid3brandname_df['show_type']==2) &(cid3brandname_df['show_id']==sku_infos.get('brand_id'))] \
                                 .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []

    temp['clicktopic_brandname']=removeRepeat(temp['clicktopic_brandname'])[:30]

    return temp


def searchTopic():
    temp=defaultdict(list)
    from recommend.searchTopic import getseed,step1
    #top10_sku,_=step1()
    top10_sku=['108385546803201', '257610058310145', '255342753613825', '253183834981889', '261761672684545', '253184857873409', '310400617292289', '276970748256769', '100007743035', '7261582']
    sku_infos=getseed(top10_sku)
    print(sku_infos)
    temp=clickTopic(cid3=sku_infos.get('cid3'),brandid=sku_infos.get('brand_id'),cid1=9987,cid2=653)
    return temp
    # cid3_intention_change_sql=concatSql(cate_brand_intention_score_sql,
    #                                     **{"category_id":sku_infos.get('cid3')})
    # cid3_intention_change_sql+=" or brand_id='{}'".format(sku_infos.get('brand_id'))
    # print("sql==",cid3_intention_change_sql)
    # cid3brandname_df=execmysl(119,cid3_intention_change_sql)
    # print("sql结果:",cid3brandname_df)
    # cid3brandname_df['category_id']=cid3brandname_df['category_id'].astype('string')
    # cid3brandname_df['brand_id']=cid3brandname_df['brand_id'].astype('string')
    #
    # temp['searchtopic_cid3']=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
    #                                          & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
    #                             .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    #
    # temp['searchtopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
    #                                           & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']=='-1')] \
    #                              .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    #
    # temp['searchtopic_cid3']+=cid3brandname_df[(cid3brandname_df['category_id']=='-1')
    #                                                & (cid3brandname_df['show_type']==1) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
    #                                   .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    # print(temp)
    # temp['searchtopic_brandname']=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
    #                                           & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
    #                              .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    #
    # temp['searchtopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']==sku_infos.get('cid3'))
    #                                            & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']=='-1')] \
    #                               .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    #
    # temp['searchtopic_brandname']+=cid3brandname_df[(cid3brandname_df['category_id']=='-1')
    #                                            & (cid3brandname_df['show_type']==2) &(cid3brandname_df['brand_id']==sku_infos.get('brand_id'))] \
    #                               .sort_values(by=['final_score'],ascending=False)['show_id'].to_list()[:30]  if not cid3brandname_df.empty else []
    # temp['searchtopic_cid3']=removeRepeat(temp['searchtopic_cid3'])[:30]
    # temp['searchtopic_brandname']=removeRepeat(temp['searchtopic_brandname'])[:30]
    # return temp


def allrun(skuno):
    t=appTopic()
    tt=clickTopic()
    print(t)




if __name__ == '__main__':
    #print(clickTopic('179240378044417'))
    #print(allrun())
    gid='479610b6-f346-474f-8d3a-e4de04db8f6e-002'
    gid1='2f2a0320-210e-4e47-b814-a8b564dc2332'
    #print('批次:',backBatch(gid,isuuid=0))
    print("***"*50)
    print('批次:',backBatch(gid1,isuuid=1))
    #skuno='201782177569280'
    #print(clickTopic(skuno))
    #print(searchTopic())





