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


# 2023-07-13
from .hs_class_batch import HsData




import psycopg2

# 2023-01-13
from .uivs_nfc import YgNFC



@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="hntj", password="hntj!@)!", 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="hntj", password="hntj!@)!", 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



# 2023-07-13
@shared_task
def post_hs(data):
    is_test=True
    #is_test=False

    batch_id=data['batch']['id']
    #actions=data['actions']
    rt=[]
    cn=psycopg2.connect(database="datacenter", user="hntj", password="hntj!@)!", host="47.107.230.103", port="5432")    
    hs=HsData(is_test=is_test,cn=cn)   
    r=hs.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="hntj", password="hntj!@)!", 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






# 2023-01-13 

# 默认使用测试环境
is_test=True

# 默认激活nfc标签
enable=True

@shared_task
def nfc_activate(data):
    cn=psycopg2.connect(database="datacenter", user="hntj", password="hntj!@)!", host="47.107.230.103", port="5432") 
    ygnfc=YgNFC(is_test=is_test, cn=cn)
    rt=ygnfc.post_data(data, enable)
    return rt
