from datetime import datetime, time
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.role_do import RobotRole
from module_admin.entity.vo.role_vo import RoleModel, RolePageQueryModel
from utils.page_util import PageUtil


class RoleDao:
    """
    系统角色模块数据库操作层
    """

    @classmethod
    async def get_role_detail_by_id(cls, db: AsyncSession, robot_role_id: int):
        """
        根据机器人角色ID获取系统角色详细信息

        :param db: orm对象
        :param robot_role_id: 机器人角色ID
        :return: 系统角色信息对象
        """
        role_info = (
            (
                await db.execute(
                    select(RobotRole)
                    .where(
                        RobotRole.robot_role_id == robot_role_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return role_info

    @classmethod
    async def get_role_detail_by_info(cls, db: AsyncSession, role: RoleModel):
        """
        根据系统角色参数获取系统角色信息

        :param db: orm对象
        :param role: 系统角色参数对象
        :return: 系统角色信息对象
        """
        role_info = (
            (
                await db.execute(
                    select(RobotRole).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return role_info

    @classmethod
    async def get_role_list(cls, db: AsyncSession, query_object: RolePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取系统角色列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 系统角色列表信息对象
        """
        query = (
            select(RobotRole)
            .where(
                RobotRole.rolename.like(f'%{query_object.rolename}%') if query_object.rolename else True,
                RobotRole.detail == query_object.detail if query_object.detail else True,
                RobotRole.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,
                RobotRole.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(RobotRole.robot_role_id)
            .distinct()
        )
        role_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return role_list

    @classmethod
    async def add_role_dao(cls, db: AsyncSession, role: RoleModel):
        """
        新增系统角色数据库操作

        :param db: orm对象
        :param role: 系统角色对象
        :return:
        """
        db_role = RobotRole(**role.model_dump(exclude={}))
        db.add(db_role)
        await db.flush()

        return db_role

    @classmethod
    async def edit_role_dao(cls, db: AsyncSession, role: dict):
        """
        编辑系统角色数据库操作

        :param db: orm对象
        :param role: 需要更新的系统角色字典
        :return:
        """
        await db.execute(update(RobotRole), [role])

    @classmethod
    async def delete_role_dao(cls, db: AsyncSession, role: RoleModel):
        """
        删除系统角色数据库操作

        :param db: orm对象
        :param role: 系统角色对象
        :return:
        """
        await db.execute(delete(RobotRole).where(RobotRole.robot_role_id.in_([role.robot_role_id])))

