import requests

from recommend import *


def dealData(t):
    print('处理成json的数据:',t)
    tt=json.loads(t)
    temp=[]
    for i in tt:
        temp.append(i.get('sku_id'))
    return temp


def payRecall(skuno,num=50):
    temp=defaultdict(list)
    print("支付图关联表召回start")
    pay_product_change_redis=pay_product_redis.strip().format(sku_no=skuno)
    #print(pay_product_change_redis)
    pay_result=getRedisValue(pay_product_change_redis)
    #print(pay_result)

    from recommend.publicFunc import skuinfo
    skuno_info=skuinfo(skuno)
    pay_skus=dealData(pay_result.get(pay_product_change_redis))
    print(pay_skus)
    if pay_result :
        skuss_change_sql=concatSql(skuinfo_sql,**{"sku_no":pay_skus}) +" and cid3!='{}'".format(skuno_info.get('cid3')) \
                         +" and status=3 and stock>0 order by sku_no"
        skdss=execmysl('nearly',skuss_change_sql)
        if not skdss.empty :
            final_skus=skdss['sku_no'].to_list()
            temp['70011']=final_skus[:num]
            print(final_skus[:num])
            print("支付图关联表召回stop",len(final_skus),skuss_change_sql)
            #return temp

    print("下单图关联表召回start",'**'*50)
    order_product_change_redis=order_product_redis.format(sku_no=skuno).strip()
    order_result=getRedisValue(order_product_change_redis)
    order_skus=dealData(order_result.get(order_product_change_redis))
    if order_result :
        skuss_change_sql=concatSql(skuinfo_sql,**{"sku_no":order_skus}) +' and cid3!={}'.format(skuno_info.get('cid3'))  +" and status=3 and stock>0 order by sku_no"
        skdss=execmysl('nearly',skuss_change_sql)
        if not skdss.empty :
            final_skus=skdss['sku_no'].to_list()
            #print("redis===",final_skus)
            chaji=subtraction(final_skus,temp.get('70011'))
            temp['70012']=chaji[:num]
            #print( temp['70012'])
            print("下单图关联表召回stop",len(final_skus),skuss_change_sql)
            #return temp
    print("加购图关联表召回start",'**'*50)

    product_change_redis=product_redis.format(sku_no=skuno).strip()
    product_result=getRedisValue(product_change_redis)
    if product_result :#and len(product_result.get(product_change_redis))>=num:
        skuss_change_sql=concatSql(skuinfo_sql,**{"sku_no":product_result.get(product_change_redis)}) +' and cid3!={}'.format(skuno_info.get('cid3')) +" and status=3 and stock>0 order by sku_no"
        skdss=execmysl('nearly',skuss_change_sql)
        if not skdss.empty :#and skdss.shape>num:
            final_skus=skdss['sku_no'].to_list()
            chaji=subtraction(subtraction(final_skus,temp.get('70011')),temp.get('70012'))
            temp['70013']=chaji
            print("加购图关联表召回stop")
            #return temp

    print("ALS相关表召回start",'**'*50)
    als_correlation_change_redis=als_correlation_redis.format(sku_no=skuno).strip()
    als_correlation_result=getRedisValue(als_correlation_change_redis)

    if als_correlation_result :#and len(als_correlation_result.get(als_correlation_change_redis))>=num:
        skuss_change_sql=concatSql(skuinfo_sql,**{"sku_no":als_correlation_result.get(als_correlation_change_redis)}) +' and cid3!={}'.format(skuno_info.get('cid3')) +" and status=3 and stock>0"
        skdss=execmysl('nearly',skuss_change_sql)
        if not skdss.empty: #and skdss.shape>num:
            final_skus=skdss['sku_no'].to_list()
            chaji=subtraction(subtraction(subtraction(final_skus,temp.get('70011')),temp.get('70012')),temp.get('70013'))
            temp['70014']=chaji
            print("ALS相关表召回stop",len(skdss),skuss_change_sql)
            #return temp

    dapan_change_sql=concatSql(dapan_sql,**{"cid3":skuno_info.get('cid3')})+" and brand_name!='{}'".format(skuno_info.get('brand_name'))

    dapan_df=execmysl('nearly',dapan_change_sql)
    #if dapan_df.shape[0]>=50:
    temp['70015_1']=dapan_df['sku_no'].to_list()[:50]

    #return temp

    dapan_change_sql=concatSql(dapan_sql,**{"brand_name":skuno_info.get('brand_name')})+' and cid3!={}'.format(skuno_info.get('cid3'))
    dapan_df=execmysl('nearly',dapan_change_sql)
    temp['70015_2']=dapan_df['sku_no'].to_list()[:50]
    return temp

