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


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 Functions(Base):
    """
    监控与触发器关系表
    """
    __tablename__ = "functions"

    functionid = Column(BigInteger, primary_key=True)
    itemid = Column(BigInteger)
    triggerid = Column(BigInteger)
    name = Column(VARCHAR(12))
    parameter = Column(VARCHAR(255))


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

        self.table = Functions

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