# -*- coding: utf_8 -*-
# 表名: triggers
# 作者: 陈磊
# 时间: 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 Triggers(Base):
    """
    触发器信息表
    """
    __tablename__ = "triggers"

    triggerid = Column(BigInteger, primary_key=True)
    expression = Column(VARCHAR(2048))
    description = Column(VARCHAR(255))
    url = Column(VARCHAR(255))
    status = Column(Integer)
    value = Column(Integer)
    priority = Column(Integer)
    comments = Column(TEXT)
    error = Column(VARCHAR(2048))
    templateid = Column(BigInteger)
    type = Column(Integer)
    state = Column(Integer)
    flags = Column(Integer)
    recovery_mode = Column(Integer)
    recovery_expression = Column(VARCHAR(2048))
    correlation_mode = Column(Integer)
    correlation_tag = Column(VARCHAR(255))
    manual_close = Column(Integer)
    details = Column(VARCHAR(255))


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

        self.table = Triggers

    def select_all_from_allKeys(self, session, triggerid=None, description=None, templateid=None):
        """
        :param session: 指针
        :param triggerid:
        :param templateid:
        :param description:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        self.log.debug("triggerid: %s" % triggerid)
        self.log.debug("description: %s" % description)
        self.log.debug("templateid: %s" % templateid)
        try:
            base = UtilsDataBase()
            base.add_param(_key="triggerid", value=triggerid)
            base.add_param(_key="description", value=description)
            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_like_description(self, session, description):
        """
        :param session: 指针
        :param description:
        :return:
        """
        self.log.debug("查询数据库:")
        try:
            self.log.debug("删除相关测试数据")
            session.query(self.table).filter(self.table.description.like(description + "%")).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
