

from BaseDataMaintenance.model.ots.enterprise import Enterprise
from BaseDataMaintenance.dataSource.source import getConnect_ots
from tablestore import *
from BaseDataMaintenance.common.Utils import *
from BaseDataMaintenance.common.multiThread import MultiThreadHandler
import random
from threading import RLock,Thread
import queue
import ctypes

def filter():

    def producer(task_queue):
        ots_client = getConnect_ots()
        bool_query = BoolQuery(must_not_queries=[
            ExistsQuery("tyc_id")
            ,
            RangeQuery("bid_number",1)
            ,
            RangeQuery("status",401,451),
            BoolQuery(should_queries=[NestedQuery("contacts",ExistsQuery("contacts.phone_no")),
                                      NestedQuery("contacts",ExistsQuery("contacts.mobile_no"))])
        ])

        columns = ["name","phone","credit_code","company_org_type","address","legal_person"]

        rows, next_token, total_count, is_all_succeed = ots_client.search("enterprise", "enterprise_index",
                                                                          SearchQuery(bool_query,sort=Sort(sorters=[FieldSort("tyc_id",SortOrder.ASC)]), limit=100, get_total_count=True),
                                                                          ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))
        _update_count = 0
        _count = 0
        while True:
            adict_rows = getRow_ots(rows)
            for _dict in adict_rows:
                task_queue.put(_dict)

            _count += len(adict_rows)
            print("total_count:%d,count:%d"%(total_count,_count))
            if not next_token:
                break
            rows, next_token, total_count, is_all_succeed = ots_client.search("enterprise", "enterprise_index",
                                                                              SearchQuery(bool_query,next_token=next_token, limit=100, get_total_count=True),
                                                                              ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))
    def comsumer(_dict,result_queue,_lock,ots_client,update_count,_set = set(["phone","credit_code","company_org_type","address","legal_person"])):
        _e = Enterprise(_dict)
        if len(_set&_e.getProperties().keys())==0:
            _e.setValue("status",random.randint(401,450),True)
            print(_dict)
            _e.update_row(ots_client)
            with _lock:
                _update_count[0] += 1
                log(",update_count:%d"%_update_count[0])

    _update_count = [0]
    _lock = RLock()
    task_queue = queue.Queue()
    obj_producerT = Thread(target=producer,args=(task_queue,))
    obj_producerT.start()
    ots_client = getConnect_ots()
    mt = MultiThreadHandler(task_queue,comsumer,None,30,_lock=_lock,ots_client=ots_client,update_count=_update_count)
    mt.run()







if __name__=="__main__":
    filter()






