# -*- coding: utf_8 -*-
# 表名: items
# 作者: 陈磊
# 时间: 2019-11-21


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, VARCHAR, SMALLINT, DATETIME, DECIMAL, BigInteger, FLOAT, NUMERIC, TEXT
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
from sqlalchemy import or_

from WorkUtils.UtilsLog import UtilsLog
from WorkUtils.UtilsDataBase import UtilsDataBase

import time

Base = declarative_base()


class Items(Base):
    """
    监控信息表
    """
    __tablename__ = "items"

    itemid = Column(BigInteger, primary_key=True)
    type = Column(Integer)
    snmp_community = Column(VARCHAR(64))
    snmp_oid = Column(VARCHAR(512))
    hostid = Column(BigInteger)
    name = Column(VARCHAR(255))
    key_ = Column(VARCHAR(255))
    delay = Column(VARCHAR(1024))
    history = Column(VARCHAR(255))
    trends = Column(VARCHAR(255))
    status = Column(Integer)
    value_type = Column(Integer)
    trapper_hosts = Column(VARCHAR(255))
    units = Column(VARCHAR(255))
    snmpv3_securityname = Column(VARCHAR(64))
    snmpv3_securitylevel = Column(Integer)
    snmpv3_authpassphrase = Column(Integer)
    snmpv3_privpassphrase = Column(VARCHAR(64))
    formula = Column(VARCHAR(255))
    error = Column(VARCHAR(2048))
    lastlogsize = Column(BigInteger)
    logtimefmt = Column(VARCHAR(64))
    templateid = Column(BigInteger)
    valuemapid = Column(BigInteger)
    params = Column(TEXT)
    ipmi_sensor = Column(VARCHAR(128))
    authtype = Column(Integer)
    username = Column(VARCHAR(64))
    password = Column(VARCHAR(64))
    publickey = Column(VARCHAR(64))
    privatekey = Column(VARCHAR(64))
    mtime = Column(Integer)
    flags = Column(Integer)
    interfaceid = Column(BigInteger)
    port = Column(VARCHAR(64))
    description = Column(TEXT)
    inventory_link = Column(Integer)
    lifetime = Column(VARCHAR(255))
    snmpv3_authprotocol = Column(Integer)
    snmpv3_privprotocol = Column(Integer)
    state = Column(Integer)
    snmpv3_contextname = Column(VARCHAR(255))
    evaltype = Column(Integer)
    jmx_endpoint = Column(VARCHAR(255))
    master_itemid = Column(BigInteger)
    timeout = Column(VARCHAR(255))
    url = Column(VARCHAR(2048))
    query_fields = Column(VARCHAR(2048))
    posts = Column(TEXT)
    status_codes = Column(VARCHAR(255))
    follow_redirects = Column(Integer)
    post_type = Column(Integer)
    http_proxy = Column(VARCHAR(255))
    headers = Column(TEXT)
    retrieve_mode = Column(Integer)
    request_method = Column(Integer)
    output_format = Column(Integer)
    ssl_cert_file = Column(VARCHAR(255))
    ssl_key_file = Column(VARCHAR(255))
    ssl_key_password = Column(VARCHAR(64))
    verify_peer = Column(Integer)
    verify_host = Column(Integer)
    allow_traps = Column(Integer)


class DataItems(object):
    def __init__(self):
        self.log = UtilsLog()
        self.log.info(self.__class__)

        self.table = Items

    def select_all_from_allKeys(self, session, itemid=None, hostid=None, templateid=None):
        """
        :param session: 指针
        :param itemid:
        :param hostid:
        :param templateid:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        self.log.debug("itemid: %s" % itemid)
        self.log.debug("hostid: %s" % hostid)
        self.log.debug("templateid: %s" % templateid)
        try:
            base = UtilsDataBase()
            base.add_param(_key="itemid", value=itemid)
            base.add_param(_key="hostid", value=hostid)
            base.add_param(_key="templateid", value=templateid)
            sql_rep = session.query(self.table).filter_by(**base.param).all()
            self.log.debug(sql_rep)
            session.close()
            return sql_rep
        except UtilsDataBase().errors as error:
            self.log.error("异常:")
            self.log.error(error)
            session.close()
            return error

    def delete_hostid(self, session, hostid):
        """
        :param session: 指针
        :param hostid:
        :return:
        """
        self.log.debug("查询数据库:")
        try:
            self.log.debug("删除相关测试数据")
            session.query(self.table).filter_by(hostid=hostid).delete(synchronize_session=False)
            # session.delete(sql)
            session.commit()
            session.close()
            self.log.debug("删除成功")
            return True
        except UtilsDataBase().errors as error:
            self.log.error("异常:")
            self.log.error(error)
            session.close()
            return error
