from datetime import datetime, time
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.explanation_style_do import ExplanationStyle
from module_admin.entity.vo.explanation_style_vo import Explanation_styleModel, Explanation_stylePageQueryModel
from utils.page_util import PageUtil


class Explanation_styleDao:
    """
    讲解风格模块数据库操作层
    """

    @classmethod
    async def get_explanation_style_detail_by_id(cls, db: AsyncSession, explanation_style_id: int):
        """
        根据主键ID获取讲解风格详细信息

        :param db: orm对象
        :param explanation_style_id: 主键ID
        :return: 讲解风格信息对象
        """
        explanation_style_info = (
            (
                await db.execute(
                    select(ExplanationStyle)
                    .where(
                        ExplanationStyle.explanation_style_id == explanation_style_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return explanation_style_info

    @classmethod
    async def get_explanation_style_detail_by_info(cls, db: AsyncSession, explanation_style: Explanation_styleModel):
        """
        根据讲解风格参数获取讲解风格信息

        :param db: orm对象
        :param explanation_style: 讲解风格参数对象
        :return: 讲解风格信息对象
        """
        explanation_style_info = (
            (
                await db.execute(
                    select(ExplanationStyle).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return explanation_style_info

    @classmethod
    async def get_explanation_style_list(cls, db: AsyncSession, query_object: Explanation_stylePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取讲解风格列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 讲解风格列表信息对象
        """
        query = (
            select(ExplanationStyle)
            .where(
                ExplanationStyle.name.like(f'%{query_object.name}%') if query_object.name else True,
                ExplanationStyle.detail == query_object.detail if query_object.detail else True,
                ExplanationStyle.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,
                ExplanationStyle.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(ExplanationStyle.explanation_style_id)
            .distinct()
        )
        explanation_style_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return explanation_style_list

    @classmethod
    async def add_explanation_style_dao(cls, db: AsyncSession, explanation_style: Explanation_styleModel):
        """
        新增讲解风格数据库操作

        :param db: orm对象
        :param explanation_style: 讲解风格对象
        :return:
        """
        db_explanation_style = ExplanationStyle(**explanation_style.model_dump(exclude={}))
        db.add(db_explanation_style)
        await db.flush()

        return db_explanation_style

    @classmethod
    async def edit_explanation_style_dao(cls, db: AsyncSession, explanation_style: dict):
        """
        编辑讲解风格数据库操作

        :param db: orm对象
        :param explanation_style: 需要更新的讲解风格字典
        :return:
        """
        await db.execute(update(ExplanationStyle), [explanation_style])

    @classmethod
    async def delete_explanation_style_dao(cls, db: AsyncSession, explanation_style: Explanation_styleModel):
        """
        删除讲解风格数据库操作

        :param db: orm对象
        :param explanation_style: 讲解风格对象
        :return:
        """
        await db.execute(delete(ExplanationStyle).where(ExplanationStyle.explanation_style_id.in_([explanation_style.explanation_style_id])))

