from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.robot_role_do import RobotRole
from module_admin.entity.do.robot_role_pairing_do  import RobotRolePairing

from module_admin.entity.vo.robot_role_pairing_vo import PairingModel, PairingPageQueryModel
from utils.page_util import PageUtil


class PairingDao:
    """
    角色-机器人-映射模块数据库操作层
    """

    @classmethod
    async def edit_pairing_status(cls, db: AsyncSession,robot_id: int = None, pairing_id: int = None, status: str = '0'):

        condition = list()
        if robot_id:
            condition.append(RobotRolePairing.robot_id == robot_id)
        if pairing_id:
            condition.append(RobotRolePairing.pairing_id == pairing_id)
        query = (
            update(RobotRolePairing)
            .values(status=status)
            .where(
                * condition
            )
        )
        await db.execute(query)

    @classmethod
    async def get_role_pairing_list(cls, db: AsyncSession, robot_id: int):

        query = (
            select(RobotRolePairing.pairing_id, RobotRole.robot_role_id ,RobotRole.rolename, RobotRole.detail, RobotRolePairing.status)
            .select_from(RobotRolePairing)
            .join(RobotRole, RobotRole.robot_role_id == RobotRolePairing.robot_role_id)
            .where(RobotRolePairing.robot_id == robot_id)
        )
        role_pairing_list = (
            await db.execute(query)
            ).mappings().all()
        return role_pairing_list
    @classmethod
    async def get_pairing_detail_by_id(cls, db: AsyncSession, pairing_id: int):
        """
        根据主键ID获取角色-机器人-映射详细信息

        :param db: orm对象
        :param pairing_id: 主键ID
        :return: 角色-机器人-映射信息对象
        """
        pairing_info = (
            (
                await db.execute(
                    select(RobotRolePairing)
                    .where(
                        RobotRolePairing.pairing_id == pairing_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return pairing_info

    @classmethod
    async def get_pairing_detail_by_info(cls, db: AsyncSession, pairing: PairingModel):
        """
        根据角色-机器人-映射参数获取角色-机器人-映射信息

        :param db: orm对象
        :param pairing: 角色-机器人-映射参数对象
        :return: 角色-机器人-映射信息对象
        """
        pairing_info = (
            (
                await db.execute(
                    select(RobotRolePairing).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return pairing_info

    @classmethod
    async def get_pairing_list(cls, db: AsyncSession, query_object: PairingPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取角色-机器人-映射列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 角色-机器人-映射列表信息对象
        """
        query = (
            select(RobotRolePairing)
            .where(
                RobotRolePairing.pairing_id == query_object.pairing_id if query_object.pairing_id else True,
                RobotRolePairing.robot_role_id == query_object.robot_role_id if query_object.robot_role_id else True,
                RobotRolePairing.robot_id == query_object.robot_id if query_object.robot_id else True,
                RobotRolePairing.status == query_object.status if query_object.status else True,
            )
            .order_by(RobotRolePairing.pairing_id)
            .distinct()
        )
        pairing_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return pairing_list

    @classmethod
    async def add_pairing_dao(cls, db: AsyncSession, pairing: PairingModel):
        """
        新增角色-机器人-映射数据库操作

        :param db: orm对象
        :param pairing: 角色-机器人-映射对象
        :return:
        """
        db_pairing = RobotRolePairing(**pairing.model_dump(exclude={}))
        db.add(db_pairing)
        await db.flush()

        return db_pairing

    @classmethod
    async def edit_pairing_dao(cls, db: AsyncSession, pairing: dict):
        """
        编辑角色-机器人-映射数据库操作

        :param db: orm对象
        :param pairing: 需要更新的角色-机器人-映射字典
        :return:
        """
        await db.execute(update(RobotRolePairing), [pairing])

    @classmethod
    async def delete_pairing_dao(cls, db: AsyncSession, pairing: PairingModel):
        """
        删除角色-机器人-映射数据库操作

        :param db: orm对象
        :param pairing: 角色-机器人-映射对象
        :return:
        """
        await db.execute(delete(RobotRolePairing).where(RobotRolePairing.pairing_id.in_([pairing.pairing_id])))

