

from BaseDataMaintenance.model.ots.BaseModel import BaseModel
import random

ATTACHMENT_INIT = 0
ATTACHMENT_PROCESSED = 10
ATTACHMENT_PROCESSED_FAILED = 3
ATTACHMENT_TOOLARGE = -1

ATTACHMENT_UPDATE_DOCUMENT_STATUS = 1

ATTACHMENT_MC_TODEAL_FROM = 20
ATTACHMENT_MC_TODEAL_TO = 30

ATTACHMENT_MC_SUCCEED_FROM  = 31
ATTACHMENT_MC_SUCCEED_TO = 40
ATTACHMENT_MC_FAILED_FROM = 41
ATTACHMENT_MC_FAILED_TO = 50

#10M以上不进行处理
ATTACHMENT_LARGESIZE = 1024*1024*10

#1.5M一下通过maxcompute处理
ATTACHMENT_SMALL_SIZE = int(10*1024*1024)

attachment_table_name = "attachment"

attachment_filemd5 = "filemd5"
attachment_size = "size"
attachment_path = "path"
attachment_crtime = "crtime"
attachment_attachmenthtml = "attachmenthtml"
attachment_attachmentcon = "attachmentcon" #-1代表逻辑错误，-2接口挂了,-3文件格式错误
attachment_process_time = "process_time"
attachment_classification = "classification"
attachment_page_time = "page_time"
attachment_docids = "docids"
attachment_filetype = "filetype"
attachment_status = "status"
attachment_file_link = "filelink"
attachment_file_title = "filetitle"
attachment_recsize = "recsize"
attachment_base64 = "base64"
attachment_swfUrls = "swfUrls"
attachment_has_table = "has_table"
attachment_link_status = "link_status" #0-未连接 1-已经连接 2-找不到未连接公告

class attachment(BaseModel):

    #filemd5,path,crtime,attachmenthtml,attachmentcon,process_time,page_time,docids,filetype,status,sourcelink,filetitle

    def __init__(self,_dict):
        BaseModel.__init__(self)
        for k,v in _dict.items():
            self.setValue(k,v,True)
        self.table_name = "attachment"

    def getPrimary_keys(self):
        return ["filemd5"]

    def setStatusToMCDeal(self):
        self.setValue(attachment_status,random.randint(ATTACHMENT_MC_TODEAL_FROM,ATTACHMENT_MC_TODEAL_TO),True)

    # def delete_row(self,ots_client):
    #     raise NotImplementedError()


def splitBase64(str_base64,_dict,dis=ATTACHMENT_SMALL_SIZE):
    begin = 0
    list_attach = []
    part = -1
    while begin<len(str_base64):
        part += 1
        _base = str_base64[begin:begin+dis]
        new_dict = {}
        for k,v in _dict.items():
            new_dict[k] = v
        new_dict["base64"] = _base
        new_dict["part"] = part
        if part>=1:
            new_dict[attachment_filemd5] = "%s-%d"%(new_dict[attachment_filemd5],part)
        begin += dis
        _attach = attachment(new_dict)
        list_attach.append(_attach)
    return list_attach


from tablestore import *
from BaseDataMaintenance.dataSource.source import getConnect_ots
from BaseDataMaintenance.common.Utils import getRow_ots
def deleteMultiParts():

    ots_client = getConnect_ots()
    bool_query = BoolQuery(must_not_queries=[ExistsQuery("path")])

    columns = [attachment_status]
    rows, next_token, total_count, is_all_succeed = ots_client.search("attachment", "attachment_index",
                                                                           SearchQuery(bool_query ,sort=Sort(sorters=[FieldSort(attachment_crtime,SortOrder.ASC)]), limit=100, get_total_count=True),
                                                                           ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))

    list_data = getRow_ots(rows)
    print(total_count)
    for _data in list_data:
        _attach = attachment(_data)
        _attach.delete_row(ots_client)
    _count = 1
    while next_token:
        rows,next_token,total_count,is_all_succeed = ots_client.search("attachment","attachment_index",
                                                                       SearchQuery(bool_query,next_token=next_token,limit=100,get_total_count=True),
                                                                       ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))
        list_data = getRow_ots(rows)
        for _data in list_data:
            _attach = attachment(_data)
            _attach.delete_row(ots_client)
        _count += 1
        print("delete 100 with %d counts"%(_count))


def turnStatus():
    ots_client = getConnect_ots()
    bool_query = BoolQuery(must_queries=[TermQuery("status",0)
                                         #,WildcardQuery("path","*1627449078568.swf")
                                         ])

    columns = [attachment_status,attachment_size]
    rows, next_token, total_count, is_all_succeed = ots_client.search("attachment", "attachment_index",
                                                                      SearchQuery(bool_query ,sort=Sort(sorters=[FieldSort(attachment_crtime,SortOrder.ASC)]), limit=100, get_total_count=True),
                                                                      ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))

    list_data = getRow_ots(rows)
    print(total_count)
    for _data in list_data:
        _attach = attachment(_data)
        # if _attach.getProperties().get(attachment_size)<ATTACHMENT_SMALL_SIZE:
        #     _attach.setStatusToMCDeal()
        # else:
        _attach.setValue(attachment_status,-1,True)
        _attach.update_row(ots_client)
    _count = 1
    while next_token:
        rows,next_token,total_count,is_all_succeed = ots_client.search("attachment","attachment_index",
                                                                       SearchQuery(bool_query,next_token=next_token,limit=100,get_total_count=True),
                                                                       ColumnsToGet(columns,return_type=ColumnReturnType.SPECIFIED))
        list_data = getRow_ots(rows)
        for _data in list_data:
            _attach = attachment(_data)
            # if _attach.getProperties().get(attachment_size)<ATTACHMENT_SMALL_SIZE:
            #     _attach.setStatusToMCDeal()
            # else:
            _attach.setValue(attachment_status,-1,True)
            _attach.update_row(ots_client)
        _count += 1
        print("update 100 with %d counts"%(_count))

if __name__=="__main__":
    # deleteMultiParts()
    turnStatus()

