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


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 Scripts(Base):
    """
    脚本信息表
    """
    __tablename__ = "scripts"

    scriptid = Column(Integer, primary_key=True)
    name = Column(VARCHAR(255))
    command = Column(VARCHAR(255))
    host_access = Column(Integer)
    usrgrpid = Column(Integer)
    groupid = Column(Integer)
    description = Column(TEXT)
    confirmation = Column(VARCHAR(255))
    type = Column(Integer)
    execute_on = Column(Integer)


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

        self.table = Scripts

    def select_all_from_allKeys(self, session, scriptid=None, name=None, command=None, host_access=None, usrgrpid=None, groupid=None,
                                description=None, confirmation=None, _type=None, execute_on=None):
        """
        :param session: 指针
        :param scriptid:
        :param name:
        :param command:
        :param host_access:
        :param usrgrpid:
        :param groupid:
        :param description:
        :param confirmation:
        :param _type:
        :param execute_on:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="scriptid", value=scriptid)
            base.add_param(_key="name", value=name)
            base.add_param(_key="command", value=command)
            base.add_param(_key="host_access", value=host_access)
            base.add_param(_key="usrgrpid", value=usrgrpid)
            base.add_param(_key="groupid", value=groupid)
            base.add_param(_key="description", value=description)
            base.add_param(_key="confirmation", value=confirmation)
            base.add_param(_key="type", value=_type)
            base.add_param(_key="execute_on", value=execute_on)
            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
