

from BaseDataMaintenance.model.oracle.BaseModel import BaseModel
from datetime import datetime
from BaseDataMaintenance.common.Utils import getCurrent_date,log

T_SHEN_PI_XIANG_MU_ID = "id"
T_SHEN_PI_XIANG_MU_WEB_SOURCE_NO = "web_source_no"
T_SHEN_PI_XIANG_MU_AREA = "area"
T_SHEN_PI_XIANG_MU_PROVINCE = "province"
T_SHEN_PI_XIANG_MU_CITY = "city"
T_SHEN_PI_XIANG_MU_DISTRICT = "district"
T_SHEN_PI_XIANG_MU_WEB_SOURCE_NAME = "web_source_name"
T_SHEN_PI_XIANG_MU_SP_TYPE = "sp_type"
T_SHEN_PI_XIANG_MU_DETAILLINK = "detaillink"
T_SHEN_PI_XIANG_MU_RECORD_ID = "record_id"
T_SHEN_PI_XIANG_MU_PAGE_TITLE = "page_title"
T_SHEN_PI_XIANG_MU_PAGE_TIME = "page_time"
T_SHEN_PI_XIANG_MU_PAGE_CONTENT = "page_content"
T_SHEN_PI_XIANG_MU_PAGE_CODE = "page_code"
T_SHEN_PI_XIANG_MU_CREATE_TIME = "create_time"
T_SHEN_PI_XIANG_MU_SOURCE_TYPE = "source_type"
T_SHEN_PI_XIANG_MU_SOURCE_STAGE = "source_stage"
T_SHEN_PI_XIANG_MU_ATTACHMENT_PATH = "attachment_path"
T_SHEN_PI_XIANG_MU_PAGE_ATTACHMENTS = "page_attachments"


class T_SHEN_PI_XIANG_MU(BaseModel):


    def __init__(self,_dict):
        self.all_columns = []
        for k,v in _dict.items():
            self.setValue(k,v,True)



    def getProperties(self):
        return self.__dict__

    def getProperties_ots(self):
        new_dict = {}
        for k,v in self.__dict__.items():
            if k=="all_columns":
                continue
            if v is not None:
                if isinstance(v,(str,int,float)):
                    pass
                elif isinstance(v,(datetime)):
                    v = v.strftime("%Y-%m-%d %H:%M:%S")
                else:
                    v = str(v)
                new_dict[k] = v
        docid = int(new_dict.get("id",0))
        partition_key = docid%500+1

        new_dict["partitionkey"] = partition_key
        new_dict["docid"] = docid
        new_dict["original_id"] = str(new_dict.get(T_SHEN_PI_XIANG_MU_ID))
        new_dict["uuid"] = str(new_dict.get(T_SHEN_PI_XIANG_MU_ID))
        new_dict.pop(T_SHEN_PI_XIANG_MU_ID)

        try:
            if new_dict.get(T_SHEN_PI_XIANG_MU_SOURCE_STAGE) is not None:
                new_dict[T_SHEN_PI_XIANG_MU_SOURCE_STAGE] = int(new_dict.get(T_SHEN_PI_XIANG_MU_SOURCE_STAGE,0))
            if new_dict.get(T_SHEN_PI_XIANG_MU_SOURCE_TYPE) is not None:
                new_dict[T_SHEN_PI_XIANG_MU_SOURCE_TYPE] = int(new_dict.get(T_SHEN_PI_XIANG_MU_SOURCE_TYPE,0))
        except Exception as e:
            pass


        new_dict["crtime"] = new_dict.get(T_SHEN_PI_XIANG_MU_CREATE_TIME)
        new_dict["docchannel"] = 302

        new_dict["doctitle"] = new_dict.get(T_SHEN_PI_XIANG_MU_PAGE_TITLE,"")
        new_dict.pop(T_SHEN_PI_XIANG_MU_PAGE_TITLE)

        new_dict["dochtmlcon"] = new_dict.get(T_SHEN_PI_XIANG_MU_PAGE_CONTENT,"")
        if T_SHEN_PI_XIANG_MU_PAGE_CONTENT in new_dict:
            new_dict.pop(T_SHEN_PI_XIANG_MU_PAGE_CONTENT)

        new_dict["detail_link"] = new_dict.get(T_SHEN_PI_XIANG_MU_DETAILLINK,"")
        if T_SHEN_PI_XIANG_MU_DETAILLINK in new_dict:
            new_dict.pop(T_SHEN_PI_XIANG_MU_DETAILLINK)

        new_dict[T_SHEN_PI_XIANG_MU_PAGE_ATTACHMENTS] = new_dict.get(T_SHEN_PI_XIANG_MU_ATTACHMENT_PATH,"[]")

        opertime = getCurrent_date(format="%Y-%m-%d %H:%M:%S")
        publishtime = "%s %s"%(new_dict.get("page_time",""),opertime.split(" ")[1])
        new_dict["opertime"] = opertime
        new_dict["publishtime"] = publishtime
        if "docchannel" in new_dict:
            new_dict["original_docchannel"] = new_dict["docchannel"]
        return new_dict

    @staticmethod
    def get_max_id(conn):
        cursor = conn.cursor()
        sql = "select max(id) from %s"%("bxkc.t_shen_pi_xiang_mu_new")

        cursor.execute(sql)
        rows = cursor.fetchall()

        if len(rows)>0:
            max_id = rows[0][0]
            log("selext_max_id:%d"%(max_id))
            return max_id
        return None


    @staticmethod
    def select_rows(conn,_id,limit=500):
        list_result = []
        s_limit = ""
        if limit is not None:
            s_limit = "limit %d"%limit
        s_where = " where id=%d "%(_id)

        cursor = conn.cursor()
        sql = "select %s from %s %s "%("*","bxkc.t_shen_pi_xiang_mu_new",s_where)
        log("select rows:%s"%(sql))
        cursor.execute(sql)

        vol = cursor.description
        rows = cursor.fetchall()
        for row in rows:
            _dict = {}
            for _vol,_val in zip(vol,row):
                _name = str(_vol[0]).lower()
                _dict[_name] = _val
            list_result.append(T_SHEN_PI_XIANG_MU(_dict))
        return list_result