# -*- coding: utf_8 -*-
# 表名: dchecks
# 作者: 陈磊
# 时间: 2019-12-06


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 Dchecks(Base):
    """
    发现规则检测表
    """
    __tablename__ = "dchecks"

    dcheckid = Column(BigInteger, primary_key=True)
    druleid = Column(BigInteger)
    type = Column(Integer)
    key_ = Column(VARCHAR(255))
    snmp_community = Column(VARCHAR(255))
    ports = Column(VARCHAR(255))
    snmpv3_securityname = Column(VARCHAR(64))
    snmpv3_securitylevel = Column(Integer)
    snmpv3_authpassphrase = Column(VARCHAR(64))
    snmpv3_privpassphrase = Column(VARCHAR(64))
    uniq = Column(Integer)
    snmpv3_authprotocol = Column(Integer)
    snmpv3_privprotocol = Column(Integer)
    snmpv3_contextname = Column(VARCHAR(255))
    # 1:DNS 2:IP
    host_source = Column(Integer)
    name_source = Column(Integer)


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

        self.table = Dchecks

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