from datetime import datetime, time
from sqlalchemy import delete, select, update, desc
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.explanation_content_do import ExplanationContent
from module_admin.entity.vo.explanation_content_vo import Explanation_contentModel, Explanation_contentPageQueryModel
from utils.page_util import PageUtil


class Explanation_contentDao:
    """
    讲解内容模块数据库操作层
    """

    @classmethod
    async def get_explanation_content_detail_by_id(cls, db: AsyncSession, explanation_content_id: int):
        """
        根据主键ID获取讲解内容详细信息

        :param db: orm对象
        :param explanation_content_id: 主键ID
        :return: 讲解内容信息对象
        """
        explanation_content_info = (
            (
                await db.execute(
                    select(ExplanationContent)
                    .where(
                        ExplanationContent.explanation_content_id == explanation_content_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return explanation_content_info

    @classmethod
    async def get_explanation_content_detail_by_info(cls, db: AsyncSession, explanation_content: Explanation_contentModel):
        """
        根据讲解内容参数获取讲解内容信息

        :param db: orm对象
        :param explanation_content: 讲解内容参数对象
        :return: 讲解内容信息对象
        """
        explanation_content_info = (
            (
                await db.execute(
                    select(ExplanationContent).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return explanation_content_info

    @classmethod
    async def get_explanation_content_list(cls, db: AsyncSession, query_object: Explanation_contentPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取讲解内容列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 讲解内容列表信息对象
        """
        query = (
            select(ExplanationContent)
            .where(
                # ExplanationContent.title == query_object.title if query_object.title else True,
                # ExplanationContent.content == query_object.content if query_object.content else True,
                ExplanationContent.type == query_object.type if query_object.type else True,
                # ExplanationContent.spend_time == query_object.spend_time if query_object.spend_time else True,
                ExplanationContent.language == query_object.language if query_object.language else True,
                ExplanationContent.status == query_object.status if query_object.status else True,
                ExplanationContent.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,
                ExplanationContent.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(ExplanationContent.update_time))
            .distinct()
        )
        explanation_content_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return explanation_content_list

    @classmethod
    async def add_explanation_content_dao(cls, db: AsyncSession, explanation_content: Explanation_contentModel):
        """
        新增讲解内容数据库操作

        :param db: orm对象
        :param explanation_content: 讲解内容对象
        :return:
        """
        db_explanation_content = ExplanationContent(**explanation_content.model_dump(exclude={}))
        db.add(db_explanation_content)
        await db.flush()

        return db_explanation_content

    @classmethod
    async def edit_explanation_content_dao(cls, db: AsyncSession, explanation_content: dict):
        """
        编辑讲解内容数据库操作

        :param db: orm对象
        :param explanation_content: 需要更新的讲解内容字典
        :return:
        """
        await db.execute(update(ExplanationContent), [explanation_content])

    @classmethod
    async def delete_explanation_content_dao(cls, db: AsyncSession, explanation_content: Explanation_contentModel):
        """
        删除讲解内容数据库操作

        :param db: orm对象
        :param explanation_content: 讲解内容对象
        :return:
        """
        await db.execute(delete(ExplanationContent).where(ExplanationContent.explanation_content_id.in_([explanation_content.explanation_content_id])))