def backRecall(uuid):
    back_recall_change_sql=nearly_batch_sql.format(uuid)+' and recall_reason!=50000'
    back_recall_result=execmysl('nearly',back_recall_change_sql)
    return {"7002":back_recall_result['sku_no'].to_list()} if not back_recall_result.empty else {"7002":[]}

def payDapanbu(skuno):
    try:
        from recommend.publicFunc import dapanBu
        dapan_result=dapanBu()
        return {"7003":dapan_result[:300]}
    except :
        traceback.print_exc(2)
        return {"7003":[]}


def all(skuno,uuid):
    temp=defaultdict()
    sku_temp=[]
    back_recall_skus=backRecall(uuid)
    print("上一批次数据:",back_recall_skus)
    #temp.append(back_recall_skus)
    pay_recall_skus=payRecall(skuno,num=50)
    temp.update(pay_recall_skus)

    dapan_result=payDapanbu(skuno)
    for k,v in temp.items():
        sku_temp+=v
    print('支付召回数据',sku_temp[:300])

    if len(removeRepeat(mergelist(sku_temp)))<300:
        temp.update(dapan_result)

    return temp

def breakUp(temp):
    pass




def checkresult():
    a=['100012355342', '11050632353793', '11102968880129', '179229279393281', '188701955858944', '214857903777280', '214864564329984', '216227226849793', '219236606151681', '219717743418368', '219717760191488', '220405299154944', '221238765822976', '221242506611712', '221326317721601', '226455405730817', '2302639605249', '230741925957633', '232141556812289', '238057538721280', '239217926999553', '241458842967559', '241462055803909', '2428586170369', '243851634153473', '244342367719425', '245090111202307', '246531249415169', '246532608630785', '246615437743105', '257383818600960', '257401358391296', '261007085148162', '266081478647298', '275001899225600', '279850356055553', '281430559826433', '281529478289409', '282035714270724', '29169639759873', '298224007783425', '298225467657729', '301741544312332', '302492895152129', '307494551820801', '307565317857793', '311153452910081', '316840577339904', '316975868289025', '319861666615296', '320721197213185', '322042655152128', '322740226627585', '326397760381953', '328040374869504', '328575677900289', '340438528036359', '340913373319168', '340970164461576', '343082273351169', '343204705611264', '343222237008385', '343594322895873', '343989417936385', '346650937922049', '346845511949325', '346848867125256', '346936259644929', '349035157262337', '351699319917057', '351737404724737', '351824041478657', '353012455837185', '353229235030529', '355068805384705', '356766476148225', '356795642291201', '359044015728131', '361900941516801', '361913264637441', '367038149960712', '367681053661185', '369193007195649', '369201421750785', '371346178315265', '372730534042113', '372781200972325', '373380391116801', '374333605684738', '374340064647694', '377681608183297', '377843289688065', '37992240793601', '37993935289345', '383811482819591', '64833806673409', '794422', '384196679049746', '393791694966273']
    sql="""
    select  -- a.*,
distinct b.sku_no,
b.recall_reason 
-- count(1)
from 
(select id,recall_batch_uuid,event_type from nearline_recommend_test.recommend_product_batch 
where user_uuid='1e51cf78-e12c-4f17-9d5b-b25404303a66-3' order by id desc limit 1)a
join nearline_recommend_test.recommend_product_record b on a.recall_batch_uuid=b.recall_batch_uuid
where -- b.sku_no='37993381637121'
recall_reason=70011
    """
    df=execmysl('nearly',sql)['sku_no'].to_list()
    print(subtraction(a,df))


if __name__ == '__main__':
    skuno='179240378044417'
    uuid='2f2a0320-210e-4e47-b814-a8b564dc2332'
    #t=all(skuno,uuid)
    print(t)
    #requests.post().t
    #print(payDapanbu(skuno))
    #print(deal_all())
    #print(checkresult())




