from celery import task
from celery import shared_task 

#from .hand_class import HandData
from .hand_class_batch import HandData
from .bz_class_batch import BzData
#from .hand_replace import HandReplace

import psycopg2

@shared_task
def add(x, y):
    #print(x+y)

    #f=open('D:\\datacenter\\taskman\\test_log.txt','a')
    #f.write(str(x)+","+str(y)+","+str(x+y))
    #f.close()
    data={'x':x,'y':y,'result':x+y}
    return data


@shared_task
def post_hand(data):
    is_test=True
    is_test=False

    batch_id=data['batch']['id']
    actions=data['actions']
    rt=[]
    cn=psycopg2.connect(database="datacenter", user="acc",password="123",host="47.107.230.103", port="5432")    
    hand=HandData(is_test=is_test,cn=cn)    
    for act in actions:
        r=hand.process_batch(act,batch_id)
        rt.append(r)
    return rt


@shared_task
def post_bz(data):
    is_test=True
    is_test=False

    batch_id=data['batch']['id']
    #actions=data['actions']
    rt=[]
    cn=psycopg2.connect(database="datacenter", user="acc",password="123",host="47.107.230.103", port="5432")    
    bz=BzData(is_test=is_test,cn=cn)   
    r=bz.process_batch(batch_id)
    rt.append(r)
    return rt




#2022-03-16 处理酒鬼工厂上传的标签替换工具 参数 batch_id: label.lbl_replace_batch
@shared_task
def jiugui_replace(batch_id):
    is_test=True
    is_test=False   
    
    cn=psycopg2.connect(database="datacenter", user="acc",password="123",host="47.107.230.103", port="5432")
    hand=HandReplace(is_test=is_test,cn=cn)
    rt=hand.process_batch(batch_id=batch_id)        
    return rt

