from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.robot_action_do import RobotAction
from module_admin.entity.vo.robot_action_vo import Robot_actionModel, Robot_actionPageQueryModel
from utils.page_util import PageUtil


class Robot_actionDao:
    """
    机器人动作模块数据库操作层
    """

    @classmethod
    async def get_robot_action_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据主键 自增获取机器人动作详细信息

        :param db: orm对象
        :param id: 主键 自增
        :return: 机器人动作信息对象
        """
        robot_action_info = (
            (
                await db.execute(
                    select(RobotAction)
                    .where(
                        RobotAction.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return robot_action_info

    @classmethod
    async def get_robot_action_detail_by_info(cls, db: AsyncSession, robot_action: Robot_actionModel):
        """
        根据机器人动作参数获取机器人动作信息

        :param db: orm对象
        :param robot_action: 机器人动作参数对象
        :return: 机器人动作信息对象
        """
        robot_action_info = (
            (
                await db.execute(
                    select(RobotAction).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return robot_action_info

    @classmethod
    async def get_robot_action_list(cls, db: AsyncSession, query_object: Robot_actionPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取机器人动作列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 机器人动作列表信息对象
        """
        query = (
            select(RobotAction)
            .where(
                RobotAction.id == query_object.id if query_object.id else True,
                RobotAction.name.like(f'%{query_object.name}%') if query_object.name else True,
                RobotAction.status == query_object.status if query_object.status else True,
            )
            .order_by(RobotAction.id)
            .distinct()
        )
        robot_action_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return robot_action_list

    @classmethod
    async def add_robot_action_dao(cls, db: AsyncSession, robot_action: Robot_actionModel):
        """
        新增机器人动作数据库操作

        :param db: orm对象
        :param robot_action: 机器人动作对象
        :return:
        """
        db_robot_action = RobotAction(**robot_action.model_dump(exclude={}))
        db.add(db_robot_action)
        await db.flush()

        return db_robot_action

    @classmethod
    async def edit_robot_action_dao(cls, db: AsyncSession, robot_action: dict):
        """
        编辑机器人动作数据库操作

        :param db: orm对象
        :param robot_action: 需要更新的机器人动作字典
        :return:
        """
        await db.execute(update(RobotAction), [robot_action])

    @classmethod
    async def delete_robot_action_dao(cls, db: AsyncSession, robot_action: Robot_actionModel):
        """
        删除机器人动作数据库操作

        :param db: orm对象
        :param robot_action: 机器人动作对象
        :return:
        """
        await db.execute(delete(RobotAction).where(RobotAction.id.in_([robot_action.id])))

