
from BaseDataMaintenance.dataSource.source import getConnect_ots
from BaseDataMaintenance.common.Utils import *
from BaseDataMaintenance.dataSource.pool import ConnectorPool
from tablestore import *
from BaseDataMaintenance.model.ots.enterprise import Enterprise
from BaseDataMaintenance.model.ots.enterprise_check import enterprise_check
import queue
from BaseDataMaintenance.dataSource.searchPaddle import PaddleSpider
from uuid import uuid4
from BaseDataMaintenance.common.multiThread import MultiThreadHandler

from apscheduler.schedulers.blocking import BlockingScheduler

class CheckEnterprise():

    def __init__(self):
        self.ots_client = getConnect_ots()
        self.task_queue = queue.Queue(maxsize=1000)
        self.spider = PaddleSpider()
        self.set_enterprise = set()
        self.set_lock = RLock()


    def producer(self):

        try:
            bool_query = BoolQuery(must_queries=[RangeQuery("bid_number",1)]
                                   #,must_not_queries=[ExistsQuery("check_status")]
                                   )
            rows, next_token, total_count, is_all_succeed = self.ots_client.search("enterprise","enterprise_index",
                                                                                   SearchQuery(bool_query,sort=Sort(sorters=[FieldSort("bid_number",SortOrder.ASC)]) , limit=100, get_total_count=True),
                                                                                   ColumnsToGet(["name","bid_number"],return_type=ColumnReturnType.SPECIFIED))
            list_dict = getRow_ots(rows)
            for _dict in list_dict:
                with self.set_lock:
                    _name = _dict.get("name")
                    if _name  not in self.set_enterprise:
                        self.set_enterprise.add(_name)
                        _enterprise = Enterprise(_dict)
                        self.task_queue.put(_enterprise,True)

        except Exception as e:
            log("producer error of %s"%(str(e)))



    def comsumer(self):

        def _handle(_enterprise,result_queue):
            _uuid = uuid4().hex
            _name = _enterprise.getProperties().get("name","")
            bid_number = _enterprise.getProperties().get("bid_number",-1)
            _result = self.spider.search(_name)
            _status = 1
            if _result.get("exists"):
                new_name = _name
                _status = 2
            else:
                new_name = ""
            _province = _result.get("province","")
            _city = _result.get("city","")
            _district = _result.get("district","")
            _address = _result.get("address","")
            _dict = {"uuid":_uuid,"old_name":_name,"new_name":new_name,"rule":1,"create_time":getCurrent_date('%Y-%m-%d %H:%M:%S'),
                     "create_by":"paddle","status":_status,"province":_province,"city":_city,"district":_district,
                     "address":_address,"bid_number":bid_number}
            print(_dict)
            with self.set_lock:
                if _name in self.set_enterprise:
                    self.set_enterprise.remove(_name)
            # _enterprise_check = enterprise_check(_dict)
            # _enterprise_check.update_row(self.ots_client)
            # _enterprise.setValue("check_status","1",True)
            # _enterprise.update_row(self.ots_client)

        mt = MultiThreadHandler(self.task_queue,_handle,None,30)
        mt.run()



    def schedule(self):
        _scheduler = BlockingScheduler()
        _scheduler.add_job(self.producer,"cron",minute="*/1")
        _scheduler.add_job(self.comsumer,"cron",minute="*/1")
        _scheduler.start()


def start_enterpriseCheck():
    ce = CheckEnterprise()
    ce.schedule()

if __name__=="__main__":
    start_enterpriseCheck()