from datetime import datetime, time
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.identification_record_do import IdentificationRecord
from module_admin.entity.vo.identification_record_vo import Identification_recordModel, Identification_recordPageQueryModel
from utils.page_util import PageUtil


class Identification_recordDao:
    """
    识别记录模块数据库操作层
    """

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

        :param db: orm对象
        :param id: 主键 自增
        :return: 识别记录信息对象
        """
        identification_record_info = (
            (
                await db.execute(
                    select(IdentificationRecord)
                    .where(
                        IdentificationRecord.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return identification_record_info

    @classmethod
    async def get_identification_record_detail_by_info(cls, db: AsyncSession, identification_record: Identification_recordModel):
        """
        根据识别记录参数获取识别记录信息

        :param db: orm对象
        :param identification_record: 识别记录参数对象
        :return: 识别记录信息对象
        """
        identification_record_info = (
            (
                await db.execute(
                    select(IdentificationRecord).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return identification_record_info

    @classmethod
    async def get_identification_record_list(cls, db: AsyncSession, query_object: Identification_recordPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取识别记录列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 识别记录列表信息对象
        """
        query = (
            select(IdentificationRecord)
            .where(
                IdentificationRecord.person_name.like(f'%{query_object.person_name}%') if query_object.person_name else True,
                IdentificationRecord.door_name.like(f'%{query_object.door_name}%') if query_object.door_name else True,
                IdentificationRecord.status == query_object.status if query_object.status else True,
                IdentificationRecord.source == query_object.source if query_object.source else True,
                IdentificationRecord.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(IdentificationRecord.create_time.desc())
            .distinct()
        )
        identification_record_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return identification_record_list

    @classmethod
    async def add_identification_record_dao(cls, db: AsyncSession, identification_record: Identification_recordModel):
        """
        新增识别记录数据库操作

        :param db: orm对象
        :param identification_record: 识别记录对象
        :return:
        """
        db_identification_record = IdentificationRecord(**identification_record.model_dump(exclude={}))
        db.add(db_identification_record)
        await db.flush()

        return db_identification_record

    @classmethod
    async def edit_identification_record_dao(cls, db: AsyncSession, identification_record: dict):
        """
        编辑识别记录数据库操作

        :param db: orm对象
        :param identification_record: 需要更新的识别记录字典
        :return:
        """
        await db.execute(update(IdentificationRecord), [identification_record])

    @classmethod
    async def delete_identification_record_dao(cls, db: AsyncSession, identification_record: Identification_recordModel):
        """
        删除识别记录数据库操作

        :param db: orm对象
        :param identification_record: 识别记录对象
        :return:
        """
        await db.execute(delete(IdentificationRecord).where(IdentificationRecord.id.in_([identification_record.id])))

