
from BaseDataMaintenance.model.ots_capacity.tyc_company import Tyc_company
from BaseDataMaintenance.dataSource.source import getConnect_ots_capacity
from apscheduler.schedulers.blocking import BlockingScheduler
from queue import Queue
from BaseDataMaintenance.common.Utils import *
from tablestore import *
from BaseDataMaintenance.common.multiThread import MultiThreadHandler

class Tyc_company_maintenance():

    def __init__(self):
        self.ots_capacity = getConnect_ots_capacity()
        self.remove_queue = Queue()

    def remove_handle(self,item,result_queue):
        # print(item)
        _tc = Tyc_company(item)
        _tc.delete_row(self.ots_capacity)

    def remove_processed(self):
        query = BoolQuery(must_queries=[TermQuery("processed",1)])
        rows,next_token,total_count,is_all_succeed = self.ots_capacity.search("tyc_company","tyc_company_index",
                                                                              SearchQuery(query,sort=Sort(sorters=[FieldSort("processed")]),limit=100,get_total_count=True),
                                                                              ColumnsToGet(return_type=ColumnReturnType.NONE))
        list_data = getRow_ots(rows)
        for _data in list_data:
            self.remove_queue.put(_data)
        while next_token:
            rows,next_token,total_count,is_all_succeed = self.ots_capacity.search("tyc_company","tyc_company_index",
                                                                                  SearchQuery(query,next_token=next_token,limit=100,get_total_count=True),
                                                                                  ColumnsToGet(return_type=ColumnReturnType.NONE))
            list_data = getRow_ots(rows)
            for _data in list_data:
                self.remove_queue.put(_data)
            if self.remove_queue.qsize()>=10000:
                break

    def remove_comsumer(self):
        mt = MultiThreadHandler(self.remove_queue,self.remove_handle,None,30)
        mt.run()




    def start_remove_processed(self):
        _schedule = BlockingScheduler()
        # _schedule.add_job(self.remove_processed,"cron",second="*/5")
        _schedule.add_job(self.remove_comsumer,"cron",second="*/5")
        _schedule.start()

def start_remove_processed_tyc_company():
    tcm = Tyc_company_maintenance()
    tcm.start_remove_processed()

if __name__ == '__main__':
    start_remove_processed_tyc_company()
