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


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, VARCHAR, BIGINT, 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 HostDiscovery(Base):
    """
    主机发现表
    """
    __tablename__ = "host_discovery"

    hostid = Column(BigInteger, primary_key=True)
    parent_hostid = Column(BigInteger)
    parent_itemid = Column(BigInteger)
    host = Column(VARCHAR(64))
    lastcheck = Column(Integer)
    ts_delete = Column(Integer)


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

        self.table = HostDiscovery

    def select_all_from_allKeys(self, session, hostid=None, parent_itemid=None):
        """
        :param session: 指针
        :param hostid:
        :param parent_itemid:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="hostid", value=hostid)
            base.add_param(_key="parent_itemid", value=parent_itemid)
            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
