from recommend import *
from recommend.publicFunc import *
from recommend.searchTopic import *
def allAppStartTopic():
    '''
    :return: 获取到top150的cid3，brandname，jgid
    '''
    top_info=defaultdict(list)
    #获取top150的cid3
    cid3_concat='jg_intention_score.category_id,jg_intention_score.category_level,jg_heat_rank.rank'
    new_hot_cid150_sql=hot_jd150_sql.format(cid3_concat)
    cid150_df=execmysl(119,new_hot_cid150_sql)
    top_info['cid3']=cid150_df[(cid150_df['category_level']==3)].sort_values(by=['rank'])['category_id'].to_list()[:30]
    print('top 30 cid3==>',top_info['cid3'])
    #获取top150的brandnane
    brandname_concat='jg_intention_score.brand_name,jg_heat_rank.rank'
    new_hot_brandname150_sql=hot_jd150_sql.format(brandname_concat)
    brandname_df=execmysl(119,new_hot_brandname150_sql)
    top_info['brand_name']=brandname_df[(brandname_df['brand_name']!='')].sort_values(by=['rank'])['brand_name'].to_list()[:30]
    print('top 30 brandname==>',top_info['brand_name'])
    #获取top150的jg
    jg_concat='jg_intention_score.jg_id,jg_heat_rank.rank'
    new_hot_jg150_sql=hot_jd150_sql.format(jg_concat)
    jg_df=execmysl(119,new_hot_jg150_sql)
    temp_jg=jg_df.sort_values(by=['rank'])['jg_id'].to_list()[:30]
    top_info['jg_id']=fixpos(temp_jg)
    print('top 30 jgid==>',top_info['jg_id'])
    return top_info



def AllClickTopic(sku_no):
    clickresutl=defaultdict(list)
    sku_info=skuinfo(sku_no)
    cids=[]
    print(sku_info)
    cids.append(sku_info.get('cid1'))
    cids.append(sku_info.get('cid2'))
    cids.append(sku_info.get('cid3'))
    ###金刚位的点击召回
    jd_change_sql=concatSql(jd_hot_sql,**{"category_id":cids})
    jd_click_result=normalClickTopic(sku_no,jd_change_sql)
    clickresutl['jd_click']=jd_click_result
    ###品类的点击召回
    cid_change_sql=concatSql(jg_purpost_sql,**{"jg_type":1})
    cid_click_result=normalClickTopic(sku_no,cid_change_sql)
    clickresutl['cid_click']=cid_click_result
    ##品牌的点击召回
    brandname_change_sql=concatSql(jg_purpost_sql,**{"jg_type":2})
    brandname_click_result=normalClickTopic(sku_no,cid_change_sql)
    clickresutl['brandname_click']=brandname_click_result
    return clickresutl

def allSearchTopic():
    result=defaultdict(list)
    top10_sku,searchinfo=step1()
    ##获取种子数据
    seedData=getseed(top10_sku)
    ##根据cid3种子，获取cid2，cid1数据
    cids_df=cidinfo(seedData.get('cid3')).to_dict(orient='records')[0]
    seedData.update(**{'cid2':cids_df['c_id2']})
    seedData.update(**{'cid1':cids_df['c_id1']})
    jd_change_sql=jg_purpost_all_sql.format(seedData['cid3'],seedData['cid2'],seedData['cid1'],seedData['brand_name'])
    df=execmysl(119,jd_change_sql)
    conditions=concatCondition(seedData,contype='jg')
    conditions1=concatCondition(seedData,contype='cid')
    conditions2=concatCondition(seedData,contype='brandname')
    result['jg_search']=normalSearchTopic(conditions,df)
    result['cid_search']=normalSearchTopic(conditions1,df)
    result['brandname_search']=normalSearchTopic(conditions2,df)
    return result






def getoperate():
    '''
    :return: 获取业务固定位
    '''
    result=requests.get('http://kdsp-api-test1.liangkebang.net/api/kdsp/index/ex/effective-config',headers={"qg-tenant-id":"560761"})
    configinfo=jsonpath.jsonpath(result.json(),'$..config')[0]
    print(result.json())
    df=pd.DataFrame(configinfo)
    temp_df=df[df["fixedPosition"]==2].sort_values(by='rank')
    temp_df['rank']=temp_df['rank'].astype('int')
    #print('获取业务金刚位数据:',temp_df)
    return temp_df[['id','rank']].to_dict(orient='records')

def fixpos(recall_list):
    '''
    :param recall_list:
    :return: 召回数据去掉固定位后，再按照固定位置插入到召回数据中
    '''
    pos=getoperate()
    ##当召回数据中有固定位时，则去掉固定位
    [recall_list.remove(i['id']) for i in pos if i['id'] in recall_list]
    for i in pos:
        recall_list.insert(i.get('rank')-1,i['id'])
    return recall_list





if __name__ == '__main__':
    t=1
    print(t)



