from datetime import datetime, time
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.identification_statistics_do import IdentificationStatistics
from module_admin.entity.vo.identification_statistics_vo import Identification_statisticsModel, Identification_statisticsPageQueryModel
from utils.page_util import PageUtil


class Identification_statisticsDao:
    """
    识别统计模块数据库操作层
    """
    
    @classmethod
    async def get_today_identification_statistics(cls, query_db: AsyncSession):
        stmt = (
            select(IdentificationStatistics.employ, IdentificationStatistics.stranger, IdentificationStatistics.visitor, IdentificationStatistics.create_time)
            .where(IdentificationStatistics.create_time ==  datetime.now().date())
        )
        
        result = await query_db.execute(stmt) 
        return result.mappings().all()

    @classmethod
    async def get_identification_statistics_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据主键 自增获取识别统计详细信息

        :param db: orm对象
        :param id: 主键 自增
        :return: 识别统计信息对象
        """
        identification_statistics_info = (
            (
                await db.execute(
                    select(IdentificationStatistics)
                    .where(
                        IdentificationStatistics.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return identification_statistics_info

    @classmethod
    async def get_identification_statistics_detail_by_info(cls, db: AsyncSession, identification_statistics: Identification_statisticsModel):
        """
        根据识别统计参数获取识别统计信息

        :param db: orm对象
        :param identification_statistics: 识别统计参数对象
        :return: 识别统计信息对象
        """
        identification_statistics_info = (
            (
                await db.execute(
                    select(IdentificationStatistics).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return identification_statistics_info

    @classmethod
    async def get_identification_statistics_list(cls, db: AsyncSession, query_object: Identification_statisticsPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取识别统计列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 识别统计列表信息对象
        """
        query = (
            select(IdentificationStatistics)
            .where(
                IdentificationStatistics.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,
            )
            .order_by(IdentificationStatistics.id)
            .distinct()
        )
        identification_statistics_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return identification_statistics_list

    @classmethod
    async def add_identification_statistics_dao(cls, db: AsyncSession, identification_statistics: Identification_statisticsModel):
        """
        新增识别统计数据库操作

        :param db: orm对象
        :param identification_statistics: 识别统计对象
        :return:
        """
        db_identification_statistics = IdentificationStatistics(**identification_statistics.model_dump(exclude={}))
        db.add(db_identification_statistics)
        await db.flush()

        return db_identification_statistics

    @classmethod
    async def edit_identification_statistics_dao(cls, db: AsyncSession, identification_statistics: dict):
        """
        编辑识别统计数据库操作

        :param db: orm对象
        :param identification_statistics: 需要更新的识别统计字典
        :return:
        """
        await db.execute(update(IdentificationStatistics), [identification_statistics])

    @classmethod
    async def delete_identification_statistics_dao(cls, db: AsyncSession, identification_statistics: Identification_statisticsModel):
        """
        删除识别统计数据库操作

        :param db: orm对象
        :param identification_statistics: 识别统计对象
        :return:
        """
        await db.execute(delete(IdentificationStatistics).where(IdentificationStatistics.id.in_([identification_statistics.id])))

