from sqlalchemy import delete, select, update, desc
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.info_do import RobotInfo
from module_admin.entity.vo.info_vo import InfoModel, InfoPageQueryModel
from utils.page_util import PageUtil
from datetime import  datetime, time



class InfoDao:
    """
    机器人信息模块数据库操作层
    """
    
    
    @classmethod
    async def get_video_uri(cls, robot_id: int, query_db: AsyncSession):
        """根据机器人id获取视频流地址

        Args:
            robot_id (int): 机器人地址
            query_db (AsyncSession): _description_
        """
        stmt = (
            select(RobotInfo.video_uri)
            .select_from(RobotInfo)
            .where(RobotInfo.robot_id == robot_id)
        )
        
        result = await query_db.execute(stmt)
        
        return result.scalars().first()
    
    @classmethod
    async def refresh_robot_by_ids(cls, db: AsyncSession, robot_ids: str):
        id_list = map(int, robot_ids.strip(" ").split(','))
        refresh_result = (
            (
                await db.execute(
                    select(RobotInfo)
                    .where(RobotInfo.robot_id.in_(id_list))

                )

            )
            .scalars()
            .all()
        )
        return refresh_result

    @classmethod
    async def get_info_detail_by_id(cls, db: AsyncSession, robot_id: int):
        """
        根据机器人ID获取机器人信息详细信息

        :param db: orm对象
        :param robot_id: 机器人ID
        :return: 机器人信息信息对象
        """
        info_info = (
            (
                await db.execute(
                    select(RobotInfo)
                    .where(
                        RobotInfo.robot_id == robot_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return info_info

    @classmethod
    async def get_info_detail_by_info(cls, db: AsyncSession, info: InfoModel):
        """
        根据机器人信息参数获取机器人信息信息

        :param db: orm对象
        :param info: 机器人信息参数对象
        :return: 机器人信息信息对象
        """
        info_info = (
            (
                await db.execute(
                    select(RobotInfo).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return info_info

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

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 机器人信息列表信息对象
        """
        query = (
            select(RobotInfo)
            .where(
                RobotInfo.name.like(f'%{query_object.name}%') if query_object.name else True,
                RobotInfo.model == query_object.model if query_object.model else True,
                RobotInfo.serial == query_object.serial if query_object.serial else True,
                RobotInfo.online == query_object.online if query_object.online else True,
                RobotInfo.power == query_object.power if query_object.power else True,
                RobotInfo.run_time == query_object.run_time if query_object.run_time else True,
                RobotInfo.position == query_object.position if query_object.position else True,
                RobotInfo.image == query_object.image if query_object.image else True,
                RobotInfo.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,
                RobotInfo.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(desc(RobotInfo.create_time))
            .distinct()
        )
        info_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return info_list

    @classmethod
    async def add_info_dao(cls, db: AsyncSession, info: InfoModel):
        """
        新增机器人信息数据库操作

        :param db: orm对象
        :param info: 机器人信息对象
        :return:
        """
        db_info = RobotInfo(**info.model_dump(exclude={}))
        db.add(db_info)
        await db.flush()

        return db_info

    @classmethod
    async def edit_info_dao(cls, db: AsyncSession, info: dict):
        """
        编辑机器人信息数据库操作

        :param db: orm对象
        :param info: 需要更新的机器人信息字典
        :return:
        """
        await db.execute(update(RobotInfo), [info])

    @classmethod
    async def delete_info_dao(cls, db: AsyncSession, info: InfoModel):
        """
        删除机器人信息数据库操作

        :param db: orm对象
        :param info: 机器人信息对象
        :return:
        """
        await db.execute(delete(RobotInfo).where(RobotInfo.robot_id.in_([info.robot_id])))

