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


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 Businesses(Base):
    """
    业务表
    """
    __tablename__ = "businesses"

    businessid = Column(BigInteger, primary_key=True)
    name = Column(VARCHAR(255), comment="名称")
    description = Column(VARCHAR(255), comment="描述")
    type = Column(Integer, comment="业务类型")
    principal_name = Column(VARCHAR(50), comment="业务负责人姓名")
    principal_phone = Column(VARCHAR(20), comment="业务负责人手机号")
    memberid = Column(BigInteger, comment="运维人员")
    create_time = Column(BigInteger, comment="创建时间")


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

        self.table = Businesses

    def select_all_from_allKeys(self, session, businessid=None, name=None, _type=None):
        """
        :param session: 指针
        :param businessid:
        :param name:
        :param _type:
        :return: 查询结果
        """
        self.log.debug("查询数据库:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="businessid", value=businessid)
            base.add_param(_key="name", value=name)
            base.add_param(_key="type", value=_type)
            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 insert_one(self, session, businessid=None, name=None, description=None, _type=None,
                   principal_name=None, principal_phone=None, memberid=None, create_time=None):
        """
        :param session: 指针
        :param businessid:
        :param name:
        :param description:
        :param _type:
        :param principal_name:
        :param principal_phone:
        :param memberid:
        :param create_time:
        :return:
        """
        self.log.debug("新增表数据:")
        try:
            base = UtilsDataBase()
            base.add_param(_key="businessid", value=businessid)
            base.add_param(_key="name", value=name)
            base.add_param(_key="description", value=description)
            base.add_param(_key="type", value=_type)
            base.add_param(_key="principal_name", value=principal_name)
            base.add_param(_key="principal_phone", value=principal_phone)
            base.add_param(_key="memberid", value=memberid)
            base.add_param(_key="create_time", value=create_time)
            sql_rep = self.table(**base.param)

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