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


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 Problem(Base):
    """
    告警信息表
    """
    __tablename__ = "problem"

    eventid = Column(BigInteger, primary_key=True)
    source = Column(Integer)
    object = Column(Integer)
    objectid = Column(BigInteger)
    clock = Column(Integer)
    ns = Column(Integer)
    r_eventid = Column(BigInteger)
    r_clock = Column(Integer)
    r_ns = Column(Integer)
    correlationid = Column(BigInteger)
    userid = Column(BigInteger)
    name = Column(VARCHAR(2048))
    acknowledged = Column(Integer)
    severity = Column(Integer)


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

        self.table = Problem

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

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

    def insert_one(self, session, eventid=None, source=None, _object=None, objectid=None, clock=None,
                   r_eventid=None, r_clock=None, r_ns=None, correlationid=None, userid=None,
                   acknowledged=None, ns=None, name=None, severity=None):
        """
        :param session: 指针
        :param eventid:
        :param source:
        :param _object:
        :param objectid:
        :param clock:
        :param r_eventid:
        :param r_clock:
        :param r_ns:
        :param correlationid:
        :param userid:
        :param acknowledged:
        :param ns:
        :param name:
        :param severity:
        :return:
        """
        self.log.debug("新增表数据:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="eventid", value=eventid)
            base.add_param(_key="source", value=source)
            base.add_param(_key="object", value=_object)
            base.add_param(_key="objectid", value=objectid)
            base.add_param(_key="clock", value=clock)
            base.add_param(_key="ns", value=ns)
            base.add_param(_key="r_eventid", value=r_eventid)
            base.add_param(_key="r_clock", value=r_clock)
            base.add_param(_key="r_ns", value=r_ns)
            base.add_param(_key="correlationid", value=correlationid)
            base.add_param(_key="userid", value=userid)
            base.add_param(_key="name", value=name)
            base.add_param(_key="acknowledged", value=acknowledged)
            base.add_param(_key="severity", value=severity)
            sql_rep = self.table(**base.param)

            session.add(sql_rep)
            session.commit()
            eventid = sql_rep.eventid
            self.log.debug("新增的problem:%s" % eventid)
            session.close()
            return eventid
        except UtilsDataBase().errors as error:
            self.log.error("异常:")
            self.log.error(error)
            session.close()
            return error
