from datetime import datetime, time
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.sys_alert_do import SysAlert
from module_admin.entity.vo.sys_alert_vo import Sys_alertModel, Sys_alertPageQueryModel
from utils.page_util import PageUtil


class Sys_alertDao:
    """
    系统告警模块数据库操作层
    """

    @classmethod
    async def get_sys_alert_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据主键 自增获取系统告警详细信息

        :param db: orm对象
        :param id: 主键 自增
        :return: 系统告警信息对象
        """
        sys_alert_info = (
            (
                await db.execute(
                    select(SysAlert)
                    .where(
                        SysAlert.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return sys_alert_info

    @classmethod
    async def get_sys_alert_detail_by_info(cls, db: AsyncSession, sys_alert: Sys_alertModel):
        """
        根据系统告警参数获取系统告警信息

        :param db: orm对象
        :param sys_alert: 系统告警参数对象
        :return: 系统告警信息对象
        """
        sys_alert_info = (
            (
                await db.execute(
                    select(SysAlert).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return sys_alert_info

    @classmethod
    async def get_sys_alert_list(cls, db: AsyncSession, query_object: Sys_alertPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取系统告警列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 系统告警列表信息对象
        """
        query = (
            select(SysAlert)
            .where(
                SysAlert.detail.like(f'%{query_object.detail}%') if query_object.detail else True,
                SysAlert.type == query_object.type if query_object.type else True,
                SysAlert.status == query_object.status if query_object.status else True,
                SysAlert.create_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_create_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_create_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_create_time and query_object.end_create_time
                else True,
                SysAlert.update_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_update_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_update_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_update_time and query_object.end_update_time
                else True,
            )
            .order_by(SysAlert.id)
            .distinct()
        )
        sys_alert_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return sys_alert_list

    @classmethod
    async def add_sys_alert_dao(cls, db: AsyncSession, sys_alert: Sys_alertModel):
        """
        新增系统告警数据库操作

        :param db: orm对象
        :param sys_alert: 系统告警对象
        :return:
        """
        db_sys_alert = SysAlert(**sys_alert.model_dump(exclude={}))
        db.add(db_sys_alert)
        await db.flush()

        return db_sys_alert

    @classmethod
    async def edit_sys_alert_dao(cls, db: AsyncSession, sys_alert: dict):
        """
        编辑系统告警数据库操作

        :param db: orm对象
        :param sys_alert: 需要更新的系统告警字典
        :return:
        """
        await db.execute(update(SysAlert), [sys_alert])

    @classmethod
    async def delete_sys_alert_dao(cls, db: AsyncSession, sys_alert: Sys_alertModel):
        """
        删除系统告警数据库操作

        :param db: orm对象
        :param sys_alert: 系统告警对象
        :return:
        """
        await db.execute(delete(SysAlert).where(SysAlert.id.in_([sys_alert.id])))

