

from tablestore import *
from apscheduler.schedulers.blocking import BlockingScheduler
from BaseDataMaintenance.dataSource.source import getConnect_ots
from BaseDataMaintenance.common.Utils import *
from BaseDataMaintenance.model.ots.document import Document
import random


class TurnHandInputDocument():


    def __init__(self):
        self.document_table = "document"
        self.document_table_index = "document_index"
        self.ots_client = getConnect_ots()



    def turn_status(self):

        bool_query = BoolQuery(must_queries=[RangeQuery("status",101,120,include_lower=True,include_upper=True),
                                             TermQuery("web_source_no",'000000')])

        columns = ["partitionkey","docid","status"]
        rows, next_token, total_count, is_all_succeed = self.ots_client.search(self.document_table, self.document_table_index,
                                                                          SearchQuery(bool_query ,sort=Sort(sorters=[FieldSort("docid",SortOrder.ASC)]), limit=100, get_total_count=True),
                                                                          ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))
        list_data = getRow_ots(rows)

        log("processed number %d"%(len(list_data)))
        for _data in list_data:
            _document = Document(_data)
            new_status = random.randint(201,300)
            _document.setValue("status",new_status,True)
            _document.update_row(self.ots_client)

    def scheduler(self):
        _scheduler = BlockingScheduler()
        _scheduler.add_job(self.turn_status,"cron",second="*/30")
        _scheduler.start()

def startSychro_handInput():
    turnStatus = TurnHandInputDocument()
    turnStatus.scheduler()

if __name__=="__main__":
    startSychro_handInput()