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


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 Hosts(Base):
    """
    主机信息表
    """
    __tablename__ = "hosts"

    hostid = Column(BigInteger, primary_key=True)
    proxy_hostid = Column(BigInteger)
    host = Column(VARCHAR(128))
    status = Column(Integer)
    disable_until = Column(Integer)
    error = Column(VARCHAR(2048))
    available = Column(Integer)
    errors_from = Column(Integer)
    lastaccess = Column(Integer)
    ipmi_authtype = Column(Integer)
    ipmi_privilege = Column(Integer)
    ipmi_username = Column(VARCHAR(16))
    ipmi_password = Column(VARCHAR(20))
    ipmi_disable_until = Column(Integer)
    ipmi_available = Column(Integer)
    snmp_disable_until = Column(Integer)
    snmp_available = Column(Integer)
    maintenanceid = Column(BigInteger)
    maintenance_status = Column(Integer)
    maintenance_type = Column(Integer)
    maintenance_from = Column(Integer)
    ipmi_errors_from = Column(Integer)
    snmp_errors_from = Column(Integer)
    ipmi_error = Column(VARCHAR(2048))
    snmp_error = Column(VARCHAR(2048))
    jmx_disable_until = Column(Integer)
    jmx_available = Column(Integer)
    jmx_errors_from = Column(Integer)
    jmx_error = Column(VARCHAR(2048))
    name = Column(VARCHAR(128))
    flags = Column(Integer)
    templateid = Column(BigInteger)
    description = Column(TEXT)
    tls_connect = Column(Integer)
    tls_accept = Column(Integer)
    tls_issuer = Column(VARCHAR(1024))
    tls_subject = Column(VARCHAR(1024))
    tls_psk_identity = Column(VARCHAR(128))
    tls_psk = Column(VARCHAR(512))
    proxy_address = Column(VARCHAR(255))
    auto_compress = Column(Integer)


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

        self.table = Hosts

    def select_all_from_allKeys(self, session, hostid=None, host=None, name=None):
        """
        :param session: 指针
        :param hostid:
        :param host:
        :param name:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        self.log.debug("hostid: %s" % hostid)
        self.log.debug("host: %s" % host)
        self.log.debug("name: %s" % name)
        try:
            base = UtilsDataBase()
            base.add_param(_key="hostid", value=hostid)
            base.add_param(_key="host", value=host)
            base.add_param(_key="name", value=name)
            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 select_like_name(self, session, name):
        """
        :param session: 指针
        :param name:
        :return:
        """
        self.log.debug("查询数据库:")
        try:
            self.log.debug("查询相关测试数据")
            sql = session.query(self.table).filter(self.table.name.like(name + "%")).all()
            session.close()
            self.log.debug("删除成功")
            return sql
        except UtilsDataBase().errors as error:
            self.log.error("异常:")
            self.log.error(error)
            session.close()
            return error

    def delete_like_name(self, session, name):
        """
        :param session: 指针
        :param name:
        :return:
        """
        self.log.debug("查询数据库:")
        try:
            self.log.debug("删除相关测试数据")
            session.query(self.table).filter(self.table.name.like(name + "%")).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
