from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.visitor_type_do import VisitorType
from module_admin.entity.vo.visitor_type_vo import Visitor_typeModel, Visitor_typePageQueryModel
from utils.page_util import PageUtil


class Visitor_typeDao:
    """
    访客类型模块数据库操作层
    """

    @classmethod
    async def get_system_visitor_type_list(cls, db: AsyncSession):
        stmt = (
            select(VisitorType.type_id, VisitorType.name, VisitorType.value)
        )
        return (await db.execute(stmt)).mappings().all()

    @classmethod
    async def get_visitor_type_detail_by_id(cls, db: AsyncSession, type_id: int):
        """
        根据访客类型ID获取访客类型详细信息

        :param db: orm对象
        :param type_id: 访客类型ID
        :return: 访客类型信息对象
        """
        visitor_type_info = (
            (
                await db.execute(
                    select(VisitorType)
                    .where(
                        VisitorType.type_id == type_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return visitor_type_info

    @classmethod
    async def get_visitor_type_detail_by_info(cls, db: AsyncSession, visitor_type: Visitor_typeModel):
        """
        根据访客类型参数获取访客类型信息

        :param db: orm对象
        :param visitor_type: 访客类型参数对象
        :return: 访客类型信息对象
        """
        visitor_type_info = (
            (
                await db.execute(
                    select(VisitorType).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return visitor_type_info

    @classmethod
    async def get_visitor_type_list(cls, db: AsyncSession, query_object: Visitor_typePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取访客类型列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 访客类型列表信息对象
        """
        query = (
            select(VisitorType)
            .where(
                VisitorType.name.like(f'%{query_object.name}%') if query_object.name else True,
                VisitorType.value == query_object.value if query_object.value else True,
            )
            .order_by(VisitorType.type_id)
            .distinct()
        )
        visitor_type_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return visitor_type_list

    @classmethod
    async def add_visitor_type_dao(cls, db: AsyncSession, visitor_type: Visitor_typeModel):
        """
        新增访客类型数据库操作

        :param db: orm对象
        :param visitor_type: 访客类型对象
        :return:
        """
        db_visitor_type = VisitorType(**visitor_type.model_dump(exclude={}))
        db.add(db_visitor_type)
        await db.flush()

        return db_visitor_type

    @classmethod
    async def edit_visitor_type_dao(cls, db: AsyncSession, visitor_type: dict):
        """
        编辑访客类型数据库操作

        :param db: orm对象
        :param visitor_type: 需要更新的访客类型字典
        :return:
        """
        await db.execute(update(VisitorType), [visitor_type])

    @classmethod
    async def delete_visitor_type_dao(cls, db: AsyncSession, visitor_type: Visitor_typeModel):
        """
        删除访客类型数据库操作

        :param db: orm对象
        :param visitor_type: 访客类型对象
        :return:
        """
        await db.execute(delete(VisitorType).where(VisitorType.type_id.in_([visitor_type.type_id])))

