

from tablestore import *
from BaseDataMaintenance.model.ots.designed_project import designed_project

from BaseDataMaintenance.dataSource.source import getConnect_ots
from BaseDataMaintenance.common.Utils import *
from queue import Queue

from BaseDataMaintenance.common.multiThread import MultiThreadHandler

import json

def turnPageTime():
    ots_client = getConnect_ots()

    task_queue = Queue()

    bool_query = BoolQuery(must_queries=[RangeQuery("update_time","2022-08-03")])
    rows,next_token,total_count,is_all_succeed = ots_client.search("designed_project","designed_project_index",
                                                                   SearchQuery(bool_query,sort=Sort(sorters=[FieldSort("id")]),limit=100,get_total_count=True),
                                                                   columns_to_get=ColumnsToGet(["update_time","page_time","follows"],return_type=ColumnReturnType.SPECIFIED))
    list_data = getRow_ots(rows)
    for _data in list_data:
        task_queue.put(_data)
    _count = len(list_data)
    log("count/total_count:%d/%d"%(_count,total_count))


    while next_token:
        rows,next_token,total_count,is_all_succeed = ots_client.search("designed_project","designed_project_index",
                                                                       SearchQuery(bool_query,next_token=next_token,limit=100,get_total_count=True),
                                                                       columns_to_get=ColumnsToGet(["update_time","page_time","follows"],return_type=ColumnReturnType.SPECIFIED))
        list_data = getRow_ots(rows)
        for _data in list_data:
            task_queue.put(_data)

        _count += len(list_data)
        log("count/total_count:%d/%d"%(_count,total_count))

    def _handle(item,result_queue):


        list_follow = json.loads(item.get("follows","[]"))

        item.pop("follows")
        dp = designed_project(item)
        if len(list_follow)==0:
            dp.setValue("page_time",dp.getProperties().get("update_time"))
        else:
            list_follow.sort(key=lambda x:x.get("mdate",0),reverse=True)
            _page_time = list_follow[0].get("mdate")[:10]
            dp.setValue("page_time",_page_time)



        dp.update_row(ots_client)
    mt = MultiThreadHandler(task_queue,_handle,None,30)
    mt.run()



if __name__ == '__main__':
    turnPageTime()