from sqlalchemy import delete, select, update, or_
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.door_do import Door
from module_admin.entity.vo.door_vo import DoorModel, DoorPageQueryModel
from utils.page_util import PageUtil


class DoorDao:
    """
    门禁设备模块数据库操作层
    """
    
    @classmethod
    async def get_door_door_status(cls, db: AsyncSession):
        """获取门禁状态
        """
        stmt = (
            select(Door.indexCode, Door.name).select_from(Door).where(Door.permission=="1")
        )
        result = await db.execute(stmt)
        
        return result.mappings().all()

    @classmethod
    async def get_door_index_code_list(cls, db: AsyncSession, permission: str = None):
        """
        获取门禁设备索引编码列表
        """
        query = select(Door.indexCode).where(Door.permission == permission if permission else True)
        result = await db.execute(query)
        return [row[0] for row in result.all()]

    @classmethod
    async def get_door_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据主键自增获取门禁设备详细信息

        :param db: orm对象
        :param id: 主键自增
        :return: 门禁设备信息对象
        """
        door_info = (
            (
                await db.execute(
                    select(Door)
                    .where(
                        Door.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return door_info

    @classmethod
    async def get_door_detail_by_info(cls, db: AsyncSession, door: DoorModel):
        """
        根据门禁设备参数获取门禁设备信息

        :param db: orm对象
        :param door: 门禁设备参数对象
        :return: 门禁设备信息对象
        """
        door_info = (
            (
                await db.execute(
                    select(Door).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return door_info

    @classmethod
    async def get_door_list(cls, db: AsyncSession, query_object: DoorPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取门禁设备列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 门禁设备列表信息对象
        """
        query = (
            select(Door)
            .where(
                # Door.index_code == query_object.index_code if query_object.index_code else True,
                or_(
                    (
                        Door.name.like(f"%{query_object.key_word}%")
                        if query_object.key_word
                        else True
                    ),
                    (
                        Door.install_location.like(f"%{query_object.key_word}%")
                        if query_object.key_word
                        else True
                    ),
                ),
                # Door.status == query_object.status if query_object.status else True,
                (
                    Door.permission == query_object.permission
                    if query_object.permission
                    else True
                ),
            )
            .order_by(Door.id)
            .distinct()
        )
        door_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return door_list

    @classmethod
    async def add_door_dao(cls, db: AsyncSession, door: DoorModel):
        """
        新增门禁设备数据库操作

        :param db: orm对象
        :param door: 门禁设备对象
        :return:
        """
        db_door = Door(**door.model_dump(exclude={}))
        db.add(db_door)
        await db.flush()

        return db_door

    @classmethod
    async def edit_door_dao(cls, db: AsyncSession, door: dict):
        """
        编辑门禁设备数据库操作

        :param db: orm对象
        :param door: 需要更新的门禁设备字典
        :return:
        """
        await db.execute(update(Door), [door])

    @classmethod
    async def delete_door_dao(cls, db: AsyncSession, door: DoorModel):
        """
        删除门禁设备数据库操作

        :param db: orm对象
        :param door: 门禁设备对象
        :return:
        """
        await db.execute(delete(Door).where(Door.id.in_([door.id])))
