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


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 HostsItems(Base):
    """
    设备监控项
    """
    __tablename__ = "hosts_items"

    id = Column(BigInteger, primary_key=True)
    host_id = Column(BigInteger, comment="设备编号")
    name = Column(VARCHAR(255), comment="监控项名")
    key_ = Column(VARCHAR(255), comment="监控项的值")


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

        self.table = HostsItems

    def select_all_from_allKeys(self, session, _id=None, host_id=None, name=None, key_=None):
        """
        :param session: 指针
        :param _id:
        :param host_id:
        :param name:
        :param key_:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="id", value=_id)
            base.add_param(_key="host_id", value=host_id)
            base.add_param(_key="name", value=name)
            base.add_param(_key="key_", value=key_)
            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_host_id(self, session, host_id):
        """
        :param session: 指针
        :param host_id:
        :return:
        """
        self.log.debug("查询数据库:")
        try:
            self.log.debug("删除相关测试数据")
            session.query(self.table).filter_by(host_id=host_id).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, _id=None, host_id=None, name=None, key_=None):
        """
        :param session: 指针
        :param _id:
        :param host_id:
        :param name:
        :param key_:
        :return:
        """
        self.log.debug("新增表数据:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="id", value=_id)
            base.add_param(_key="host_id", value=host_id)
            base.add_param(_key="name", value=name)
            base.add_param(_key="key_", value=key_)
            sql_rep = self.table(**base.param)

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