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


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 Experience(Base):
    """
    经验手册
    """
    __tablename__ = "experience"

    experienceid = Column(BigInteger, primary_key=True)
    title = Column(VARCHAR(255), comment="标题")
    content = Column(TEXT, comment="内容")
    businessid = Column(VARCHAR(32), comment="业务Id")
    business_name = Column(VARCHAR(255), comment="业务名称")
    host_type = Column(Integer, comment="资产类型（和下面hostid对应的资产不一定对应）")
    hostid = Column(BigInteger, comment="资产Id")
    versions = Column(Integer, comment="版本号")
    monitorid = Column(BigInteger, comment="监控Id")
    monitor_name = Column(VARCHAR(255), comment="监控名称")
    description = Column(VARCHAR(255), comment="告警描述")
    eventid = Column(BigInteger, comment="对应problem")


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

        self.table = Experience

    def select_all_from_allKeys(self, session, experienceid=None, title=None, description=None):
        """
        :param session: 指针
        :param experienceid:
        :param title:
        :param description:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="experienceid", value=experienceid)
            base.add_param(_key="title", value=title)
            base.add_param(_key="description", value=description)
            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_title(self, session, title):
        """
        :param session: 指针
        :param title:
        :return:
        """
        self.log.debug("查询数据库:")
        try:
            self.log.debug("删除相关测试数据")
            session.query(self.table).filter(self.table.title.like(str(title) + "%")).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, title=None, content=None, businessid=None, business_name=None, host_type=None,
                   hostid=None, versions=None, monitorid=None, monitor_name=None, description=None, eventid=None):
        """
        :param session: 指针
        :param title:
        :param content:
        :param businessid:
        :param business_name:
        :param host_type:
        :param hostid:
        :param versions:
        :param monitorid:
        :param monitor_name:
        :param description:
        :param eventid:
        :return:
        """
        self.log.debug("新增表数据:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="title", value=title)
            base.add_param(_key="content", value=content)
            base.add_param(_key="businessid", value=businessid)
            base.add_param(_key="business_name", value=business_name)
            base.add_param(_key="host_type", value=host_type)
            base.add_param(_key="hostid", value=hostid)
            base.add_param(_key="versions", value=versions)
            base.add_param(_key="monitorid", value=monitorid)
            base.add_param(_key="monitor_name", value=monitor_name)
            base.add_param(_key="description", value=description)
            base.add_param(_key="eventid", value=eventid)
            sql_rep = self.table(**base.param)

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